我一直在尝试运行发出 Web 服务请求的单元测试。我正在使用 EasyMock 在我的 JUnit 测试中模拟 jax-ws 请求的代理对象。
我在应用程序上下文中使用 DI 定义了 bean,如下所示:
<bean id="mockOrderPort" name="mockOrderPort" class="org.easymock.EasyMock" factory-method="createStrictMock" >
<constructor-arg value="com.proyecti.perama.siman.replica.integration.schema.OrderPort" />
</bean>
这是失败的测试用例:
//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);
在 executeTest 方法中,有使用模拟代理对象对 WS 的调用:
authenticationResponse = portToServer.authentication(authenticationRequest);
无论我尝试什么,但它总是尝试连接到实际的 WS,从而引发以下异常:
authentication request has not been successful. Exception: com.sun.xml.ws.client.ClientTransportException: HTTP Transport error: java.net.ConnectException: Connection refused: connect
为什么模拟对象试图连接而不是返回我创建的对象?
谢谢!