Search code examples
jmockit

Access properties of MockUp Classes


I would like to access the requestContext property of the MockUp class instance WebServiceProxyInstance. The property only exists in the mock class and is not part of the implemented interfaces. Is there any way to do this?

@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)
}

This code throws:

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

Solution

  • I missed the forest for the trees. I moved the requestContext declaration outside the mock class:

    @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)
    }