【发布时间】:2018-07-27 04:57:32
【问题描述】:
我正在构建一个 spring webflux 项目,其中我已经实现了路由功能作为具有 spring 客户驱动合同的控制器。 我在执行测试用例时遇到了问题,我在任何地方都没有找到任何关于此问题的在线解决方案。 我的要求是执行生成的测试并为 n 加载应用程序上下文。的测试。下面是示例代码:
==========Base class===========
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class GetBase extends SampleParentBase{
protected MockMvc mvc ;
@MockBean
private PersonService service;
@MockBean
private ServerRequest request;
@Before
public void setup() throws Exception {
mvc = MockMvcBuilders
.webAppContextSetup(context).build();
}
}
============groovy file==================
Contract.make {
description "."
request {
method GET()
urlPath('/person/1')
headers {
contentType(applicationJson())
header('''Accept''', applicationJson())
}
}
response {
headers {
contentType(applicationJson())
}
status 200
bodyMatchers {
jsonPath('$.name', byType())
jsonPath('$.age', byType())
jsonPath('$.pId', byType())
}
body ('''{
"name":"string",
"age":20,
"pId":"string"
}''')
}
}
=======================Router Configuration====================
@Configuration
public class RoutesConfiguration {
@Autowired
PersonRespository personRespository;
@Bean
RouterFunction<?> routes() {
return nest(path("/person"),
route(RequestPredicates.GET("/{id}"),
request -> ok().body(personRespository.findById(request.pathVariable("id")), Person.class))
.andRoute(method(HttpMethod.GET),
request -> ok().body(personRespository.findAll(), Person.class))
);
}
}
【问题讨论】:
标签: spring reactive-programming spring-webflux