Весенний загрузочный тест: MockedBean конфликтует с другими - PullRequest
0 голосов
/ 08 октября 2018

В настоящее время у меня есть два RestTemplate бина:

@Bean
@Primary
public RestTemplate jwtRestTemplate(
    RestTemplateBuilder builder,
    JWTService jwtService) {

        return builder
            .additionalInterceptors(
                Collections.singletonList(
                    new JWTHeaderRequestInterceptor(jwtService)
                )
            )
            .build();
}

@Bean
public RestTemplate rawRestTemplate(RestTemplateBuilder builder) {
    return builder.build();
}

Первый является основным, а другой запрашивается @Qualifier("rawRestTemplate").

Однако яResTemplate

@RunWith(SpringRunner.class)
@SpringBootTest()
public class AuditoryTest {

    @MockBean()
    private RestTemplate frontOfficeRestTemplate;

    @Autowired
    private DocumentServiceBackOffice documentService;

DocumentServiceBackOffice Конструктор:

public DocumentServiceBackOffice(RestTemplate restTemplate);

Я получаю исключение:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in net.gencat.transversal.espaidoc.backoffice.service.DocumentServiceBackOffice required a single bean, but 2 were found:
    - rawRestTemplate: defined by method 'rawRestTemplate' in class path resource [net/gencat/transversal/espaidoc/backoffice/config/BackOfficeConfiguration.class]
    - jwtRestTemplate: defined by method 'createMock' in null


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

Сообщениедовольно ясно, но я не совсем понимаю, как это решить.

Есть идеи?

1 Ответ

0 голосов
/ 09 октября 2018

Я решил, что с помощью этого дополнительного Configuration класса:

@TestConfiguration
public static class RestTemplateTestConfiguration {

    @Bean("jwtRestTemplate")
    @Primary
    public static RestTemplate someService() {
        return Mockito.mock(RestTemplate.class);
    }
}
...