描述:
我似乎无法让我的存根或嘲笑的I类有下试生效。 我试图使用whenNew动作,所以我可以模拟一个返回对象,然后嘲笑那个对象上的操作与返回的值。
我想象它的一些简单的我失踪,但没有看到它。
解决方案:本来我是用跑MockitoRunner.class
,它需要被改变,以PowerMockRunner.class
。 下面的代码反映了解决方案。
在classpath罐:
- powermock-的Mockito-1.4.11-full.jar
- mockoito-ALL-1.9.0.jar
- Javassist进行-3.15.0-GA.jar
- 基于JUnit 4.8.2.jaf
- objensis-1.2.jar
- CGLIB的节点p-2.2.2.jar
测试类
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import static org.powermock.api.mockito.PowerMockito.*;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Matchers.any;
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassA.class)
public class ClassATest {
@Test
public void test() throws Exception
{
String[] returnSomeValue = {"PowerMockTest"};
String[] inputValue = {"Test1"};
ClassB mockedClassB = mock(ClassB.class);
whenNew( ClassB.class).withNoArguments().thenReturn( mockedClassB );
when( mockedClassB, "getResult", any(String[].class) ).thenReturn(returnSomeValue);
IClassA classUnderTest = new ClassA();
String[] expectedValue = classUnderTest.runTest(inputValue);
}
}
一个类的实现
public class ClassA implements IClassA {
@Override
public String[] runTest(String[] inputValues) {
String[] result;
IClassB classB = new ClassB();
result = classB.getResult(inputValues);
return result;
}
}