原文链接:http://blog.csdn.net/u013041642/article/details/71430293
Spring对Controller、Service、Dao进行Junit单元测试总结
Spring对Controller、Service、Dao进行Junit单元测试总结
所有用Junit进行单元测试,都需要下面的配置
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
- 1
- 2
- 3
applicationContext.xml 是整个项目的Spring的配置文件。包括数据源配置、MVC配置和各种Bean的注册扫描。如果你是多个文件,就用都好隔开写多个,像这样
{ "classpath:applicationContext.xml","classpath:servlet-context.xml"}
- 1
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();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
// 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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
}
~~~
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);
}
- 1
- 2
- 3
- 4
- 5
- 6
}
~~~
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());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42