在 Spring Boot 测试中使用 @RestClientTest

Raj*_*ami 2 spring-boot spring-rest spring-boot-test

我想为@RestClientTest下面的组件编写一个简单的测试(注意:我可以在不使用@RestClientTest和模拟工作正常的依赖 bean 的情况下完成它。)。

@Slf4j
@Component
@RequiredArgsConstructor
public class NotificationSender {

    private final ApplicationSettings settings;
    private final RestTemplate restTemplate;

    public ResponseEntity<String> sendNotification(UserNotification userNotification)
            throws URISyntaxException {
            // Some modifications to request message as required
            return restTemplate.exchange(new RequestEntity<>(userNotification, HttpMethod.POST, new URI(settings.getNotificationUrl())), String.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

和测试;

@RunWith(SpringRunner.class)
@RestClientTest(NotificationSender.class)
@ActiveProfiles("local-test")
public class NotificationSenderTest {

    @MockBean
    private ApplicationSettings settings;
    @Autowired
    private MockRestServiceServer server;
    @Autowired
    private NotificationSender messageSender;

    @Test
    public void testSendNotification() throws Exception {
        String url = "/test/notification";
        UserNotification userNotification = buildDummyUserNotification();
        when(settings.getNotificationUrl()).thenReturn(url);
        this.server.expect(requestTo(url)).andRespond(withSuccess());

        ResponseEntity<String> response = messageSender.sendNotification(userNotification );

        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    }

    private UserNotification buildDummyUserNotification() {
     // Build and return a sample message
    }
}
Run Code Online (Sandbox Code Playgroud)

但我得到的错误是No qualifying bean of type 'org.springframework.web.client.RestTemplate' available。这当然是正确的,因为我没有嘲笑它或用来@ContextConfiguration加载它。

不是@RestClientTest配置一个RestTemplate吗?还是我理解错了?

Raj*_*ami 5

找到了!由于我使用的是RestTemplate直接注入的 bean,因此我们必须添加@AutoConfigureWebClient(registerRestTemplate = true)到解决此问题的测试中。

这是在@RestClientTest我以前似乎忽略的 javadoc 中。

测试成功;

@RunWith(SpringRunner.class)
@RestClientTest(NotificationSender.class)
@ActiveProfiles("local-test")
@AutoConfigureWebClient(registerRestTemplate = true)
public class NotificationSenderTest {

    @MockBean
    private ApplicationSettings settings;
    @Autowired
    private MockRestServiceServer server;
    @Autowired
    private NotificationSender messageSender;

    @Test
    public void testSendNotification() throws Exception {
        String url = "/test/notification";
        UserNotification userNotification = buildDummyUserNotification();
        when(settings.getNotificationUrl()).thenReturn(url);
        this.server.expect(requestTo(url)).andRespond(withSuccess());

        ResponseEntity<String> response = messageSender.sendNotification(userNotification );

        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    }

    private UserNotification buildDummyUserNotification() {
     // Build and return a sample message
    }
}
Run Code Online (Sandbox Code Playgroud)