RestClientTest 和 NoSuchBeanDefinitionException

RestClientTest and NoSuchBeanDefinitionException

我正在尝试使用 @RestClientTest 来测试休息客户端 class。

据说:

It will apply only configuration relevant to rest client tests (Jackson or GSON auto-configuration, and @JsonComponent beans), but not regular @Component beans.

@RunWith(SpringRunner.class)
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RestClientTest(JasperClient.class)
public class JasperClientTest {
...

所以正如预期的那样,我收到如下错误:NoSuchBeanDefinitionException 对于确实不关心的 bean。

有没有办法跳过这些错误?或者我是否为这个特定测试配置上下文 class 或 sth?

提前致谢。

我找到了解决办法。

由于我们不能将@RestClientTest 与@SpringBootTest 一起使用,因此请坚持使用通常的@SpringBootTest 并使用测试实用程序类,如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class JasperClientTest {

    @Value("${jasper.baseUri}")
    private String jasperBaseURI;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    private JasperClient jasperClient;

    private MockRestServiceServer mockRestServiceServer;

    @Before
    public void setUp() {
        mockRestServiceServer = MockRestServiceServer.createServer(restTemplate);
    }

    @Test
    public void sendRequest() {

        String detailsString ="{message : 'under construction'}";

        String externalId = "89610185002142494052";
        String uri = jasperBaseURI +  "/devices/" + externalId + "/smsMessages";

        mockRestServiceServer.expect(requestTo(uri)).andExpect(method(POST))
                .andRespond(withSuccess(detailsString, MediaType.APPLICATION_JSON));

        boolean isSentSuccessfully = jasperClient.sendRequest(externalId);
        assertTrue(isSentSuccessfully);
    }
}