Add additional expectations

This commit is contained in:
Grzegorz Piwowarek 2016-07-20 07:59:33 +03:00
parent f4348cac35
commit 2f8dc9d081
2 changed files with 33 additions and 30 deletions

View File

@ -1,17 +1,8 @@
package com.baeldung.mvc.velocity.test; package com.baeldung.mvc.velocity.test;
import static org.hamcrest.Matchers.allOf; import com.baeldung.mvc.velocity.domain.Tutorial;
import static org.hamcrest.Matchers.hasItem; import com.baeldung.mvc.velocity.service.ITutorialsService;
import static org.hamcrest.Matchers.hasProperty; import com.baeldung.mvc.velocity.service.TutorialsService;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import java.util.Arrays;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -24,9 +15,19 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import com.baeldung.mvc.velocity.domain.Tutorial; import java.util.Arrays;
import com.baeldung.mvc.velocity.service.ITutorialsService;
import com.baeldung.mvc.velocity.service.TutorialsService; import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"}) @ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
@ -53,7 +54,6 @@ public class DataContentControllerTest {
@Test @Test
public void testModel() throws Exception{ public void testModel() throws Exception{
Mockito.when(tutServiceMock.listTutorials()).thenReturn(Arrays.asList( Mockito.when(tutServiceMock.listTutorials()).thenReturn(Arrays.asList(
new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"), new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"),
new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor") new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor")
@ -62,6 +62,11 @@ public class DataContentControllerTest {
mockMvc.perform(get("/")) mockMvc.perform(get("/"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(view().name("index")) .andExpect(view().name("index"))
.andExpect(content().string(containsString("GuavaAuthor")))
.andExpect(content().string(containsString("Introduction to Guava")))
.andExpect(content().string(containsString("AndroidAuthor")))
.andExpect(content().string(containsString("Introduction to Android")))
.andExpect(model().attribute("tutorials", hasSize(2)))
.andExpect(model().attribute("tutorials", hasSize(2))) .andExpect(model().attribute("tutorials", hasSize(2)))
.andExpect(model().attribute("tutorials", hasItem( .andExpect(model().attribute("tutorials", hasItem(
allOf( allOf(

View File

@ -1,15 +1,9 @@
package com.baeldung.mvc.velocity.test; package com.baeldung.mvc.velocity.test;
import static org.junit.Assert.assertEquals; import com.baeldung.mvc.velocity.controller.MainController;
import static org.junit.Assert.assertNotNull; import com.baeldung.mvc.velocity.domain.Tutorial;
import static org.mockito.Mockito.times; import com.baeldung.mvc.velocity.service.TutorialsService;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import java.util.Arrays;
import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -19,10 +13,14 @@ import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import com.baeldung.mvc.velocity.controller.MainController; import java.util.Arrays;
import com.baeldung.mvc.velocity.domain.Tutorial; import java.util.List;
import com.baeldung.mvc.velocity.service.TutorialsService;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration