2

我正在使用 MockWebServiceServer 来测试 REST API。我使用@Runwith(Parameterized.class) 将值传递给它。

   @RunWith(Parameterized.class)
   public class MyAPITest {

        protected static MockWebServiceServer mockServer;
        private Message message;

        public MyAPITest(Message messageIn) {
            this.message = messageIn;
        }

        @BeforeClass
        public static void setup(){
            mockServer = MockWebServiceServer.createServer(applicationContext);
        }

        @Test
        public final void testMethod() throws Throwable {
            Source reqPayload1 = new StringSource("...");
            Source reqPayload2 = new StringSource("...");
            Source resPayload1 = new StringSource("...");
            Source resPayload2 = new StringSource("...");
            mockServer.expect(RequestMatchers.payload(reqPayload1 )).andRespond(ResponseCreators.withPayload(resPayload1 ));
            //Unable to add below line as it throws exception. Unable to set multiple expectation
            //mockServer.expect(RequestMatchers.payload(reqPayload2 )).andRespond(ResponseCreators.withPayload(resPayload2 ));
            myClass.onMessage(this.message);
            mockServer.verify();
        }

        @Parameters
        public static Collection<Object[]> getParameters() {
            //Read input data from file
        }
    }

当我只有 1 个输入时,代码工作正常。

但是当我有多个输入时它会抛出异常。

java.lang.IllegalStateException: Can not expect another connection, the test is already underway
    at org.springframework.util.Assert.state(Assert.java:385)
    at org.springframework.ws.test.client.MockWebServiceMessageSender.expectNewConnection(MockWebServiceMessageSender.java:64)
    at org.springframework.ws.test.client.MockWebServiceServer.expect(MockWebServiceServer.java:162)
    at com.rakuten.gep.newsletter.batch.ExacttargetMQJobTest.testOnMessage(ExacttargetMQJobTest.java:82)

我正在使用 Spring 3.2。我想用多个输入测试我的 api。

4

0 回答 0