访问MockUp类的属性

时间:2014-11-07 10:12:31

标签: jmockit

我想访问MockUp类实例WebServiceProxyInstance的requestContext属性。该属性仅存在于mock类中,并且不是已实现接口的一部分。有没有办法做到这一点?

@Test public <WebServiceProxy extends WebService & BindingProvider> void callWebServiceTest() {
    final WebService WebServiceProxyInstance = new MockUp<WebServiceProxy>() {  
        public Map<String, Object> requestContext = new HashMap<String, Object>();
        @Mock public Map<String, Object> getRequestContext() { return requestContext; }
    }.getMockInstance();

    System.out.println("Count: " + WebServiceProxyInstance.requestContext.count)
}

此代码抛出:

java.lang.Error: Unresolved compilation problem: requestContext cannot be resolved or is not a field

1 个答案:

答案 0 :(得分:0)

我错过了森林里的树木。我将requestContext声明移到mock类之外:

@Test public <WebServiceProxy extends WebService & BindingProvider> void callWebServiceTest() {

    public Map<String, Object> requestContext = new HashMap<String, Object>();

    final WebService WebServiceProxyInstance = new MockUp<WebServiceProxy>() {       
        @Mock public Map<String, Object> getRequestContext() { return requestContext; }
    }.getMockInstance();

    System.out.println("Count: " + WebServiceProxyInstance.requestContext.count)
}