SpringBoot&Spring单元测试

SpringBoot

一、Service层Junit单元测试

需要的jar包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

Springboot 1.3的版本与1.4的版本稍有不同

1.3及以下版本

package com.suning.epp.fmasosweb.service.impl;

import com.suning.epp.fmasosweb.FmasosWebApplication;
import com.suning.epp.fmasosweb.result.RankGenreResult;
import com.suning.epp.fmasosweb.service.intf.CommentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉
 * 〈功能详细描述〉
 *
 * @author 17090889
 * @see [相关类/方法](可选)
 * @since [产品/模块版本] (可选)
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = FmasosWebApplication.class)
@WebAppConfiguration
public class CommentServiceImplTest {

    @Autowired
    private CommentService commentService;

    @Test
    public void queryAppRankGenreResultTest() {
        Map<String, String> param = new HashMap<>();
        List<RankGenreResult> rankGenreResultList = commentService.queryAppRankGenreResult(param);
        System.out.println(rankGenreResultList);
    }

}

1.4及以上版本

@SpringApplicationConfiguration 注解标记为过时了

提供了注解@SpringBootTest

使用SpringRunner 替代 SpringJUnit4ClassRunner

package com.suning.epp.fmasosweb.service.impl;

import com.suning.epp.fmasosweb.result.RankGenreResult;
import com.suning.epp.fmasosweb.service.intf.CommentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉
 * 〈功能详细描述〉
 *
 * @author 17090889
 * @see [相关类/方法](可选)
 * @since [产品/模块版本] (可选)
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class CommentServiceImplTest {

    @Autowired
    private CommentService commentService;

    @Test
    public void queryAppRankGenreResultTest() {
        Map<String, String> param = new HashMap<>();
        List<RankGenreResult> rankGenreResultList = commentService.queryAppRankGenreResult(param);
        System.out.println(rankGenreResultList);
    }

}

二、Controller层Mock测试

 1.3及以下版本

package com.suning.epp.fmasosadmin.mapper;

import com.suning.epp.fmasos.FmasosApplication;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

/**
 * 〈一句话功能简述〉
 * 〈功能详细描述〉
 *
 * @author 17090889
 * @see [相关类/方法](可选)
 * @since [产品/模块版本] (可选)
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = FmasosApplication.class)
@WebAppConfiguration
@Transactional
public class ProcessorServiceTest {

//    @Autowired
//    @Qualifier("commentProcessorServiceImpl")
//    private CommentProcessorService commentProcessorServiceImpl;

    @Autowired
    private WebApplicationContext webApplicationContext;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        //构造MockMvc
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();

    }


    @Test
    public void spiderRun() throws Exception {
        String url = "/comment/spiderRun2";
        mockMvc.perform(MockMvcRequestBuilders.get(url));
    }

}

1.4及以上版本

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class CommentControllerTest {
    @Autowired
    private MockMvc mvc;

    @Test
    public void spiderRun() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/spiderRun"))
                .andExpect(MockMvcResultMatchers.status().isOk());
        //.andExpect(MockMvcResultMatchers.content().string("365"));  //测试接口返回内容
    }

}

Spring

   1、需要在test/resources下新建spring配置文件,扫描注入测试需要的所有bean及依赖bean

/**
 * @author yangyongjie
 * @date 2020/2/26
 * @desc
 */
@RunWith(SpringJUnit4ClassRunner.class) // 启动 Spring 对测试类的支持
@ContextConfiguration("classpath:spring-*.xml") // 指定 Spring 配置文件或者配置类的位置,classpath路径为test/resources
public class AutoRenewCheckTaskTest {

    @Autowired
    private AutoRenewCheckTask autoRenewCheckTask;

    @Test
    public void executeTest(){
        autoRenewCheckTask.execute();
    }

}

  2、不在test/resources下新建spring配置文件也可,使用main/resources 下的Spring配置文件,此时需要使用 @ContextConfiguration 注解的 locations 属性指定配置文件在计算机上的绝对路径,如:

@RunWith(SpringJUnit4ClassRunner.class) // 启动 Spring 对测试类的支持
@ContextConfiguration(locations = {"file:D:\IdeaProjects\taskModuleOptimize\bssadmin-task\src\main\webapp\WEB-INF\spring\spring-*.xml"}) // 指定 Spring 配置文件或者配置类的位置
public class AutoRenewCheckTaskTest {

    @Autowired
    private AutoRenewCheckTask autoRenewCheckTask;

    @Test
    public void executeTest(){
        autoRenewCheckTask.execute();
    }

}

end

原文地址:https://www.cnblogs.com/yangyongjie/p/10790452.html