Skip to content

Bean created by @MockBean still has its fields autowired #6663

@WildDev

Description

@WildDev

The following code taken from the doc doesn't work as expected:

@RunWith(SpringRunner.class)
@WebMvcTest(TestController.class)
public class TestControllerTest {

    @MockBean
    private TestValidator testValidator;

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void test() throws Exception {
        // no-opts
    });
}

... where's the mocked bean is:

@Component
public class TestValidator implements Validator {

    @Autowired
    private TestRepository testRepository;
    ...
}

during test startup it fails:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'testValidator': Error creating bean with name 'com.example.TestValidator#0': Unsatisfied dependency expressed through field 'categoryRepository': No qualifying bean of type [com.example.TestRepository] found for dependency [com.example.TestRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. 

Java: 1.8.0_65
Spring Boot: 1.4.0.RELEASE & 1.4.1-BUILD-SNAPSHOT
Maven: 3.3.3

Test project on GitHub

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions