Skip to content

Instantly share code, notes, and snippets.

@MakotoTheKnight
Last active June 28, 2018 03:05
Show Gist options
  • Save MakotoTheKnight/2e33490793ddc77d9e82a3e77e34f6a1 to your computer and use it in GitHub Desktop.
Save MakotoTheKnight/2e33490793ddc77d9e82a3e77e34f6a1 to your computer and use it in GitHub Desktop.
package com.makototheknight.springbootdozer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class FooService {
private final RestTemplate restTemplate;
@Autowired
public FooService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String getResponseFromStackOverflow() {
return restTemplate.getForObject("https://www.stackoverflow.com", String.class);
}
}
package com.makototheknight.springbootdozer;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Before;
import org.junit.Rule;
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.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
@RunWith(SpringRunner.class)
@SpringBootTest
public class NetworkMockTests {
@Autowired
private FooService fooService;
@Autowired
private RestTemplate restTemplate;
private MockRestServiceServer mockServer;
@Before
public void setup() {
mockServer = MockRestServiceServer.createServer(restTemplate);
}
@Test
public void callOutToStackOverflow() {
mockServer.expect(requestTo("https://www.stackoverflow.com"))
.andExpect(method(HttpMethod.GET))
.andRespond(withSuccess("hmm", MediaType.APPLICATION_JSON));
System.out.println(fooService.getResponseFromStackOverflow());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment