gpt4 book ai didi

java - RestClientTest 和 NoSuchBeanDefinitionException

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:01:42 28 4
gpt4 key购买 nike

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

据说:

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。

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

提前致谢。

最佳答案

我找到了一个解决方案。

由于我们不能将@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);
}
}

关于java - RestClientTest 和 NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49148595/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com