We’d - srujanabala/springboot-couchbase GitHub Wiki
Viewcontroller test
package org.axp.springbootcouchbase.mvc.service;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList; import java.util.List; import java.util.Optional;
import org.axp.springbootcouchbase.mvc.model.Student; import org.axp.springbootcouchbase.mvc.repository.UserRepository; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult;
import com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper;
@RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc(secure = false) @ActiveProfiles("playground") public class ViewControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserRepository userRepository;
@InjectMocks
private ViewController viewController;
List<Student> students = new ArrayList<>();
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
Student student = new Student("101", "Sapthika", 2);
students.add(student);
}
@Test
public void testAddNew() throws Exception {
doNothing().when(userRepository).deleteAll();
when(userRepository.save(Mockito.any())).thenReturn(new Student("101", "Sapthika", 2));
MvcResult result = this.mockMvc
.perform(get("/add"))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
System.out.println(result.getResponse());
verify(userRepository,times(1)).deleteAll();
verify(userRepository,times(5)).save(Mockito.any());
}
@Test
public void testFetchRecords() throws Exception {
when(userRepository.findAll()).thenReturn(students);
//userRepository.findAll().forEach(System.out::println);
MvcResult result = this.mockMvc
.perform(get("/get"))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
System.out.println(result.getResponse());
}
@Test
public void testFetchByName() throws Exception {
when(userRepository.findByName(Mockito.anyString())).thenReturn(students);
String name ="Abhishek";
MvcResult result = this.mockMvc
.perform(get("/get/"+name))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
System.out.println(result.getResponse());
}
@Test
public void testFetchByQuery() throws Exception {
when(userRepository.findByTheQuery(Mockito.anyString())).thenReturn(students);
String name ="Abhishek";
MvcResult result = this.mockMvc
.perform(get("/get/"+name))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
assertNotNull(result.getResponse().getContentAsString());
assertEquals("101", result.getResponse().getContentAsString());
//System.out.println(result.getResponse());
}
@Test
public void testDel() throws Exception {
doNothing().when(userRepository).deleteAll();
doNothing().when(userRepository).deleteById(Mockito.anyString());
MvcResult result = this.mockMvc
.perform(delete("/del"))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
System.out.println(result.getResponse());
verify(userRepository,times(1)).deleteAll();
verify(userRepository,times(1)).deleteById("101");
}
@Test
public void testIpFromHostname() throws Exception {
String ip ="127.0.0.1";
MvcResult result = this.mockMvc
.perform(get("ip-from-hostname/"+ip))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
assertNotNull(result.getResponse().getContentAsString());
}
@Test
public void testIpFromHostnameUnknownHostException() throws Exception {
String ip ="127";
MvcResult result = this.mockMvc
.perform(get("ip-from-hostname/"+ip))
.andExpect(status().isBadRequest())
.andDo(print())
.andReturn();
assertNotNull(result.getResponse().getErrorMessage());
}
@Test
public void testPost()throws Exception{
when(userRepository.save(Mockito.any())).thenReturn(new Student("101", "Sapthika", 2));
ObjectMapper mapper = new ObjectMapper();
MvcResult result = this.mockMvc
.perform(post("/students").contentType(MediaType.APPLICATION_JSON_VALUE).content(
mapper.writeValueAsString(new Student("101", "Sapthika", 2))))
.andExpect(status().isOk()).andReturn();
assertNotNull(result.getRequest());
}
@Test
public void testPut()throws Exception{
Student student =new Student("101", "Abhishek", 2);
when(userRepository.findById(Mockito.anyString())).thenReturn(Optional.ofNullable(student));
when(userRepository.save(Mockito.any())).thenReturn(new Student("101", "Abhishek", 2));
String id ="101";
MvcResult result = this.mockMvc
.perform(put("/students/"+id).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
assertNotNull(result.getRequest());
}
@Test
public void testPutDataNotFound()throws Exception{
when(userRepository.findById(Mockito.anyString())).thenReturn(Optional.ofNullable(null));
String id ="101";
MvcResult result = this.mockMvc
.perform(put("/students/"+id).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
assertNull(result.getResponse().getContentAsString());
}
}