如何使用jMockit将模拟传递给unitundertest?

时间:2014-12-12 11:08:24

标签: junit jmockit

我想将Mock传递给我的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);
}

1 个答案:

答案 0 :(得分:0)

您可以按如下方式使用Deencapsulation

    Deencapsulation.setField(referenceOfUnitUnderTest, referenceOfMockedOrRealInstance);
相关问题