测试 Spring - 没有模拟的 REST API
Testing Spring - REST API without mock
如何使用 JUnit 和 Spring 调用未在 spring 或 java(其 wcf rest 服务)中编写的 rest 服务中的方法?
注意:我想做 HTTP-GET 所以模拟不是这里的情况。
Spring 是否允许我使用 JUnit 中的 restTemplate.getForObject(..)
?黄瓜?
到目前为止,我有一个使用 Spring:
编写的客户端
@SpringBootApplication
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
private static final String SERVICE_URL="http://localhost:12345/PrivilegesService/IsAlive";
public static void main(String args[]) {
SpringApplication.run(Application.class);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
boolean response = restTemplate.getForObject(SERVICE_URL, boolean.class);
log.info("response: "+ response); // print : true
};
}
}
我希望我的测试看起来像:
public class StepDefinitions {
@When("^application is up$")
public void the_client_issues_GET_version(){
}
@Then("^the server should be running$")
public void the_client_receives_status_code_of() {
boolean response = restTemplate.getForObject(SERVICE_URL, boolean.class);
AssertTrue(true,response);
}
}
RestTemplate 也适用于 Junit。不管是源码还是测试码。
REST是基于HTTP的,所以用什么框架来写REST服务并不重要。只要是 REST 服务,就应该可以调用它
如何使用 JUnit 和 Spring 调用未在 spring 或 java(其 wcf rest 服务)中编写的 rest 服务中的方法?
注意:我想做 HTTP-GET 所以模拟不是这里的情况。
Spring 是否允许我使用 JUnit 中的 restTemplate.getForObject(..)
?黄瓜?
到目前为止,我有一个使用 Spring:
编写的客户端@SpringBootApplication
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
private static final String SERVICE_URL="http://localhost:12345/PrivilegesService/IsAlive";
public static void main(String args[]) {
SpringApplication.run(Application.class);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
boolean response = restTemplate.getForObject(SERVICE_URL, boolean.class);
log.info("response: "+ response); // print : true
};
}
}
我希望我的测试看起来像:
public class StepDefinitions {
@When("^application is up$")
public void the_client_issues_GET_version(){
}
@Then("^the server should be running$")
public void the_client_receives_status_code_of() {
boolean response = restTemplate.getForObject(SERVICE_URL, boolean.class);
AssertTrue(true,response);
}
}
RestTemplate 也适用于 Junit。不管是源码还是测试码。
REST是基于HTTP的,所以用什么框架来写REST服务并不重要。只要是 REST 服务,就应该可以调用它