vendredi 29 mai 2015

Spring controller tests with mocks

So i'm having some issues coming up with a solution for a test, here's what i have so far.

This is the method i want to test(i'm new to this) this clears all fields on a web page each time it's loaded.

@RequestMapping("/addaddressform")
public String addAddressForm(HttpSession session)
{
    session.removeAttribute("firstname");
    session.removeAttribute("surname");
    session.removeAttribute("phone");
    session.removeAttribute("workno");
    session.removeAttribute("homeno");
    session.removeAttribute("address");
    session.removeAttribute("email");

    return "simpleforms/addContact";
}

and here's what i have so far for the test package ControllerTests;

import java.text.AttributedCharacterIterator.Attribute;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
   import                   org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
   import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
   import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
   import org.springframework.test.web.servlet.setup.MockMvcBuilders;
   import org.springframework.web.context.WebApplicationContext;

import static     org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


   @RunWith(SpringJUnit4ClassRunner.class)
   @WebAppConfiguration
   @ContextConfiguration (classes = {SimpleFormsControllerTest.class})

public class SimpleFormsControllerTest {
  @Autowired
  private WebApplicationContext wac;
enter code here
  private MockMvc mockMvc;

  @Before
  public void setup() {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
  }

    @Test
    public void addAddressForm_ExistingContactDetailsInForm_RemovalFromSession()          throws     Exception{

    MockHttpSession mockSession = new MockHttpSession();

    mockSession.putValue("firstname", "test");
    mockSession.putValue("surname", "test");
    mockSession.putValue("phone", "test");
    mockSession.putValue("workno", "test");
    mockSession.putValue("homeno", "test");
    mockSession.putValue("address", "test");
    mockSession.putValue("email", "test");

                  mockMvc.perform(get("simpleForms/addaddressform").session(mockSession));

}

as this is the first time i've ever had to do this kind of thing i really don't have much clue where to go with this.

Aucun commentaire:

Enregistrer un commentaire