1

I'm stuck trying to run a unit test that makes a web service request. I'm mocking the proxy object of the jax-ws request in my JUnit test using EasyMock.

I have defined the bean using DI in my application-context as follows:

<bean id="mockOrderPort" name="mockOrderPort" class="org.easymock.EasyMock" factory-method="createStrictMock" >
    <constructor-arg value="com.proyecti.perama.siman.replica.integration.schema.OrderPort" />
</bean>

This is the test case that is failing:

//II. Fill the authentication response will be used to mock the server calling
final AuthenticationResponse authenticationResponse = new AuthenticationResponse();
authenticationResponse.setToken(encode(TestConstants.EMPTY_TOKEN));

//III. When authentication is called, mock must return the authentication request object created earlier
expect(mockOrderPort.authentication(EasyMock.anyObject(AuthenticationRequest.class))).andReturn(authenticationResponse);

//IV. Make the mock object available for executing the test
replay(mockOrderPort);

//V. Execute the test (call to server is produced inside this method)
executeTest();

//VI. Verify mock behaviour is correct
verify(mockOrderPort);

Inside the executeTest method, there is the call to the WS using the mocked proxy object:

authenticationResponse = portToServer.authentication(authenticationRequest);

No matter what I try, but it ALWAYS tries to connect to the actual WS, raising the following exception:

authentication request has not been successful. Exception: com.sun.xml.ws.client.ClientTransportException: HTTP Transport error: java.net.ConnectException: Connection refused: connect

Why is the mock object trying to connect instead of returning the object I have created?

Thanks!

Antonio Acevedo
  • 1,480
  • 3
  • 21
  • 39
  • Have you tried debugging the test to see what is happening at the point where you mocked object is supposed to be firing? Where are you setting the mock object on the system under test? – cdugga Mar 21 '13 at 19:11
  • I have find that the app is not creating the mocked object, but the real one...so DI is not working. I have annotated the mock object in test class with @Resource. The mock object is private in CUT. Need I to configure anything else? – Antonio Acevedo Mar 21 '13 at 21:27
  • I have found a solution based on reflection viewing this post: http://stackoverflow.com/questions/10011480/how-to-inject-easymock-mock-into-tested-class-private-field Thank you ;) – Antonio Acevedo Mar 21 '13 at 22:22

0 Answers0