Difference between @TestSubject and @InjectMocks?

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 annotation is used to identify class which is going to use
    // the mock object
    @TestSubject
    MathApplication mathApplication = new MathApplication();

    // @Mock annotation is used to create the mock object to be injected
    @Mock
    CalculatorService calcService;

    @Test(expected = RuntimeException.class)
    public void testAdd() {
        // add the behavior to throw exception
        Mockito.doThrow(new RuntimeException("Add operation not implemented")).when(calcService).add(10.0, 20.0);

        // test the add functionality
        Assert.assertEquals(mathApplication.add(10.0, 20.0), 30.0, 0);
    }
}

.
  1. - ? , ?
 2. , @TestSubject @InjectMocks?

+4
1

@TestSubject - EasyMock, , Mockito @InjectMocks. Mockito, @InjectMocks.

+6

Source: https://habr.com/ru/post/1668606/


All Articles