Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
danielerepici opened this issue Apr 9, 2020 · 4 comments
Labels
status: superseded An issue that has been superseded by another

Comments

@danielerepici
Copy link

@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 {
  ...
}
@snicoll
Copy link
Member

snicoll commented Apr 9, 2020

@SpringBootTest(classes = ...) does not seem to pick classes annotated by JSR-330 @nAmed if they have a constructor.

The important missing piece is that the bean is not created if the constructor is not public. I can't see why that would be related to Spring Boot at this point, moving to framework.

@snicoll snicoll transferred this issue from spring-projects/spring-boot Apr 9, 2020
@encircled
Copy link
Contributor

Hi,

The bug is in spring boot in BeanDefinitionLoader, @snicoll please move it back and I'll push the PR

@snicoll snicoll transferred this issue from spring-projects/spring-framework Apr 13, 2020
@snicoll snicoll added the status: waiting-for-triage An issue we've not yet triaged label Apr 13, 2020
@philwebb
Copy link
Member

The commit that originally implemented this logic is d82c508

@snicoll
Copy link
Member

snicoll commented Apr 15, 2020

Closing in favour of PR #20929

@snicoll snicoll closed this as completed Apr 15, 2020
@snicoll snicoll added status: superseded An issue that has been superseded by another and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants