While learning Mockito, I found two different annotations @TestSubject and @InjectMocks from the links below.
@TestSubject Ref
@InjectMocks Ref < usch >
@InjectMocksannotation works absolutely fine, as explained in the tutorial, but @TestSubjectdoes not work, but rather displays an error.
I get an TestSubject cannot be resolved to a typeannotation error @TestSubjectin the code snippet below, however I made the correct setup (including Junit and Mockito files ) in the build path).
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import com.infosys.junitinteg.action.MathApplication;
import com.infosys.junitinteg.service.CalculatorService;
@RunWith(MockitoJUnitRunner.class)
public class MathApplicationTester {
@TestSubject
MathApplication mathApplication = new MathApplication();
@Mock
CalculatorService calcService;
@Test(expected = RuntimeException.class)
public void testAdd() {
Mockito.doThrow(new RuntimeException("Add operation not implemented")).when(calcService).add(10.0, 20.0);
Assert.assertEquals(mathApplication.add(10.0, 20.0), 30.0, 0);
}
}
.
1. - ? , ?
2. , @TestSubject @InjectMocks?