Module testing with jUnit and Mockito for external REST API calls

I am building unit tests in a Spring Boot Java application for a service class.

The service class makes an external call to the REST API service, which returns a JSON response. I mock this call using Mockito. I am hard-coded JSON in a mockserver answer.

Is this bad practice having hardcoded JSON in your unit tests? If the JSON structure changes, then the test should fail, these are my considerations. Is there a best, best practice where to do this?

Example snippet below:

The actual code is functional, I just edited this snippet for brevity to get this idea, so post a comment if you see any errors:

public class UserServiceTest extends TestCase { private static final String MOCK_URL = "baseUrl"; private static final String DEFAULT_USER_ID = "353"; UserService classUnderTest; ResponseEntity<Customer> mockResponseEntity; MockRestServiceServer mockServer; @Mock RestTemplate mockRestTemplate; public void setUp() throws Exception { super.setUp(); classUnderTest = new UserRestService(); mockRestTemplate = new RestTemplate(); mockServer = MockRestServiceServer.createServer(mockRestTemplate); ReflectionTestUtils.setField(classUnderTest, "restTemplate", mockRestTemplate); ReflectionTestUtils.setField(classUnderTest, "userSvcUrl", MOCK_URL); } public void testGetUserById() throws Exception { mockServer.expect(requestTo(MOCK_URL + "/Users/" + DEFAULT_USER_ID)).andExpect(method(HttpMethod.GET)) .andRespond(withSuccess( "{\n" + " \"UserCredentials\": {\n" + " \"Password\": \"\",\n" + " \"PasswordNeedsUpdate\": false,\n" + " \"Security\": [\n" + " {\n" + " \"Answer\": \"\",\n" + " \"Question\": \"Why did the bicycle fall over?\"\n" + " }\n" + " ]\n" + "}" , MediaType.APPLICATION_JSON)); Customer customer = classUnderTest.getUserById(DEFAULT_USER_ID); mockServer.verify(); assertNotNull(customer); assertEquals(DEFAULT_USER_ID, customer.getId()); } } 
+6
source share
2 answers

I am currently in the same boat as you, and my reasoning was as follows: Creating a dummy JSON response is like mocking an object and managing it with Mockito.when . You will need to change something in the when().thenReturn() call when().thenReturn() you change some kind of internal parsing or you expect different results. The same thing happens with a JSON response when calls change and object representations change.

Therefore, I assume that everything is in order. While reading various articles on testing REST APIs, the general consensus is that creating dummy JSON responses is good practice. It would be best practice (from time to time) to download a real JSON response and embed this as a mocked reaction. Thus, you can keep your tests up to date with the outside, while tests can be performed without Internet requests.

Change as requested:

+7
source

I would like to highlight another approach to the consideration, namely to create a POJO (for example, a JavBean data model) of an object that JSON represents, and then use a JSON serializer, such as Gson from Google, to convert it to a JSON string.

 UserCredentials uc = new UserCredentials (); //set the values Gson gson = new GsonBuilder().setPrettyPrinting().create(); String json = gson.toJson(uc); 
+4
source

Source: https://habr.com/ru/post/987211/


All Articles