【发布时间】:2017-05-18 07:44:08
【问题描述】:
如何使用 JUnit 和 Spring 调用不是用 spring 或 java 编写的 rest 服务(它的 wcf 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);
}
}
【问题讨论】:
-
是什么让你认为它不允许在 Junit 中使用 RestTemplate?当然允许。 More over REST 是基于 HTTP 的,因此使用什么框架来编写 REST 服务并不重要。只要是REST服务,应该都能调用
-
谢谢,它正在工作!请把它写成答案。
-
完成。你愿意接受。
标签: java spring rest junit cucumber-jvm