How do I evaluate the following integral when where $C$ is the square with vertices at $\pm2, \pm2+4i$,
$$\frac{1}{2 \pi i} \oint_{C} \frac{z^{2} d z}{z^{2}+4}$$
Using Cauchy integral:
$\frac{z^{2}}{z^{2}+4}=\frac{z^2}{(z+2i)(z-2i)}=\frac12\frac{z^2}{z+2i}+\frac12\frac{z^2}{z-2i}$ then $\frac{1}{2 \pi i} \oint_{C} \frac{z^{2} d z}{z^{2}+4} \implies \frac{1}{2 \pi i} \oint_{C} \frac12\frac{z^2 d z}{z+2i}+\frac{1}{2 \pi i} \oint_{C} \frac12\frac{z^2 d z}{z-2i}$
$\frac{z^2}{z+2i}$ is analytic on and inside $C$, hence we can apply Cauchy theorem and for the second term we use Cauchy integral formula,
$$\frac{1}{2 \pi i} \oint_{C} \frac12\frac{z^2 d z}{z+2i}+\frac{1}{2 \pi i} \oint_{C} \frac12\frac{z^2 d z}{z-2i}=0+\frac{1}{4\pi i}2\pi i \times f(2i)=-2$$
Using Residue Theorem:
$2i$ is the only isolated singularity in $C$.
$$\frac{1}{2 \pi i} \oint_{C} \frac{z^{2} d z}{z^{2}+4}=\frac{1}{2 \pi i} 2\pi i \times \text{Res}(f,2i)=i$$
I get correct answer for Residue Theorem but couldn't understand where I do wrong when using Cauchy integral.
It will be great help if someone clear me when to use which method to find the integral.
Here you don't have to use partial fractions but use $f(z) = \frac{z^2}{z+2i}$ for $f(a)$ in the Cauchy Integral Formula, since $f$ is holomorphic in the smallest open disk that fits your $C$.
Then you get $f(2i) = \frac{(2i)^2}{4i} = i$