- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有:
Router.router(vertx).route().handler(new Handler<RoutingContext>() {
@Override
public void handle(RoutingContext event) {
HttpServerRequest request = event.request();
Buffer body = event.getBody();
HttpServerResponse response = event.response();
// Some code to test
response.setStatusCode(200);
response.end();
}
});
如何通过提供准备好的请求和正文并检查响应来对此进行单元测试?
最佳答案
通常测试此类端点更多的是集成测试而不是单元测试。没必要 mock 一切。模拟旨在测试一些在您的测试上下文中不可用的外部服务。这是使用 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
}
关于java - 如何在 Vert.x 中对 Handler<RoutingContext> 进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59378705/
我正在使用 ReactJS 和 react-router 构建同构应用程序在服务器端用于路由目的的模块。 来自其guide关于在服务器上使用 react-router: (req, res) => {
实际上我正在使用 Vertx Routher,并且我有一个 header 中包含 Authorization 的请求,并且想要查找有关 token 的更多信息,因此我尝试创建 KeycloakSecu
我的应用程序是带有 react-router 的 ES6 React 应用程序。我想在一小段延迟后将用户重定向到另一个页面。这是我的 React 组件: import React from 'reac
我有: Router.router(vertx).route().handler(new Handler() { @Override public void handle(Routin
我正在尝试使用 React 和 React-router 进行服务器端渲染。到目前为止,它只是来自各种来源的复制粘贴代码。但是当我尝试使用 Node 运行应用程序时出现语法错误(不是运行时错误)。下面
我正在设置一个简单的玩具应用程序来学习 React/Hapi,并且一切正常,直到我尝试设置服务器端路由。服务器运行无误,并使用 hello world 正确呈现“/”。 但是,当我导航到“/test”
我是一名优秀的程序员,十分优秀!