无法使用MockWebServiceServer设置多个期望

时间:2017-07-13 08:18:00

标签: spring-test parameterized spring-junit mockwebserver

我正在使用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。

0 个答案:

没有答案