Junit 如何使用jMockit将模拟传递到unitundertest?
我想通过一个模拟考试。如何在JMockit中实现这一点? 请参阅以下代码摘录Junit 如何使用jMockit将模拟传递到unitundertest?,junit,jmockit,Junit,Jmockit,我想通过一个模拟考试。如何在JMockit中实现这一点? 请参阅以下代码摘录 @Test public void paymentResponseCreatorTest(){ final ClassUnderTest unitUnderTest = new ClassUnderTest(); MockUp<JSONObject> mockup = new MockUp<JSONObject>(){ private final Map<
@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);
}
您可以按如下方式使用:
Deencapsulation.setField(referenceOfUnitUnderTest, referenceOfMockedOrRealInstance);
您可以使用@Tested和@Injectable注释。但如果不知道哪个对象需要被传递到ClassUnderTest中,我无法确定什么更合适。