Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. package com.lwasiele.repositories.controller;
  2.  
  3. import com.lwasiele.repositories.model.RepositoryDetailsDTO;
  4. import com.lwasiele.repositories.service.api.RepositoryService;
  5. import net.minidev.json.JSONObject;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
  10. import org.springframework.boot.test.mock.mockito.MockBean;
  11. import org.springframework.test.context.junit4.SpringRunner;
  12. import org.springframework.test.web.servlet.MockMvc;
  13.  
  14. import static org.mockito.ArgumentMatchers.anyString;
  15. import static org.mockito.Mockito.when;
  16. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  17. import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
  18. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  19. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  20.  
  21. @RunWith(SpringRunner.class)
  22. @WebMvcTest(RepositoriesController.class)
  23. public class RepositoriesControllerTest {
  24.  
  25. public static final String REPOSITORY_ADDRESS = "/repositories/test/test";
  26.  
  27. @Autowired
  28. private MockMvc mockMvc;
  29.  
  30. @MockBean
  31. private RepositoryService repositoryService;
  32.  
  33.  
  34. @Test
  35. public void shouldGetRepositoryData() throws Exception {
  36. //given
  37. RepositoryDetailsDTO repositoryDetailsDTO = new RepositoryDetailsDTO();
  38. //when
  39. when(repositoryService.getRepositoryDetails(anyString(), anyString())).thenReturn(repositoryDetailsDTO);
  40. //then
  41. this.mockMvc.perform(get(REPOSITORY_ADDRESS)).andDo(print()).andExpect(status().isOk())
  42. .andExpect(content().json(prepareEmptyJsonObject().toJSONString()));
  43.  
  44. }
  45.  
  46. private JSONObject prepareEmptyJsonObject() {
  47. JSONObject jsonResponse = new JSONObject();
  48. jsonResponse.put("full_name", null);
  49. jsonResponse.put("description", null);
  50. jsonResponse.put("clone_url", null);
  51. jsonResponse.put("stargazers_count", null);
  52. jsonResponse.put("created_at", null);
  53. return jsonResponse;
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement