From 2f8dc9d081eba3539e9572864dd7540d0d1d2c7c Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Wed, 20 Jul 2016 07:59:33 +0300 Subject: [PATCH] Add additional expectations --- .../test/DataContentControllerTest.java | 39 +++++++++++-------- .../test/NavigationControllerTest.java | 24 ++++++------ 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java index d7f8411325..36453eef92 100644 --- a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java +++ b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java @@ -1,17 +1,8 @@ package com.baeldung.mvc.velocity.test; -import static org.hamcrest.Matchers.allOf; -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.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 com.baeldung.mvc.velocity.domain.Tutorial; +import com.baeldung.mvc.velocity.service.ITutorialsService; +import com.baeldung.mvc.velocity.service.TutorialsService; import org.junit.Before; import org.junit.Test; 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.web.context.WebApplicationContext; -import com.baeldung.mvc.velocity.domain.Tutorial; -import com.baeldung.mvc.velocity.service.ITutorialsService; -import com.baeldung.mvc.velocity.service.TutorialsService; +import java.util.Arrays; + +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) @ContextConfiguration(locations = {"classpath:mvc-servlet.xml"}) @@ -52,8 +53,7 @@ public class DataContentControllerTest { @Test public void testModel() throws Exception{ - - + Mockito.when(tutServiceMock.listTutorials()).thenReturn(Arrays.asList( new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"), new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor") @@ -62,6 +62,11 @@ public class DataContentControllerTest { mockMvc.perform(get("/")) .andExpect(status().isOk()) .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", hasItem( allOf( diff --git a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java index 77cd2feadd..e007cf3f94 100644 --- a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java +++ b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java @@ -1,15 +1,9 @@ package com.baeldung.mvc.velocity.test; -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; - -import java.util.Arrays; -import java.util.List; - +import com.baeldung.mvc.velocity.controller.MainController; +import com.baeldung.mvc.velocity.domain.Tutorial; +import com.baeldung.mvc.velocity.service.TutorialsService; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; @@ -19,10 +13,14 @@ import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.Model; -import com.baeldung.mvc.velocity.controller.MainController; -import com.baeldung.mvc.velocity.domain.Tutorial; -import com.baeldung.mvc.velocity.service.TutorialsService; +import java.util.Arrays; +import java.util.List; +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) @WebAppConfiguration @@ -61,7 +59,7 @@ public class NavigationControllerTest { verifyNoMoreInteractions(mainController.getTutService()); assertEquals("index", view); - assertEquals(tutorials, model.asMap().get("tutorials")); + assertEquals(tutorials, model.asMap().get("tutorials")); } private static List createTutorialList() {