我想在timeServiceMock上设置一些共同的期望,但在方法之前它仍然是null。有没有办法让它工作而不需要在每次测试中调用before()?
@RunWith(EasyMockRunner.class)
public class MyTest extends EasyMockSupport {
...@TestSubject and so on
@Mock
private TimeService timeServiceMock;
@Before
public void before() {
System.out.println(this.timeServiceMock);
}
...tests
}
我错过了什么吗?
答案 0 :(得分:0)
看起来EasyMockRunner只在@Test中创建并注入模拟。
所以如果你想在@Before中使用它,请从TimeService
中删除@Mock注释,并在@Before中自己创建模拟。然后你必须自己将模拟注入@TestSubject。
这样的事情:
@RunWith(EasyMockRunner.class)
public class MyTest extends EasyMockSupport {
@TestSubject and so on
Foo testSubject = ...
private TimeService timeServiceMock;
@Before
public void before() {
this.timeServiceMock = createMock(TimeService.class);
...
}
@Test
public void mytest(){
testSubject.setTimeService(timeServiceMock);
...
}
}