在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));
}
}