Skip to content

Instantly share code, notes, and snippets.

@rhamedy
Created September 26, 2020 02:43
Show Gist options
  • Save rhamedy/28c701fc140ca44e3e507d38eadbf053 to your computer and use it in GitHub Desktop.
Save rhamedy/28c701fc140ca44e3e507d38eadbf053 to your computer and use it in GitHub Desktop.
Unit tests generated by AI-powered Diffblue Cover plugin
package com.sampleservice.demo.controller;
import static org.mockito.AdditionalMatchers.or;
import static org.mockito.Mockito.isA;
import static org.mockito.Mockito.isNull;
import static org.mockito.Mockito.when;
import com.sampleservice.demo.dto.inbound.StudentInDTO;
import com.sampleservice.demo.model.Student;
import com.sampleservice.demo.service.StudentServiceImpl;
import java.util.ArrayList;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
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 org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
/**
* These tests are not written by a human being.
*/
@WebMvcTest(controllers = {StudentController.class})
@RunWith(SpringRunner.class)
public class StudentControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private StudentServiceImpl studentServiceImpl;
@Test
public void testGetById() throws Exception {
Student student = new Student();
student.setLastName("Doe");
student.setEmail("email");
student.setKlass(1);
student.setId(123L);
student.setFirstName("Jane");
when(this.studentServiceImpl.findById(or(isA(Long.class), isNull()))).thenReturn(student);
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/students/{sid}", 1L);
ResultActions actualPerformResult = this.mockMvc.perform(requestBuilder);
ResultActions resultActions = actualPerformResult.andExpect(MockMvcResultMatchers.status().isOk());
ResultActions resultActions1 = resultActions
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"));
Matcher<String> matcher = Matchers
.containsString("{\"id\":123,\"firstName\":\"Jane\",\"lastName\":\"Doe\",\"klass\":1}");
resultActions1.andExpect(MockMvcResultMatchers.content().string(matcher));
}
@Test
public void testList() throws Exception {
Student student = new Student();
student.setLastName("Doe");
student.setEmail("favicon.ico");
student.setKlass(0);
student.setId(123L);
student.setFirstName("Jane");
ArrayList<Student> studentList = new ArrayList<Student>();
studentList.add(student);
when(this.studentServiceImpl.list()).thenReturn(studentList);
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/students/list");
ResultActions actualPerformResult = this.mockMvc.perform(requestBuilder);
ResultActions resultActions = actualPerformResult.andExpect(MockMvcResultMatchers.status().isOk());
ResultActions resultActions1 = resultActions
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"));
Matcher<String> matcher = Matchers
.containsString("[{\"id\":123,\"firstName\":\"Jane\",\"lastName\":\"Doe\",\"klass\":0}]");
resultActions1.andExpect(MockMvcResultMatchers.content().string(matcher));
}
@Test
public void testList2() throws Exception {
when(this.studentServiceImpl.list()).thenReturn(new ArrayList<Student>());
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/students/list");
ResultActions actualPerformResult = this.mockMvc.perform(requestBuilder);
ResultActions resultActions = actualPerformResult.andExpect(MockMvcResultMatchers.status().isOk());
ResultActions resultActions1 = resultActions
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"));
Matcher<String> matcher = Matchers.containsString("[]");
resultActions1.andExpect(MockMvcResultMatchers.content().string(matcher));
}
@Test
public void testList3() throws Exception {
Student student = new Student();
student.setLastName("Doe");
student.setEmail("favicon.ico");
student.setKlass(0);
student.setId(123L);
student.setFirstName("Jane");
ArrayList<Student> studentList = new ArrayList<Student>();
studentList.add(student);
Student student1 = new Student();
student1.setLastName("Doe");
student1.setEmail("favicon.ico");
student1.setKlass(0);
student1.setId(123L);
student1.setFirstName("Jane");
studentList.add(student1);
when(this.studentServiceImpl.list()).thenReturn(studentList);
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/students/list");
ResultActions actualPerformResult = this.mockMvc.perform(requestBuilder);
ResultActions resultActions = actualPerformResult.andExpect(MockMvcResultMatchers.status().isOk());
ResultActions resultActions1 = resultActions
.andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"));
Matcher<String> matcher = Matchers.containsString(
"[{\"id\":123,\"firstName\":\"Jane\",\"lastName\":\"Doe\",\"klass\":0},{\"id\":123,\"firstName\":\"Jane\",\"lastName\":\"Doe"
+ "\",\"klass\":0}]");
resultActions1.andExpect(MockMvcResultMatchers.content().string(matcher));
}
@Test
public void testDelete() throws Exception {
Student student = new Student();
student.setLastName("Doe");
student.setEmail("email");
student.setKlass(1);
student.setId(123L);
student.setFirstName("Jane");
when(this.studentServiceImpl.findById(or(isA(Long.class), isNull()))).thenReturn(student);
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.delete("/students/{sid}", 1L);
ResultActions actualPerformResult = this.mockMvc.perform(requestBuilder);
actualPerformResult.andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void testSave() throws Exception {
MockHttpServletRequestBuilder getResult = MockMvcRequestBuilders.get("/students");
ResultActions actualPerformResult = this.mockMvc
.perform(getResult.param("dto", String.valueOf(new StudentInDTO())));
actualPerformResult.andExpect(MockMvcResultMatchers.status().is(405));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment