无法自动关联测试中的依赖项

时间:2018-11-15 08:25:44

标签: spring autowired spring-test

我想编写一个Spring集成测试,以确保Sub Open_PDF() Dim filePath As String, fileName As String filePath = "D:\Reports\" fileName = Dir(filePath & "Process Report*.pdf") If fileName <> "" Then openAnyFile filePath & fileName End If End Sub Function openAnyFile(strPath As String) Dim objShell As Object Set objShell = CreateObject("Shell.Application") objShell.Open (strPath) End Function 正确地将我的类组合在一起,但是失败了。

测试班

@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 ...我可以从无数关于该主题的StackOverflow问题中挖掘出的一切-都是徒劳的

1 个答案:

答案 0 :(得分:3)

使用[@DataJpaTest]只会引导您的Spring Boot应用程序的JPA部分。由于测试单元不是该子集的一部分,因此在应用程序上下文中将不可用。

要么自己构造它,然后注入依赖项,要么改用完整的@SpringBootTest