在学习Mockito时,我在下面的参考文献中发现了两个不同的注释@TestSubject和@InjectMocks
@TestSubject Ref
@InjectMocks Ref@InjectMocks
注释工作得非常好,如教程中所述,但@TestSubject
不工作,而是显示错误
我在下面的代码片段中收到@TestSubject
注释的TestSubject cannot be resolved to a type
错误,但我已经进行了正确的设置(包括构建路径中的Junit&Mockitojar文件)。
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
注释之间有什么区别
@TestSubject是EasyMock的注释,其作用与Mockito的@InjectMocks相同。如果您使用的是Mockito,那么您必须使用@InjectMocks
。