我想传递一个模拟到我的unitUnderTest。如何在JMockit中实现这一点?请参阅下面的代码摘录。
@Test
public void paymentResponseCreatorTest(){
final ClassUnderTest unitUnderTest = new ClassUnderTest();
MockUp<JSONObject> mockup = new MockUp<JSONObject>(){
private final Map<String, JSONValue> map = new HashMap<>();
@Mock
public JSONValue put(String key, JSONValue jsonValue){
map.put(key, jsonValue);
return jsonValue;
}
@Mock
public JSONValue get(String key){
return map.get(key);
}
};
JSONObject jsonObject = mockup.getMockInstance();
jsonObject.put("Status", new JSONString("Y"));
jsonObject.put("a", new JSONString("123"));
jsonObject.put("b", new JSONString("123"));
unitUnderTest.newInstance(jsonObject);
}发布于 2015-04-08 23:24:32
您可以按如下方式使用Deencapsulation:
Deencapsulation.setField(referenceOfUnitUnderTest, referenceOfMockedOrRealInstance);https://stackoverflow.com/questions/27442345
复制相似问题