我想写一个Spring集成测试来确保
@Autowired
考试班
@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();
}
}
应该测试一下这个
@Service
@Service
@Transactional
public class ClassUnderTest implements InterfaceUnderTest {
private final SomeRepository repository;
@Autowired
public DefaultWatchlistDataModifier(SomeRepository repository) {
this.repository = repository;
}
}
特别注意有线依赖关系
@Repository
@Transactional(readOnly = true)
@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
public interface SomeRepository extends JpaRepository<SomeEntity, String> {
}
然而,我得到的只是一个例外
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)}
同时,我还尝试了各种附加注释,例如
@ComponentScan
@EnableJpaRepositories
... 无论我能从无数关于这个话题的问题中挖掘出什么,都是徒劳的。