Skip to content
Permalink
Browse files

How to mock third party REST web service call

  • Loading branch information...
slem1 committed Oct 27, 2017
1 parent c64058e commit c2e78f20ded7ff434259aaa0073d473397b5fdec
@@ -24,6 +24,11 @@
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
@@ -3,8 +3,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
* @author slemoine
@@ -16,11 +18,18 @@
@Autowired
private MyEntityJpaRepository myEntityJpaRepository;

@Autowired
private RestTemplate restTemplate;

public void createnewMyEntity() {
myEntityJpaRepository.save(new MyEntity());
}

public List<MyEntity> getAllMyEntity() {
return myEntityJpaRepository.findAll();
}

public Map<String,String> getExternalData(){
return restTemplate.getForObject("http://mythirdpartyhost/rest/api/data", Map.class);
}
}
@@ -10,11 +10,17 @@
import org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestEntityManager;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit4.SpringRunner;
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.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
* @author slemoine
@@ -31,6 +37,9 @@
@Autowired
private MyEntityService myEntityService;

@Autowired
private RestTemplate restTemplate;

@Test
public void saveMyEntityShouldCreateNewEntity() {
testEntityManager.persistAndGetId(new MyEntity());
@@ -42,4 +51,17 @@ public void injectedSqlTestMethod() {
List<MyEntity> allMyEntity = myEntityService.getAllMyEntity();
Assert.assertEquals(1, allMyEntity.size());
}

@Test
public void getExternalData(){

MockRestServiceServer mockRestServiceServer = MockRestServiceServer.createServer(restTemplate);

mockRestServiceServer.expect(MockRestRequestMatchers.requestTo("http://mythirdpartyhost/rest/api/data"))
.andRespond(MockRestResponseCreators.withSuccess("{ \"key\": \"value\" }", MediaType.APPLICATION_JSON));

Map<String, String> externalData = myEntityService.getExternalData();

Assert.assertEquals("value",externalData.get("key"));
}
}
@@ -4,6 +4,7 @@
import org.mockito.Mockito;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
* @author slemoine
@@ -24,4 +25,9 @@ public AdditionnalTestComponent additionnalTestComponent() {
public MyComponent component() {
return Mockito.mock(MyComponent.class);
}

@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
@@ -1,6 +1,5 @@
package fr.sle.testhowto.rest.facade;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -15,7 +14,7 @@
public static final String ROOT_RESOURCE = "/myrestfacade";

@GetMapping
public String getAString(){
public String getAString() {
return "Hello !";
}

0 comments on commit c2e78f2

Please sign in to comment.
You can’t perform that action at this time.