0
如何使用JUnit & Spring来调用不是在spring或java(其wcf rest服务)中编写的rest服务中的方法?测试Spring - 没有模拟的REST API
注意:我想做HTTP-GET,所以在这里嘲笑不是这种情况。
春让我使用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?当然它允许。 REST的基础是HTTP,因此使用什么框架编写REST服务并不重要。只要它是一个REST服务,你应该可以调用它 – pvpkiran
谢谢,它的工作!请把它写成答案。 –
<@Stav>完成。你想接受它。 – pvpkiran