单元测试Spring Boot,用于在一个测试类

时间:2018-04-16 23:06:12

标签: java spring unit-testing spring-boot dependency-injection

在我的Spring Boot Web应用程序的服务层上,我有一个服务接口,有几个方法和两个实现bean。

interface VehicleService { }
@Service
class CarServiceImpl implements VehicleService {}
@Service
class BycicleServiceImpl implements VehicleService {}

我已为其中一项服务生成单元测试

@RunWith(SpringRunner.class)
public class CarServiceTest {

  @TestConfiguration
  static class VehicleServiceTestContextConfiguration {
      @Bean
      public VehicleService vehicleService() {
         return new CarServiceImpl();
      }
  }

  @Autowired
  private VehicleService vehicleService;

  @Test
  public void getCar() throws Exception {
    //tests here with CarServiceImpl object
  }

}

当我想运行测试BicycleServiceImpl bean时,我将不得不复制并粘贴同一个类,并将@Bean更改为返回BicycleServiceImpl,这似乎不是最佳解决方案。有什么方法可以两次运行相同的测试类并逐个注入每个bean。任何建议都将不胜感激。

0 个答案:

没有答案
相关问题