SpringMVC Test

  1. Please initialize the log4j system properly
  2. 加入依赖包
  3. 测试类
  4. 更多

Please initialize the log4j system properly

log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

遇到这个错,把 log4j.properties 放在 classpath 下,如 srcresources 下面。

加入依赖包

<dependency>
    <groupId>com.jayway.restassured</groupId>
    <artifactId>spring-mock-mvc</artifactId>
    <version>2.4.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.2.4.Final</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.9.5</version>
    <scope>test</scope>
</dependency>

测试类

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

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration(value = "src/main/webapp") // 默认就是 src/main/webapp
@ContextConfiguration(locations = {"classpath:config/spring/spring-mvc.xml","classpath:config/spring/applicationContext.xml"})

//java.lang.AssertionError: Status expected:<200> but was:<404>  没加spring-mvc.xml
public class LogManagerActionTest2 {
    @Autowired
    private WebApplicationContext wac;
    private MockMvc mockMvc;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).dispatchOptions(true).build();
    }

    @Test
    public void test() throws Exception {
        String actual = "{\"orderNumber\": \"4955\",\"orderVersion\": \"1\"}";
        String expect = "{\"orderNumber\": \"4956\",\"orderVersion\": \"1\"}";

        ResultActions resultActions = this.mockMvc
                .perform(get("/getSystemLog")
                .accept(MediaType.TEXT_HTML_VALUE)
                // .param("actual", actual)
                // .param("expect", expect)
                ).andDo(print()) // 包含 下面的 reponse 打印结果
                .andExpect(status().isOk());

//               .andExpect(redirectedUrl("/getSystemLog.do")) // 如果返回结果是重定向,重定向的url
//               .andExpect(content().contentType("application/json"))
//               .andExpect(jsonPath("$", hasSize(1)))
//               .andExpect(jsonPath("$[0].field").value("orderNumber"))
//               .andExpect(jsonPath("$[0].actual").value("4955"))
//               .andExpect(jsonPath("$[0].expected").value("4956"));
        // 关于JsonPath的使用请参考http://goessner.net/articles/JsonPath/

        MvcResult mvcResult = resultActions.andReturn();
        System.out.println("status :\t" + mvcResult.getResponse().getStatus());
        String resposne = mvcResult.getResponse().getContentAsString();
        System.out.println("reponse :\t" + resposne);
    }
}

更多

更为全面的参考 Spring MVC测试框架详解——服务端测试


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 bin07280@qq.com

文章标题:SpringMVC Test

文章字数:605

本文作者:Bin

发布时间:2018-04-01, 21:41:33

最后更新:2019-08-06, 00:56:55

原始链接:http://coolview.github.io/2018/04/01/Spring/SpringMVC%20Test/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录