Junit 使用PowerMock的模拟方法中的NPE
我希望使用PowerMock模拟方法调用的输出。我的班级是这样的:Junit 使用PowerMock的模拟方法中的NPE,junit,powermock,easymock,Junit,Powermock,Easymock,我希望使用PowerMock模拟方法调用的输出。我的班级是这样的: public class TestEasyMock { private static TestEasyMock TEST_INSTANCE = new TestEasyMock(); public static TestEasyMock getInstance() { return TEST_INSTANCE; } private Cache<String, S
public class TestEasyMock {
private static TestEasyMock TEST_INSTANCE = new TestEasyMock();
public static TestEasyMock getInstance() {
return TEST_INSTANCE;
}
private Cache<String, String> first = CacheBuilder.newBuilder().
maximumSize(8192).expireAfterWrite(30, TimeUnit.MINUTES).build();
private Set<String> second = new TreeSet<String>();
public String testMethod (String testParam) {
return first.getIfPresent(testParam);
}
}
知道为什么会这样吗
谢谢 当您使用PowerMock.createStrictPartialMockforAllMethodsCept(TestEasyMock.class,“testMethod”)代码>,您指定testMethod不应被模拟。请参阅PowerMock API文档中的 1)确切的堆栈跟踪是什么?2)
TestUtils.replaceString(“可替换”)是如何实现的代码>适合?
@RunWith(PowerMockRunner.class)
@PrepareForTest({TestEasyMock.class})
public class EasyMockTest {
@Test
public void firstTest (){
suppress(constructor(TestEasyMock.class));
TestEasyMock testObject = PowerMock.createStrictPartialMockForAllMethodsExcept(TestEasyMock.class, "testMethod");
EasyMock.expect(testObject.testMethod("blabla")).andReturn("blaTwice");
EasyMock.replay(testObject);
String result = TestUtils.replaceString("replaceable");
assertEquals("replaceable(blaTwice)", result);
}
}