不能注入Spring的依赖既斯波克和Spring在同一时间

问题描述 投票:1回答:1

我在与建立我的测试烦恼。我使用的是最新版本SpringBoot和斯波克框架。首先,我没有配置我的豆子“传统”的方式。我在包中的所有类除了Facade是包范围的。我没有使用@Component@Service等。

唯一的一类肌肉注射是qazxsw POI。让我告诉你我的qazxsw POI类

Repository

它工作正常,我心中已经测试过所有的手动(发送POST某些终点)。让我告诉你从Configuration类我想测试实例方法。

@Configuration
class SurveyConfiguration {
    @Bean
    SurveyFacade surveyFacade(SurveyRepository surveyRepository) {
        ConversionUtils conversionUtils = new ConversionUtils();
        SurveyValidator surveyValidator = new SurveyValidator();
        SurveyCreator surveyCreator = new SurveyCreator(surveyRepository, conversionUtils, surveyValidator);
        return new SurveyFacade(surveyCreator);
    }
}

就像我说的,在运行时能正常工作。所以,让我们讨论测试

SurveyCreator

首先测试通过,让我明白了我一切OK了测试。但是,我在我的方法上面张贴了我的Java代码越来越空指针。貌似注入过程中的测试Java代码SurveyDTO createSurvey(final SurveyDTO surveyDTO) throws ValidationException, PersistenceException { Survey survey = conversionUtils.surveyToEntity(surveyDTO); surveyValidator.validate(survey); Optional<Survey> savedInstance = Optional.ofNullable(surveyRepository.save(survey)); //Will throw NullPtr return savedInstance.map(conversionUtils::surveyToDTO) .orElseThrow(PersistenceException::new); } 心不是,因为那导致该异常的一个...任何想法如何解决的是,已经我@SpringBootTest class SurveyFacadeTest extends Specification { @Autowired private SurveyRepository surveyRepository private SurveyFacade surveyFacade = new SurveyConfiguration().surveyFacade(this.surveyRepository) def "should inject beans"() { expect: surveyRepository != null surveyFacade != null } def "should create survey and return id"() { given: Long id when: id = surveyFacade.createSurvey(SampleSurveys.validSurvey()) then: id != surveyFacade } } 两个Spring应用程序和斯波克测试注射?

spring spring-boot dependency-injection spock
1个回答
1
投票

如果有反对任何理由,我建议你运行“底层豆”(而不是手动创建的实例)上的测试:

SurveyRepository
© www.soinside.com 2019 - 2024. All rights reserved.