Camel Mockserver预期未处于活动状态

33qvvth1  于 2022-11-07  发布在  Apache
关注(0)|答案(2)|浏览(198)

上下文是一个Junit测试,使用Spring-boot、Mockserver和Camel进行设置。
问题是verified请求的模拟响应在测试期间不匹配。
日志:

INFO 1841843 --- [erver-EventLog4] org.mockserver.log.MockServerEventLog    : 8087 received request:

  {
    "method" : "GET",
    "path" : "/api/installation/organizations",
    "headers" : {
      "content-length" : [ "0" ],
      "User-Agent" : [ "Java/11.0.13" ],
      "Host" : [ "localhost:8087" ],
      "Connection" : [ "keep-alive" ],
      "Accept" : [ "application/json" ]
    },
    "keepAlive" : true,
    "secure" : false
  }

INFO 1841843 --- [erver-EventLog4] org.mockserver.log.MockServerEventLog    : 8087 no expectation for:

  {
    "method" : "GET",
    "path" : "/api/installation/organizations",
    "headers" : {
      "content-length" : [ "0" ],
      "User-Agent" : [ "Java/11.0.13" ],
      "Host" : [ "localhost:8087" ],
      "Connection" : [ "keep-alive" ],
      "Accept" : [ "application/json" ]
    },
    "keepAlive" : true,
    "secure" : false
  }

 returning response:

  {
    "statusCode" : 404,
    "reasonPhrase" : "Not Found"
  }

INFO 1841843 --- [           main] b.h.c.route.CsvRouteBuilderTest          : Started CsvRouteBuilderTest in 6.17 seconds (JVM running for 8.32)
INFO 1841843 --- [erver-EventLog4] org.mockserver.log.MockServerEventLog    : retrieved requests and responses in json that match:

  { }

DEBUG 1841843 --- [           main] b.h.c.route.CsvRouteBuilderTest          : RecordedRequestsAndResponses: 12
INFO 1841843 --- [erver-EventLog4] org.mockserver.log.MockServerEventLog    : 8087 retrieved 0 active expectations in json that match:

  { }

DEBUG 1841843 --- [           main] b.h.c.route.CsvRouteBuilderTest          : activeExpectations: 0
INFO 1841843 --- [erver-EventLog4] org.mockserver.log.MockServerEventLog    : retrieved recorded expectations in json that match:

  { }

DEBUG 1841843 --- [           main] b.h.c.route.CsvRouteBuilderTest          : recordedExpectations: 0
INFO 1841843 --- [erver-EventLog4] org.mockserver.log.MockServerEventLog    : 8087 verifying requests that match:

  {
    "httpRequest" : {
      "method" : "GET",
      "path" : "/api/installation/organizations"
    },
    "times" : {
      "atLeast" : 1
    }
  }

INFO 1841843 --- [erver-EventLog4] org.mockserver.log.MockServerEventLog    : request:

  {
    "method" : "GET",
    "path" : "/api/installation/organizations"
  }

 found at least once

还有代码:

@ExtendWith(MockServerExtension.class)
@MockServerSettings(ports = {8087})
@SpringBootTest
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@DisableJmx
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class CsvRouteBuilderTest {

    @Autowired
    ProducerTemplate producerTemplate;

    private MockServerClient mockServerClient;

    @BeforeEach
    public void beforeEachLifecyleMethod(MockServerClient client) {
        mockServerClient = client;
    }

    @Before
    public void setupMocks() {
        mockServerClient
            .when(
                request().withMethod("GET").withPath(HDConnectProxyRestTemplate.URI_GET_INSTALLATION_ORGANIZATIONS)
                    .withHeader(Header.header("Accept", MediaType.APPLICATION_JSON.toString())))
            .respond(
                response("{data}").withContentType(MediaType.APPLICATION_JSON));
    }

我错过了什么?我可以看到Camel和Mockserver在测试的那一刻都在运行。但是模拟的响应应该被触发。

qv7cva1a

qv7cva1a1#

我发现spring-boot的上下文在单元测试本身之前启动了Camel逻辑。
因此,在测试时,Camel路径已经在运行,模拟服务器的预期不可用。
为了正确测试这种情况,我需要:
1.首先阻止Camel路由启动(例如用Route.configure()方法),
1.初始化模拟及其预期(@每次测试前),
1.然后,在Junit测试期间专门启用Camel路由。

9avjhtql

9avjhtql2#

由于BeforeAllCallback启动了mock服务器,另一种方法是初始化mocker服务器beforeAll,如下所示:

@BeforeAll
static void setupMocks(MockServerClient mockServerClient) {
    mockServerClient
        .when(
            request().withMethod("GET").withPath(HDConnectProxyRestTemplate.URI_GET_INSTALLATION_ORGANIZATIONS)
                .withHeader(Header.header("Accept", MediaType.APPLICATION_JSON.toString())))
        .respond(
            response("{data}").withContentType(MediaType.APPLICATION_JSON));
}

通过这种方式,您的模拟服务器将在Camel启动之前进行配置。

相关问题