It is permissible to castrate animals, if done for a beneficial purpose. However, anything that is
removed from an animal that is lawful to be eaten, while it is still alive, is Haram (prohibited) to eat, as would meat be from an animal that was not lawfully slaughtered. Consequently, it is not permissible to eat the testicles of the animal if they are cut off while the animal is alive. In fact it is Haram, as the Prophet (peace be upon him) said,
“Whatever is cut from an animal while it is alive is Maytah (unslaughtered dead meat).”
May Allah grant us success. May peace and blessings be upon our Prophet Muhammad, his family, and Companions.