Spring ServiceDao进行Junit单元测试

Posted 支照

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring ServiceDao进行Junit单元测试相关的知识,希望对你有一定的参考价值。

pring对Controller、Service、Dao进行Junit单元测试总结

? 所有用Junit进行单元测试,都需要下面的配置

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = {"classpath:applicationContext.xml"})

? applicationContext.xml 是整个项目的Spring的配置文件。包括数据源配置、MVC配置和各种Bean的注册扫描。如果你是多个文件,就用都好隔开写多个,像这样

{ "classpath:applicationContext.xml","classpath:servlet-context.xml"}

 

1.Dao层的单元测试。

? 将*Mapper接口注入进来,直接调用即可。加上@Transactional 事物管理注解以后,单元测试执行完后会撤销对数据库的修改。想看增删改的结果的话,可以把这个注解先注释。

~~~java 

package testDao;

import com.susq.mbts.dao.UserMapper; 
import com.susq.mbts.domain.User; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/** 
* Created by susq on 2017-5-3. 
*/ 
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {“classpath:applicationContext.xml”}) 
@Transactional 
public class DaoTests { 
@Autowired 
private UserMapper userMapper;

@Test
public void testSelect() {
    User userInfo = userMapper.selectByPrimaryKey(1L);
    System.out.println(userInfo);
}


@Test
public void insert(){
    User user = new User();
// user.setId(5L); 
user.setAge(“9”); 
user.setName(“王五”); 
user.setSex(“M”); 
user.setCreateTime(new Date()); 
userMapper.insert(user); 
}

@Test
public void testUpdate() {
    User user = new User();
    user.setId(5L);
    user.setAge("999");
    user.setName("王五");
    user.setSex("M");
    user.setCreateTime(new Date());

    userMapper.updateByPrimaryKey(user);
}

@Test
public void testDelete() {
    userMapper.deleteByPrimaryKey(7L);
}
}

2. Service层的单元测试与Dao层基本一致,把Service注入进来调用就行。

~~~java 

package testDao;

import com.susq.mbts.domain.User; 
import com.susq.mbts.service.UserService; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/** 
* Created by susq on 2017-5-8. 
*/ 
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {“classpath:applicationContext.xml”}) 
public class Sertest { 
@Autowired 
private UserService userService;

@Test
public void selectUserTest() {
    User u = userService.selectUser(1);
    System.out.println(u);
}
} 

 

3. Controller层的单元测试

? 一种是把Controller的Bean注入进来,调里面的方法,这很显然比较扯淡,连Url都没经过,只测了方法。另一种是使用MockMvc模拟通过url的接口调用。MockMvc是SpringMVC提供的Controller测试类,每次进行单元测试时,都是预先执行@Before中的setup方法,初始healthArticleController单元测试环境。 
? 注意:一定要把待测试的Controller实例进行MockMvcBuilders.standaloneSetup(xxxxController).build(); 否则会抛出无法找到@RequestMapping路径的异常:No mapping found for HTTP request with URI [/cms/app/getArticleList] in DispatcherServlet

package testDao;

import com.susq.mbts.controller.UserController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;


/**
 * Created by susq on 2017-5-8.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class ConTest {
    @Autowired
    private UserController userController;

    private MockMvc mockMvc;

    @Before
    public void setup(){
        mockMvc = MockMvcBuilders.standaloneSetup(userController).build();
    }

    @Test
    public void Ctest() throws Exception {
        ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.post("/show_user3").param("id", "1"));
        MvcResult mvcResult = resultActions.andReturn();
        String result = mvcResult.getResponse().getContentAsString();
        System.out.println("=====客户端获得反馈数据:" + result);
        // 也可以从response里面取状态码,header,cookies...
//        System.out.println(mvcResult.getResponse().getStatus());
    }
}

以上是关于Spring ServiceDao进行Junit单元测试的主要内容,如果未能解决你的问题,请参考以下文章

Spring07_纯注解实战及Spring整合Junit

Spring07_纯注解实战及Spring整合Junit

单元测试

Mybatis扫描全包(包含ServiceDao的接口)导致抛出BindingException异常,引发的一系列思考(含源码分析)

为啥 Spring Boot 使用单例容器方法生成 2 个测试容器?

Spring整合Junit4进行单元测试