【发布时间】:2017-07-26 02:48:15
【问题描述】:
我是 Mockito 的新手。 代码:
public class A{
public A{
...
B.fff(); //the function I want to mock
...
}
}
public class B{
public boolean fff(){
...
... //connect DB
...
}
}
对于单元测试,
public class ATest{
@Test
public void test(){
A mock_a = new A();
Assert.assertNotNull(mock_a);
}
}
由于函数“B.fff()”需要连接数据库,所以我想模拟函数“B.fff()”,返回true或false,让测试可以在没有环境的情况下完全工作。
我尝试了一些类似的代码:
public class ATest{
@Test
public void test(){
PowerMockito.when(B.fff()).thenReturn(true);
Assert.assertNotNull(new A());
}
}
但它不起作用。
有没有使用 Mockito(或 PowerMock)的解决方案?
谢谢。
【问题讨论】:
标签: java unit-testing mockito powermock powermockito