• 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

  • 相关阅读:
    分形之城
    【SDOI2011 第2轮 DAY1】消防 树上问题+二分+贪心
    【Usaco Nov08 Gold】玩具 三分+贪心
    分治 复习
    快读板子
    最小线段覆盖 C神奇项链
    比赛经验积累1
    字符串 专题
    界面小项目之W3C
    前端小基础
  • 原文地址:https://www.cnblogs.com/yangyongjie/p/10790452.html
Copyright © 2020-2023  润新知