如何测试接收ServerRequest
并返回Mono<ServerResponse>
的 handler 方法?
我可以通过ServerRequest
创建一个org.springframework.mock.web.reactive.function.server.MockServerRequest.builder()
。然后在ServerResponse.statusCode()
上声明。但是,我想测试此ServerResponse
的正文,但无法提取它。
ServerResponse response = target.search(MockServerRequest.builder()
.method(HttpMethod.GET)
.build()
).block();
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
//assertThat(response.body()).isNotNull();
我不想使用WebTestClient
进行更广泛的测试,我想使用单元测试来测试所有可能的响应情况。
谢谢
答案 0 :(得分:2)
因此,看来最好的解决方案是使用WebTestClient
。但是,可以在没有运行服务器的情况下使用此服务器。
spring-test
模块包括一个WebTestClient
,可用于在有或没有运行服务器的情况下测试WebFlux服务器端点 。
技巧是使用bindTo..()
构建器方法。就我而言,bindToRouterFunction(new MyRouter(new MyHandler()))
效果很好
答案 1 :(得分:1)
我有完全相同的问题,因为我不想为每个测试都启动整个Spring上下文。感谢您自己回答中的信息,这对我有所帮助。这是一个完整的示例,包括Mockito和JUnit5:
import java.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;
@ExtendWith(MockitoExtension.class)
class UsersHandlerTest {
@Mock
UserRepository userRepository;
WebTestClient client;
@BeforeEach
public void setUp() {
UsersHandler usersHandler = new UsersHandler(userRepository);
Router router = new Router(usersHandler);
Mockito.when(userRepository.findAll()).thenReturn(
Arrays.asList("", ""));
client = WebTestClient.bindToRouterFunction(router.getUsers()).build();
}
@Test
public void getCommands() {
client.get().uri("/users")
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
.expectStatus().isOk()
.expectBody().json("{}");
}
}
答案 2 :(得分:0)
我已经要求 similar question 坚持不使用 WebTestClient。 在发现可以将响应转换为允许检查主体(实体)的类型后,我最终回答了我自己的问题。
魔术线(Kotlin)是:
df <- structure(list(source = c("o", "o", "n", "n", "o", "o", "n",
"n", "o", "o", "n", "n", "o", "o", "n", "n", "o", "o", "n", "n",
"o", "o", "n", "n"), activty = c("low", "high", "low", "high",
"low", "high", "low", "high", "low", "high", "low", "high", "low",
"high", "low", "high", "low", "high", "low", "high", "low", "high",
"low", "high"), jar = c("blank1", "blank1", "blank1", "blank1",
"jar1", "jar1", "jar1", "jar1", "jar2", "jar2", "jar2", "jar2",
"blank2", "blank2", "blank2", "blank2", "jar3", "jar3", "jar3",
"jar3", "jar4", "jar4", "jar4", "jar4"), sampling = c("blank",
"blank", "blank", "blank", "soil", "soil", "soil", "soil", "soil",
"soil", "soil", "soil", "blank", "blank", "blank", "blank", "soil",
"soil", "soil", "soil", "soil", "soil", "soil", "soil"), x = c(34L,
31L, 32L, 35L, 21L, 22L, 23L, 23L, 27L, 28L, 29L, 27L, 34L, 32L,
31L, 36L, 25L, 25L, 26L, 25L, 19L, 18L, 20L, 20L), y = c(46L,
43L, 44L, 47L, 33L, 34L, 34L, 35L, 39L, 46L, 41L, 39L, 45L, 44L,
45L, 48L, 37L, 37L, 38L, 37L, 34L, 30L, 34L, 33L), x_pct = c(1,
1.02, 0.98, 1.01, 1.62, 1.43, 1.37, 1.54, 1.26, 1.13, 1.09, 1.31,
1, 0.98, 1.02, 0.99, 1.36, 1.26, 1.21, 1.42, 1.79, 1.75, 1.58,
1.78), y_pct = c(0.99, 1.01, 1.01, 1.01, 1.38, 1.28, 1.31, 1.36,
1.17, 0.95, 1.09, 1.22, 1.01, 0.99, 0.99, 0.99, 1.23, 1.18, 1.17,
1.28, 1.34, 1.45, 1.31, 1.44)), class = "data.frame", row.names = c(NA,
-24L))