Orkun
Orkun

Reputation: 7268

RestClientTest and NoSuchBeanDefinitionException

I m trying to use the @RestClientTest to test a rest client class.

It is stated that :

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 {
...

So as expected,I get errors such as: NoSuchBeanDefinitionException for the beans that are indeed are out of concern.

Is there a way to skip these errors? Or do I configure the context for this particular test class or sth?

Thanks in advance.

Upvotes: 4

Views: 1993

Answers (1)

Orkun
Orkun

Reputation: 7268

I found a solution.

Since we cannot use the @RestClientTest with @SpringBootTest, stick with the usual @SpringBootTest and use the test utility classes as follows:

@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);
    }
}

Upvotes: 4

Related Questions