service单元测试如何忽略ranner实例

本文介绍了如何在SpringBoot的Service单元测试中避免运行CommandLineRunner实例,通过注解的方式解决问题,但考虑到运行环境和性能因素,提出了具体的解决方案。
摘要由CSDN通过智能技术生成

在commandLineRanner实例上加注解可以解决service单元测试忽略ranner实例的问题。

但不太适合。(运行环境和绩效的问题)

具体解决如下:

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;


import java.util.ArrayList;
import java.util.List;


import static org.assertj.core.api.Assertions.assertThat;
import static org.powermock.api.mockito.PowerMockito.when;


/**
 * Created by ji_pengyun on 17-7-14.
 */
@RunWith(SpringRunner.class)
public class BusinessDataCompleteServiceTest {

    @TestConfiguration
    public static class ServiceConfigurationForTest {
        @Bean
        public BusinessDataCompleteService mockBusinessDataCompleteSerivce() {
            return new BusinessDataCompleteService();
        }
    }

    @Autowired
    private BusinessDataCompleteService businessDataCompleteService;

    @MockBean
    private BusinessDataCompleteMapper businessDataCompleteMapper;

    private static BusinessDataComplete businessDataComplete;

    private static List<BusinessDataComplete> businessDataCompletes = new ArrayList<>();

    private static List<Integer> provinceIds = new ArrayList<>();

    @BeforeClass
    public static void setUp() throws Exception {
        businessDataComplete = new BusinessDataComplete();
        businessDataComplete.setSubTaskId(5000);
        businessDataComplete.setProvinceId(999);
        businessDataCompletes.add(businessDataComplete);
        provinceIds.add(1);
        provinceIds.add(2);
    }

    @AfterClass
    public static void tearDown() throws Exception {
        businessDataComplete = null;
        provinceIds = null;
    }

    @Test
    public void testGetBusinessDataComplete() throws Exception {
        when(businessDataCompleteMapper.getBusinessDataComplete(Matchers.anyInt(), Matchers.anyInt())).thenReturn(businessDataCompletes);
        final List<BusinessDataComplete> result = businessDataCompleteService.getBusinessDataComplete(
businessDataComplete.getSubTaskId(), businessDataComplete.getProvinceId());
        assertThat(result.get(0).getProvinceId().equals(999));
    }

    @Test
    public void testUpdateBusinessDataComplete() throws Exception {
        when(businessDataCompleteMapper.updateBusinessDataComplete(Matchers.any(BusinessDataComplete.class))).thenReturn(1);
        Integer result = businessDataCompleteService.updateBusinessDataComplete(businessDataComplete);
        assertThat(result).isEqualTo(1);
    }

    @Test
    public void testGetBusinessDataCompleteBySubTaskIdAndProvinceIds() throws Exception {
        when(businessDataCompleteMapper.getBusinessDataCompleteBySubTaskIdAndProvinceIds(Matchers.anyInt(), Matchers.anyList())).thenReturn(businessDataCompletes);
        final List<BusinessDataComplete> result = businessDataCompleteService.getBusinessDataCompleteBySubTaskIdAndProvinceIds(businessDataComplete.getSubTaskId(), provinceIds);
        assertThat(result.get(0).getProvinceId().equals(999));
    }

    @Test
    public void testGetBusinessDataCompleteByYearStageNumberProvince() throws Exception {
        when(businessDataCompleteMapper.getBusinessDataCompleteByYearStageNumberProvince(Matchers.anyInt(), Matchers.anyInt(), Matchers.anyInt(), Matchers.anyInt())).thenReturn(businessDataComplete);
        final BusinessDataComplete result = businessDataCompleteService.getBusinessDataCompleteByYearStageNumberProvince(2017, 2, 1, businessDataComplete.getProvinceId());
        assertThat(result.getProvinceId().equals(999));
    }
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值