Skip to content

Commit

Permalink
spring-http-interface add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
qihaiyan committed Dec 24, 2023
1 parent 591d0ee commit 4f434c5
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
package cn.springcamp.spring.httpinterface;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@Slf4j
@SpringBootApplication
public class Application implements CommandLineRunner {

@Autowired
private MyService myService;

public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Override
public void run(String... args) {
log.info("get data: {}", myService.getData("myHeader"));
log.info("get data by id: {}", myService.getData(1L));
log.info("save data: {}", myService.saveData(new MyData(1L, "test")));
log.info("delete data by id: {}", myService.deleteData(1L));
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package cn.springcamp.spring.httpinterface;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.support.RestTemplateAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
import org.springframework.web.util.DefaultUriBuilderFactory;

@Configuration
public class MyClientConfig {
@Bean
MyService myService() {
RestClient restClient = RestClient.builder().baseUrl("https://httpbin.org").build();
RestClientAdapter adapter = RestClientAdapter.create(restClient);
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}

@Bean
public MyService myService(RestTemplate restTemplate) {
restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("https://httpbin.org"));
RestTemplateAdapter adapter = RestTemplateAdapter.create(restTemplate);
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();

// RestClient restClient = RestClient.builder(restTemplate).baseUrl("https://httpbin.org").build();
// RestClientAdapter adapter = RestClientAdapter.create(restClient);
// HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();

return factory.createClient(MyService.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cn.springcamp.spring.httpinterface;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class MyController {
@Autowired
private MyService myService;

@GetMapping("/foo")
public String getData() {
return myService.getData("myHeader");
}

@GetMapping("/foo/{id}")
public String getDataById(@PathVariable Long id) {
return myService.getData(id);
}

@PostMapping("/foo")
public String saveData() {
return myService.saveData(new MyData(1L, "demo"));
}

@DeleteMapping("/foo")
public ResponseEntity<Void> deleteData() {
ResponseEntity<Void> resp = myService.deleteData(1L);
log.info("delete {}", resp);
return resp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cn.springcamp.spring.httpinterface;

import lombok.extern.slf4j.Slf4j;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.MockServerRestClientCustomizer;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.ExpectedCount;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.test.web.client.match.MockRestRequestMatchers;
import org.springframework.test.web.client.response.MockRestResponseCreators;
import org.springframework.web.client.RestTemplate;

import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DemoApplicationTest {

@Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private RestTemplate restTemplate;
@Autowired
private MyService myService;

private MockRestServiceServer mockRestServiceServer;
private MockServerRestClientCustomizer mockServerRestClientCustomizer;

@Before
public void before() {
mockRestServiceServer = MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder(true).build();
this.mockRestServiceServer.expect(ExpectedCount.manyTimes(), MockRestRequestMatchers.requestTo(Matchers.startsWithIgnoringCase("https://httpbin.org")))
.andExpect(method(HttpMethod.GET))
.andRespond(MockRestResponseCreators.withSuccess("{\"get\": 200}", MediaType.APPLICATION_JSON));
this.mockRestServiceServer.expect(ExpectedCount.manyTimes(), MockRestRequestMatchers.requestTo(Matchers.startsWithIgnoringCase("https://httpbin.org")))
.andExpect(method(HttpMethod.POST))
.andRespond(MockRestResponseCreators.withSuccess("{\"post\": 200}", MediaType.APPLICATION_JSON));
this.mockRestServiceServer.expect(ExpectedCount.manyTimes(), MockRestRequestMatchers.requestTo(Matchers.startsWithIgnoringCase("https://httpbin.org")))
.andExpect(method(HttpMethod.DELETE))
.andRespond(MockRestResponseCreators.withSuccess("{\"delete\": 200}", MediaType.APPLICATION_JSON));
}

@Test
public void testRemoteCallRest() {
log.info("testRemoteCallRest get {}", testRestTemplate.getForObject("/foo", String.class));
log.info("testRemoteCallRest getById {}", testRestTemplate.getForObject("/foo/1", String.class));
log.info("testRemoteCallRest post {}", testRestTemplate.postForObject("/foo", new MyData(1L, "demo"), String.class));
testRestTemplate.exchange("/foo", HttpMethod.DELETE, HttpEntity.EMPTY, String.class);
}
}

0 comments on commit 4f434c5

Please sign in to comment.