码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot单元测试

时间:2019-04-29 15:38:43      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:factory   junit4   form   util   class   out   service   request   pre   

一、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及以上版本

未完待续...

SpringBoot单元测试

标签:factory   junit4   form   util   class   out   service   request   pre   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!