• spring 4.0 JUnit简单的Controller测试


    比Dao和Service的测试稍微复杂一点。还是先写一个BasicWebTest用来总体配置:

    @WebAppConfiguration
    @ContextConfiguration(locations= {"classpath:spring/applicationContext.xml","classpath:spring/spring-servlet.xml"})
    public class BasicWebTest extends AbstractTransactionalJUnit4SpringContextTests {
    
        @Autowired
        protected WebApplicationContext wac;
        
        protected MockMvc mvc;
        
        @Before
        public void setUp() {
            this.mvc=MockMvcBuilders.webAppContextSetup(wac).build();
        }
    }
    BasicWebTest 上面多了个 @WebAppConfiguration 注解,并且类中也多了一些方法、字段。直接复制吧。
    测试用例如下:

    package com.suyin.controller;
    
    import java.util.List;
    
    import org.hamcrest.Description;
    import org.hamcrest.Matcher;
    import org.junit.Test;
    import org.springframework.mock.web.MockHttpServletRequest;
    import org.springframework.mock.web.MockHttpServletResponse;
    import org.springframework.test.web.servlet.MvcResult;
    import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
    
    import com.jayway.jsonpath.JsonPath;
    import com.suyin.BasicWebTest;
    import com.suyin.pojo.People;
    @SuppressWarnings("unchecked")
    public class PeopleControllerTest extends BasicWebTest {
    
        @Test
        public void queryAll() throws Exception {
            mvc.perform(MockMvcRequestBuilders.get("/people/queryAll"))
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andExpect(MockMvcResultMatchers.view().name("people/list"))
                    .andExpect(MockMvcResultMatchers.request().attribute("list", new MatcherAdapter() {
    
                        @Override
                        public void describeMismatch(Object item, Description mismatchDescription) {
                            mismatchDescription.appendText("wrong list size");
                        }
    
                        @Override
                        public boolean matches(Object item) { 
                            List<People> list=(List<People>) item;
                            return list.size()==4;
                        }
                        
                    }));
        }
        @Test
        public void asynQueryAll() throws Exception {
            MvcResult mr=mvc.perform(MockMvcRequestBuilders.get("/people/asynQueryAll").content("{"id":1}"))
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andExpect(MockMvcResultMatchers.jsonPath("$.id").exists())
                    .andReturn();
            
            MockHttpServletResponse response=mr.getResponse();
            MockHttpServletRequest request=mr.getRequest();
        }
        
        @Test
        public void update1() throws Exception {
            mvc.perform(MockMvcRequestBuilders.post("/people/update").param("id", "29")
                    .param("name", "Jack")
                    .param("address", "japan")
                    .param("age", "19"))
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andExpect(MockMvcResultMatchers.view().name("people/detail"));
                    
        }
        @Test
        public void update2() throws Exception {
            
            
            mvc.perform(MockMvcRequestBuilders.post("/people/update")
                    //.param("name", "Jack")
                    .param("address", "japan")
                    .param("age", "19"))
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andExpect(MockMvcResultMatchers.view().name("error"));
        }
        
        
        @Test
        public void add1()throws Exception  {
            mvc.perform(MockMvcRequestBuilders.post("/people/add")
                    .param("name", "JJJJ")
                    .param("address", "")
                    .param("age", "1")
                    )
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andExpect(MockMvcResultMatchers.view().name("people/detail"));
        }
        @Test
        public void add2()throws Exception  {
            mvc.perform(MockMvcRequestBuilders.post("/people/add")
                    .param("address", "")
                    .param("age", "1")
                    )
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andExpect(MockMvcResultMatchers.view().name("error"));
        }
        
        
        
        
    
        abstract class MatcherAdapter<T> implements Matcher<T>{
    
            @Override
            public void describeTo(Description arg0) {
                // TODO Auto-generated method stub
                
            }
    
            @Override
            public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
                // TODO Auto-generated method stub
                
            }
    
            @Override
            public abstract void describeMismatch(Object item,
                    Description mismatchDescription);
    
            @Override
            public abstract boolean matches(Object item) ;
            
        }
    }
    View Code

    认真说来关于Controller的Spring测试就这样了。在多就是关于MockMvc的API学习了。

  • 相关阅读:
    3、Semantic-UI之定义容器
    2、Semantic-UI之网格布局
    1、Semantic-UI之开发环境搭建
    PyCharm创建普通项目配置支持jinja2语法
    CentOS 7安装GitLab、汉化、配置邮件发送
    Docker swarm 实战-部署wordpress
    线程和进程
    NGINX内置变量
    iTerm2 + Oh My Zsh 打造舒适终端体验
    CentOS 7修改网卡名称
  • 原文地址:https://www.cnblogs.com/formyjava/p/4595337.html
Copyright © 2020-2023  润新知