使用模拟进行 Spring Controller 测试

标签 spring unit-testing testing controller

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

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

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

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

最佳答案

然后你要做的只是断言值,例如:

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

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

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

在您触发 GET 请求之前。

关于使用模拟进行 Spring Controller 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30533708/

相关文章:

java - 不使用 ResponseEntity 时 Spring Controller 不返回值

unit-testing - 如何测试CLI标志-当前因“标志重新定义”而失败

Java 单元测试 : the easiest way to test if a callback is invoked

java - Liferay + Vaadin 的 Selenium gui 测试

java - SQLException : ResultSet is from UPDATE. 无数据。 Spring 数据

java - 无法解析配置类,Spring

Spring Data Rest 发布到收集端点

unit-testing - 使用 jasmine 进行 Angular2 异步单元测试

python - 为具有多对多关系的 Django 模型编写测试

web-services - 自动化测试网络服务