当我的控制器具有异步servlet性质时,如何在MockMvc中验证/测试500内部服务器错误?
我正在为我的REST端点编写单元测试用例作为测试用例的一部分我需要验证服务器发送500内部错误作为http代码并带有相应的错误消息.
这是我的基于春季启动的应用程序:(为了更好的可读性,省略了所有导入)
@RestController
@RequestMapping("/user")
@EnableAutoConfiguration
@SpringBootApplication
public class App
{
@RequestMapping(method = RequestMethod.GET, value = "/{name}",
produces = MediaType.APPLICATION_JSON_VALUE)
private DeferredResult<String> greetByJson(@PathVariable("name") final String name){
DeferredResult<String> dResult = new DeferredResult<String>();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
dResult.setErrorResult(new RuntimeException("Boom!!! time for Internal server error"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
return dResult;
}
public static void main( String[] args )
{
SpringApplication.run(App.class);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的MovkMvc JUnit测试用例:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Flux 来构建我的反应式管道。在管道中,我需要调用 3 个不同的外部系统 REST API,它们的访问速率非常严格。如果我违反了每秒速率阈值,我将受到指数级的限制。每个系统都有自己的阈值。
我正在使用 Spring WebClient 进行 REST API 调用;在 3 个 API 中,其中 2 个是 GET,1 个是 POST。
在我的反应器管道中,WebClient 被包裹在 flatMap 中以执行 API 调用,如下面的代码:
WebClient getApiCall1 = WebClient.builder().build().get("api-system-1").retrieve().bodyToMono(String.class) //actual return DTO is different from string
WebClient getApiCall2 = WebClient.builder().build().get("api-system-2").retrieve().bodyToMono(String.class) //actual return DTO is different from string
WebClient getApiCall3 = WebClient.builder().build().get("api-system-3").retrieve().bodyToMono(String.class) //actual return DTO is different from string
Flux.generator(generator) // Generator pushes the elements from source 1 at a time
// make call to 1st API …
Run Code Online (Sandbox Code Playgroud) spring-boot project-reactor reactive-streams spring-webflux spring-webclient