Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ControllersTest; import controllers.HomeController; import java.sql.SQLException; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertTrue; import org.junit.Before; import org.junit.Test; import org.mockito.InjectMocks; import org.mockito.MockitoAnnotations; import org.springframework.mock.web.MockHttpSession; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.ui.ModelMap; @WebAppConfiguration @ContextConfiguration("servlet-context.xml") public class HomeControllerTest { @InjectMocks HomeController controller; private MockHttpSession session; @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); } @Test public void homeControllerTest() throws Exception { session = new MockHttpSession(); ModelMap modelMap = new ModelMap(); String viewName = controller.showForm(modelMap, session); assertEquals("index", viewName); String theme = session.getAttribute("theme").toString(); assertEquals("bright", theme); assertTrue(modelMap.containsAttribute("tags")); assertTrue(modelMap.containsAttribute("topUsers")); assertTrue(modelMap.containsAttribute("topAds")); } @Test public void changeThemeTest() throws SQLException { session = new MockHttpSession(); session.setAttribute("theme", "bright"); String viewName = controller.changeTheme(session); assertEquals("redirect:index.htm", viewName); assertEquals(session.getAttribute("theme").toString(), "dark"); session.setAttribute("theme", "dark"); controller.changeTheme(session); assertEquals(session.getAttribute("theme").toString(), "bright"); } }