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.AdLikeController; import controllers.AdListController; import java.sql.SQLException; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; 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.MockHttpServletRequest; 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 AdListControllerTest { @InjectMocks AdListController controller; private MockHttpServletRequest request; private ModelMap map; @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); map = new ModelMap(); request = new MockHttpServletRequest(); } @Test public void viewAdListTest() throws SQLException { String viewName = controller.showForm(null, null, null, map, request); assertEquals(viewName, "adlist"); assertTrue(map.containsAttribute("ads")); assertFalse(map.containsAttribute("user")); controller.showForm(null, null, "TestUser", map, request); assertTrue(map.containsAttribute("user")); } }