Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何对处理程序进行单元测试<;路由上下文>;在Vert.x中?_Java_Unit Testing_Junit_Vert.x - Fatal编程技术网

Java 如何对处理程序进行单元测试<;路由上下文>;在Vert.x中?

Java 如何对处理程序进行单元测试<;路由上下文>;在Vert.x中?,java,unit-testing,junit,vert.x,Java,Unit Testing,Junit,Vert.x,我有: Router.Router(vertx.route().handler(新处理程序)(){ @凌驾 公共无效句柄(RoutingContext事件){ HttpServerRequest=event.request(); bufferbody=event.getBody(); HttpServerResponse=event.response(); //一些要测试的代码 响应。设置状态代码(200); response.end(); } }); 如何通过给出准备好的请求和正文以及检查响

我有:

Router.Router(vertx.route().handler(新处理程序)(){
@凌驾
公共无效句柄(RoutingContext事件){
HttpServerRequest=event.request();
bufferbody=event.getBody();
HttpServerResponse=event.response();
//一些要测试的代码
响应。设置状态代码(200);
response.end();
}
});

如何通过给出准备好的请求和正文以及检查响应来进行单元测试?

这里要测试的单元是您的
处理程序的实现

因此,将匿名类更改为(至少)一个(非
private
static
内部类


通常,测试这些端点更多的是集成测试,而不是单元测试。 没有必要嘲笑一切。模拟是为了测试一些在测试上下文中不可用的外部服务。下面是一个使用vert.x进行集成测试的示例:

@ExtendWith(VertxExtension.class)
class IntegrationTest {
private static RequestSpecification requestSpecification;

  @BeforeAll
  static void prepareSpec() {
    requestSpecification = new RequestSpecBuilder()
      .addFilters(asList(new ResponseLoggingFilter(), new RequestLoggingFilter()))
      .setBaseUri("http://localhost:3002/")// Depends on your verticle config.
      .build();
  }

@BeforeEach
  void setup(Vertx vertx, VertxTestContext testContext) {

    vertx
        .deployVerticle(new MyVerticle(), 
                ar -> {
                    if(ar.succeeded()) {
                        testContext.completeNow();
                    } else {
                        testContext.failNow(ar.cause());
                    }
                }
        );
  }

@Test
  @DisplayName("Test message")
  void test() {
    JsonObject body = new JsonObject();//Fill it as you want

    given(requestSpecification)
      .contentType(ContentType.JSON)
      .body(body.encode())
      .post("/yourreqest")//Your resource path
      .then()
      .assertThat()
      .statusCode(200);//expected status
}

启动Vert.x HttpServer怎么样?它启动得相当快
@ExtendWith(MockitoExtension.class)
@RunWith(JUnitPlatform.class)
class MyHandlerTest {
   @Mock
   RoutingContext event;

   @Mock
   HttpServerRequest request;

   @Mock
   HttpServerResponse response;

   @BeforeEach
   public void setup(){
     doReturn(request).when(event).getRequest();
     doReturn(response).when(event)event.response();
  }

  @Test
  public void shouldSetStatusTo200whenNoError(){
    // arrange
    doReturn(SOME_VALID_BODY).when(request).getBody();
    // act
    new MyHandler().handle(event);
    // assert
    Mockito.verify(response).setStatusCode(200);
  }
}
@ExtendWith(VertxExtension.class)
class IntegrationTest {
private static RequestSpecification requestSpecification;

  @BeforeAll
  static void prepareSpec() {
    requestSpecification = new RequestSpecBuilder()
      .addFilters(asList(new ResponseLoggingFilter(), new RequestLoggingFilter()))
      .setBaseUri("http://localhost:3002/")// Depends on your verticle config.
      .build();
  }

@BeforeEach
  void setup(Vertx vertx, VertxTestContext testContext) {

    vertx
        .deployVerticle(new MyVerticle(), 
                ar -> {
                    if(ar.succeeded()) {
                        testContext.completeNow();
                    } else {
                        testContext.failNow(ar.cause());
                    }
                }
        );
  }

@Test
  @DisplayName("Test message")
  void test() {
    JsonObject body = new JsonObject();//Fill it as you want

    given(requestSpecification)
      .contentType(ContentType.JSON)
      .body(body.encode())
      .post("/yourreqest")//Your resource path
      .then()
      .assertThat()
      .statusCode(200);//expected status
}