Reputation: 5051
I'd like to write a Spring integration test to make sure that @Autowired
correctly puts together my classes, but fail.
The test class
@RunWith(SpringRunner.class)
@DataJpaTest
@EntityScan(basePackageClasses = {ClassUnderTest.class, SomRepository.class, SomeEntity.class})
public class ClassUnderTestIT{
@Autowired private InterfaceUnderTest cut;
@Test public void autowires() {
assertThat(cut).isNotNull();
}
}
Should test that this @Service
autowires
@Service
@Transactional
public class ClassUnderTest implements InterfaceUnderTest {
private final SomeRepository repository;
@Autowired
public DefaultWatchlistDataModifier(SomeRepository repository) {
this.repository = repository;
}
}
Paying special attention to the wired dependency
@Repository
@Transactional(readOnly = true)
@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
public interface SomeRepository extends JpaRepository<SomeEntity, String> {
}
However, all I ever get is the exception
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.[...].InterfaceUnderTest' available:
expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
Meanwhile, I have experimented with all kinds of additional annotations, such as @ComponentScan
, @EnableJpaRepositories
... whatever I could dig up in the endless number of StackOverflow questions on the topic - all in vain.
Upvotes: 1
Views: 825
Reputation: 124526
Using [@DataJpaTest
] will only bootstrap the JPA part of your Spring Boot application. As the unit of your test isn't part of that subset it will not be available in the application context.
Either construct it yourself and inject the dependencies or use a full blown @SpringBootTest
instead.
Upvotes: 4