Skip to content

@SpringBootTest not picking classes annotated by @Named with a constructor #20930

@danielerepici

Description

@danielerepici

@SpringBootTest(classes = ...) does not seem to pick classes annotated by JSR-330 @Named if they have a constructor. It works with other spring annotation (@Component, @Service, etc). It works if the class does not have a constructor.

I am using 2.2.6.RELEASE version.

E.g.
The following test fails

@Named
public class DummyService {
  DummyService() {
  }
}

@SpringBootTest(classes = {DummyService.class})
class DummyTest {
  
  @Autowired
  DummyService dummyService;

  @Test
  void dummyServiceExists() {
    assertNotNull(dummyService);
  }
}

This change to DummyService would make it succeed:

@Named
public class DummyService {
}

Also the following change to DummyService would make it succeed:

@Component
public class DummyService {
  DummyService() {
  }
}

Or you can change your test strategy and it would succeed:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {DummyService.class})
class DummyTest {
  ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: supersededAn issue that has been superseded by another

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions