无法同时向 Spock 和 Spring 注入 Spring 依赖项
Cant inject Spring dependencies to both Spock and Spring at same time
我在设置测试时遇到问题。我使用的是最新版本的 SpringBoot 和 Spock Framework。首先,我没有以 "traditional" 的方式配置 bean。除了 Facade
之外,我在包中的所有 classes 都是包范围的。我没有使用 @Component
、@Service
等
我注射的唯一 class 是 Repository
。让我告诉你我的 Configuration
class
@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);
}
}
它工作正常,我已经手动测试了所有场景(将 POST 发送到特定端点)。让我向您展示 SurveyCreator
class 我想测试的示例方法。
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
}
}
第一个测试通过,所以我知道我的测试一切正常。但是,我在上面发布的方法中的 java 代码中得到了 NullPointer。看起来 SurveyRepository
没有在测试期间注入到 java 代码中,因为那是导致此异常的原因......关于如何解决这个问题的任何想法,将我的 Repository
注入到 Spring 应用程序和 Spock 测试?
如果没有理由反对,我建议您 运行 在 "underlying bean" 上进行测试(而不是手动创建的实例):
@Autowired
private SurveyFacade surveyFacade;
我在设置测试时遇到问题。我使用的是最新版本的 SpringBoot 和 Spock Framework。首先,我没有以 "traditional" 的方式配置 bean。除了 Facade
之外,我在包中的所有 classes 都是包范围的。我没有使用 @Component
、@Service
等
我注射的唯一 class 是 Repository
。让我告诉你我的 Configuration
class
@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);
}
}
它工作正常,我已经手动测试了所有场景(将 POST 发送到特定端点)。让我向您展示 SurveyCreator
class 我想测试的示例方法。
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
}
}
第一个测试通过,所以我知道我的测试一切正常。但是,我在上面发布的方法中的 java 代码中得到了 NullPointer。看起来 SurveyRepository
没有在测试期间注入到 java 代码中,因为那是导致此异常的原因......关于如何解决这个问题的任何想法,将我的 Repository
注入到 Spring 应用程序和 Spock 测试?
如果没有理由反对,我建议您 运行 在 "underlying bean" 上进行测试(而不是手动创建的实例):
@Autowired
private SurveyFacade surveyFacade;