SpringMVC下的JUnit4單元測(cè)試

簡(jiǎn)述

在spingMVC框架中,所有的類都是通過(guò)springMVC容器來(lái)加載和實(shí)例化的。
因此在junit測(cè)試的時(shí)候可以在其初始化方法中去加載spring配置文件。所有用Junit進(jìn)行單元測(cè)試,都需要下面的配置。注:配置文件的加載根據(jù)自己項(xiàng)目的實(shí)際情況進(jìn)行配置。

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:config/spring-mvc.xml",
        "classpath:config/spring-resources.xml",
        "classpath:config/spring-application.xml",
        "classpath:config/spring-security.xml" })

Dao層的單元測(cè)試

通過(guò)依賴注入的方式注入測(cè)試對(duì)象;

package com.lczyfz.zerobdt.test;

import com.lczyfz.zerobdt.modules.test.dao.TestDao;
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 static junit.framework.TestCase.assertEquals;

/**
 * Created by maple on 2018-07-01.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@ContextConfiguration(locations = {"/spring-context.xml"})
public class TestDaoTests {
    @Autowired
    TestDao testDao;

    @Test
    public void testFindAllList(){
        System.out.println(testDao.findAllList(new com.lczyfz.zerobdt.modules.test.entity.Test()).toString());
        assertEquals(testDao.findAllList(new com.lczyfz.zerobdt.modules.test.entity.Test()).size(),13);
    }
}

成功:

失?。?/p>

Service層的單元測(cè)試

和Dao層類似;

package com.lczyfz.zerobdt.test;

import com.lczyfz.zerobdt.modules.test.service.TestService;
import junit.framework.TestCase;
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 static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.*;

/**
 * Created by maple on 2018-07-01.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring-context.xml"})
public class TestDaoServices {
    @Autowired
    TestService testService;

    @Test
    public void testFindAllList(){
        System.out.println(testService.get("5").toString());
        assertNotNull(testService.get("5"));
    }
}

Controller層的單元測(cè)試

package com.lczyfz.zerobdt.test;

import com.lczyfz.zerobdt.common.test.SpringTransactionalContextTests;
import com.lczyfz.zerobdt.modules.test.web.TestController;
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;

import java.io.UnsupportedEncodingException;

import static org.junit.Assert.assertNotNull;

/**
 * Created by maple on 2018-07-01.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring-context.xml","/spring-mvc.xml"})
public class TestDaoControllers extends SpringTransactionalContextTests{
    @Autowired
    TestController testController;

    MockMvc mockMvc;


    @Before
    public void setup(){
        mockMvc = MockMvcBuilders.standaloneSetup(testController).build();
    }
    @Test
    public void testFindAllList(){
        ResultActions resultActions = null;
        try {
            resultActions = this.mockMvc.perform(MockMvcRequestBuilders.post("/show_user").param("id", "1"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        MvcResult mvcResult = resultActions.andReturn();
        String result = null;
        try {
            result = mvcResult.getResponse().getContentAsString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println("=====客戶端獲得反饋數(shù)據(jù):" + result);
        // 也可以從response里面取狀態(tài)碼,header,cookies...
//        System.out.println(mvcResult.getResponse().getStatus());

    }
}

Controller層的測(cè)試建議采用Postman的方式來(lái)測(cè)試,可以記錄url,同時(shí)也更符合實(shí)際情況。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,688評(píng)論 19 139
  • 1、談?wù)勀銓?duì)Struts的理解。 答: 1.struts是一個(gè)按MVC模式設(shè)計(jì)的Web層框架,其實(shí)它就是一個(gè)大大的...
    慕容小偉閱讀 2,975評(píng)論 0 13
  • 轉(zhuǎn)自:http://blog.csdn.net/jackfrued/article/details/4493116...
    王帥199207閱讀 2,774評(píng)論 0 19
  • 你從不懂, 時(shí)光里的筆尖, 在書(shū)寫(xiě)的過(guò)程中, 早已失去昨日的堅(jiān)挺, 剩下的是無(wú)盡的愁容。 你從不懂, 炫麗的落日,...
    粒菈Leila_P閱讀 241評(píng)論 0 0
  • 實(shí)在不行理解撒嬌賣(mài)萌的威力竟可以那么大! 會(huì)撒嬌孩子有糖吃,可是我每次都要自己買(mǎi)糖! 撒嬌女人最好命,撒嬌是女人的...
    易大廈閱讀 179評(píng)論 0 0

友情鏈接更多精彩內(nèi)容