Ash*_*mar 4 java junit mockito java-8
在学习Mockito时,我在下面的参考文献中找到了两个不同的注释@TestSubject和@InjectMocks.
@TestSubject Ref
@InjectMocks Ref
@InjectMocks annotation正如教程中所解释的那样工作得很好但@TestSubject不起作用而不是显示错误.
我在下面的代码片段中收到TestSubject cannot be resolved to a type了@TestSubject注释错误,但是我已经做了正确的设置(包括构建路径中的Junit和Mockito jar文件).
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我这里有两个问题.
有人遇到过类似的问题吗?如果是,那么根本原因和解决方案是什么?
2.如果它工作正常那么注释@TestSubject和@InjectMocks注释之间有什么区别?
| 归档时间: |
|
| 查看次数: |
1725 次 |
| 最近记录: |