ControllersTest.AddControllerTest.java Source code

Java tutorial

Introduction

Here is the source code for ControllersTest.AddControllerTest.java

Source

/*
 * 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.AdController;
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.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 AddControllerTest {

    private MockHttpSession session;
    private ModelMap map;
    private MockHttpServletRequest request;

    @InjectMocks
    AdController controller;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        session = new MockHttpSession();
        map = new ModelMap();
        request = new MockHttpServletRequest();
    }

    @Test
    public void viewAdTest() throws SQLException {
        String viewName = controller.showForm(map, session, request);
        assertEquals(viewName, "error");

        request.addParameter("ad_id", "51");
        viewName = controller.showForm(map, session, request);
        assertEquals(viewName, "ad");
        assertTrue(map.containsAttribute("ad"));
        assertTrue(map.containsAttribute("tags"));
        assertTrue(map.containsAttribute("comments"));
        assertTrue(map.containsAttribute("photos"));

    }
}