Spring Service、Dao進行Junit單元測試

pring對Controller、Service、Dao進行Junit單元測試總結

​ 全部用Junit進行單元測試,都須要下面的配置java

@RunWith(SpringJUnit4ClassRunner.class)

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

​ applicationContext.xml 是整個項目的Spring的配置文件。包括數據源配置、MVC配置和各類Bean的註冊掃描。若是你是多個文件,就用都好隔開寫多個,像這樣web

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

 

1.Dao層的單元測試。

​ 將*Mapper接口注入進來,直接調用便可。加上@Transactional 事物管理註解之後,單元測試執行完後會撤銷對數據庫的修改。想看增刪改的結果的話,能夠把這個註解先註釋。spring

~~~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 cookie

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 DispatcherServletmvc

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());
    }
}
相關文章
相關標籤/搜索