@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 {
...
}
Comment From: snicoll
@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.
Comment From: encircled
Hi,
The bug is in spring boot in BeanDefinitionLoader
, @snicoll please move it back and I'll push the PR
Comment From: philwebb
The commit that originally implemented this logic is d82c50804f04068275ee41d4f1c7e4365f3027df
Comment From: snicoll
Closing in favour of PR #20929