Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.lwasiele.repositories.controller;
- import com.lwasiele.repositories.model.RepositoryDetailsDTO;
- import com.lwasiele.repositories.service.api.RepositoryService;
- import net.minidev.json.JSONObject;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
- import org.springframework.boot.test.mock.mockito.MockBean;
- import org.springframework.test.context.junit4.SpringRunner;
- import org.springframework.test.web.servlet.MockMvc;
- import static org.mockito.ArgumentMatchers.anyString;
- import static org.mockito.Mockito.when;
- import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
- import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
- @RunWith(SpringRunner.class)
- @WebMvcTest(RepositoriesController.class)
- public class RepositoriesControllerTest {
- public static final String REPOSITORY_ADDRESS = "/repositories/test/test";
- @Autowired
- private MockMvc mockMvc;
- @MockBean
- private RepositoryService repositoryService;
- @Test
- public void shouldGetRepositoryData() throws Exception {
- //given
- RepositoryDetailsDTO repositoryDetailsDTO = new RepositoryDetailsDTO();
- //when
- when(repositoryService.getRepositoryDetails(anyString(), anyString())).thenReturn(repositoryDetailsDTO);
- //then
- this.mockMvc.perform(get(REPOSITORY_ADDRESS)).andDo(print()).andExpect(status().isOk())
- .andExpect(content().json(prepareEmptyJsonObject().toJSONString()));
- }
- private JSONObject prepareEmptyJsonObject() {
- JSONObject jsonResponse = new JSONObject();
- jsonResponse.put("full_name", null);
- jsonResponse.put("description", null);
- jsonResponse.put("clone_url", null);
- jsonResponse.put("stargazers_count", null);
- jsonResponse.put("created_at", null);
- return jsonResponse;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement