【问题标题】:Spring controller tests with mocks使用 mocks 进行 Spring 控制器测试
【发布时间】:2015-05-29 15:40:59
【问题描述】:

所以我在想出一个测试解决方案时遇到了一些问题。

这是我要测试的方法(我是新手)。每次加载时它都会清除网页上的所有字段。

@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";
}

这是我目前的测试结果

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;
    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));
}

因为这是我第一次不得不做这种事情,所以我不知道该去哪里。

【问题讨论】:

    标签: spring unit-testing testing controller


    【解决方案1】:

    那么你要做的只是断言值,例如:

    assertNull(mockSession.getAttribute("firstname"));
    

    如果你想确定是这样,你可以这样做:

    assertNotNull(mockSession.getAttribute("firstname"));
    

    在您触发 GET 请求之前。

    【讨论】:

    • 一旦我这样做了,测试就会失败,因为它仍然返回“test”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 2019-07-07
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多