Skip to content

TestRestTemplate not autowiring in spring-boot 4 #46558

@sombriks

Description

@sombriks

The following test case works well on spring boot 3.5:

package br.gov.ce.seplag.adbens.controllers;
import br.gov.ce.seplag.adbens.models.organizacao.Orgao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.test.client.TestRestTemplate;
import org.springframework.test.context.ActiveProfiles;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;

@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class OrgaosControllerTest {

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    void deveriaListarOrgaos() {
        var result = restTemplate.getForObject("/orgaos", Orgao[].class);
        assertThat(result, notNullValue());
    }

}

In spring boot 4 it fails on autowiring the TestRestTemplate

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.web.server.test.client.TestRestTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions