一尘不染

SpringBoot @WebMvcTest和@MockBean无法正常工作

spring-boot

似乎@WebMvcTest@MockBean没有按预期工作。也许我缺少了一些东西…我有一个带有某些依赖关系的控制器@MockBean,但是我的应用程序无法启动,因为它找不到另一个我认为在这种情况下不需要的bean。

控制器:

@RestController
public class ExchangeRateStoreController {
    private AddExchangeRate addExchangeRate;
    private AddExchangeRateRequestAdapter addExchangeRateRequestAdapter;
    private GetExchangeRate getExchangeRate;
    private GetExchangeRateRequestAdapter getExchangeRateRequestAdapter;

    @Autowired
    public ExchangeRateStoreController(ExchangeRateRepository exchangeRateRepository, ExchangeRateDateValidator exchangeRateDateValidator, ExchangeRateView exchangeRateView) {
        addExchangeRate = new AddExchangeRate(exchangeRateRepository, exchangeRateDateValidator);
        addExchangeRateRequestAdapter = new AddExchangeRateRequestAdapter();
        getExchangeRate = new GetExchangeRate(exchangeRateView);
        getExchangeRateRequestAdapter = new GetExchangeRateRequestAdapter();
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public void create(@RequestBody AddExchangeRateRequest addExchangeRateRequest) {
        addExchangeRate.execute(addExchangeRateRequestAdapter.toCommand(addExchangeRateRequest));
    }
}

测试:

@RunWith(SpringRunner.class)
@WebMvcTest(ExchangeRateStoreController.class)
public class ExchangeRateStoreControllerTest {

    @Autowired
    private MockMvc mvc;  
    @MockBean
    ExchangeRateRepository exchangeRateRepository;
    @MockBean
    ExchangeRateDateValidator exchangeRateDateValidator;
    @MockBean
    ExchangeRateView exchangeRateView;

    @Test
    public void givenValidExchangeRateCommand_whenCreate_thenOK() throws Exception {
        String validRequestBody = "{\"from\":\"EUR\",\"to\":\"USD\",\"amount\":1.2345,\"date\":\"2018-11-19\"}";

        doNothing().when(exchangeRateDateValidator).validate(any());
        doNothing().when(exchangeRateRepository).save(any());

        mvc.perform(post("/").content(validRequestBody).contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isCreated());
    }

应用:

@SpringBootApplication
@EnableJpaRepositories("com...exchangerate.store.infrastructure.persistence")
@EntityScan("com...exchangerate.store.infrastructure.persistence")
@ComponentScan(basePackages = {"com...exchangerate.store.infrastructure", "com...exchangerate.store.application"} )
public class ExchangeRateStoreApplication {
    public static void main(String[] args) {
        SpringApplication.run(ExchangeRateStoreApplication.class, args);
    }
}

而我在运行测试时得到的错误:

申请开始失败


描述:

组件需要找不到名为’entityManagerFactory’的bean。

行动:

考虑在配置中定义一个名为“ entityManagerFactory”的bean。

但是,正如您所看到的,entityManagerFactory它不是控制器的依赖项。那么,为什么测试试图加载该bean?我在嘲笑所有的控制器依赖关系,所以我认为它不应该这样做。


阅读 759

收藏
2020-05-30

共1个答案

一尘不染

该问题是由于您@EnableJpaRepositories在应用程序的主类上使用引起的。通过将其放在主类上,表示无论您要测试哪个特定功能段,都必须始终启用JPA存储库。

您可以通过执行以下任一操作来解决问题:

  • 移动@EnableJpaRepositores@EntityScan进入单独的JPA特定的配置类
  • 删除@EnableJpaRepositories@EntityScan依靠自动配置的默认值。为此,您的存储库和实体必须位于主类包的子包中。

Spring Boot的参考文档中对此有更多信息,其中指出以下内容:

如果使用测试注释来测试应用程序的更特定的部分,则应避免在main方法的应用程序类上添加特定于特定区域的配置设置。

在这种特定情况下,特定于特定区域的配置设置为@EnableJpaRepositories

2020-05-30