Unit testing questions how does spring framework make

Info icon This preview shows pages 199–212. Sign up to view the full content.

View Full Document Right Arrow Icon
Unit Testing
Image of page 199

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Questions How does Spring Framework Make Unit Testing Easy? What is Mockito? What is your favorite mocking framework? How do you do mock data with Mockito? What are the different mocking annotations that you worked with?
Image of page 200
public class SomeBusinessImpl { private DataService dataService; //Constructor - public SomeBusinessImpl(DataService dataServ int findTheGreatestFromAllData () { int [] data = dataService.retrieveAllData(); int greatest = Integer.MIN_VALUE; for ( int value : data) { if (value > greatest) { greatest = value; } } return greatest; } }
Image of page 201

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Basic Mocking @Test public void testFindTheGreatestFromAllData () { DataService dataServiceMock = mock(DataService.class); when(dataServiceMock.retrieveAllData()) .thenReturn( new int [] { 24, 15, 3 }); SomeBusinessImpl businessImpl = new SomeBusinessImpl(dataServiceMock); int result = businessImpl.findTheGreatestFromAllData(); assertEquals(24, result); }
Image of page 202
Using Annotations @RunWith (MockitoJUnitRunner.class) public class SomeBusinessMockAnnotationsTest { @Mock DataService dataServiceMock; @InjectMocks SomeBusinessImpl businessImpl; @Test public void testFindTheGreatestFromAllData () { when(dataServiceMock.retrieveAllData()) .thenReturn( new int [] { 24, 15, 3 }); assertEquals(24, businessImpl.findTheGreatestFromAllData() }
Image of page 203

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Questions What is MockMvc? What is @WebMvcTest? What is @MockBean? How do you write a unit test with MockMVC? What is JSONAssert?
Image of page 204
Mock MVC Test with Spring Boot public Question retrieveDetailsForQuestion ( @PathVariable String surveyId, @PathVariable String questionId) { return surveyService.retrieveQuestion(surveyId, questionId); }
Image of page 205

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Mock MVC Test with Spring Boot @RunWith (SpringRunner.class) @WebMvcTest (value = SurveyController.class, secure = false ) public class SurveyControllerTest { @Autowired private MockMvc mockMvc; @MockBean private SurveyService surveyService;
Image of page 206
Mock MVC Test - Continued @Test public void retrieveDetailsForQuestion () throws Exception { Question mockQuestion = new Question( "Question1" , "Largest Country in the World" , "Russia" , Arrays.asList( "India" , "Russia" , "United States" , "China" )); Mockito.when( surveyService.retrieveQuestion(Mockito.anyString(), Mock .anyString())).thenReturn(mockQuestion); RequestBuilder requestBuilder = MockMvcRequestBuilders.get( "/surveys/Survey1/questions/Question1" ).accept( MediaType.APPLICATION_JSON);
Image of page 207

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Mock MVC Test - Continued MvcResult result = mockMvc.perform(requestBuilder) .andReturn(); String expected = "{id:Question1,description:Largest Country in the World,co JSONAssert.assertEquals(expected, result.getResponse() .getContentAsString(), false ); // Assert }
Image of page 208
Questions How do you write an integration test with Spring Boot? What is @SpringBootTest? What is @LocalServerPort? What is TestRestTemplate?
Image of page 209

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Integration Test with Spring Boot @RunWith (SpringRunner.class) @SpringBootTest (classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT public class SurveyControllerIT { @LocalServerPort private int port; }
Image of page 210
Integration Test with Spring Boot @Test public void testRetrieveSurveyQuestion () { String url = " :" + port + "/surveys/Survey1/questions/Question1" ; HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)) HttpEntity<String> entity = new HttpEntity<String>( null , headers); TestRestTemplate restTemplate = new
Image of page 211

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 212
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern