[英]Autowired bean in spock test null after downgrading spring from 4.3.x to 3.2.x
I downgrade my application from spring 4.x to 3.x and now when I fire simple test in spock which using autowired bean, this bean is null. 我将应用程序从Spring 4.x降级到3.x,现在当我在使用自动装配bean的spock中进行简单测试时,此bean为null。
@ContextConfiguration(classes = Configuration.class)
class SomeTestClass extends Specification {
@Autowired
SomeService someService
def "someService"(){
expect:
someService.returnHelloWorld() == "Hello World" // (<- NullPointer)
}
}
My pom.xml file: 我的pom.xml文件:
<dependencies>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.12</version>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.1-groovy-2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-spring</artifactId>
<version>1.1-groovy-2.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.1.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
After when I downgrade also spock-core/spring to 0.6-groovy-1.8 and groovy-all to 1.8 and fire my test it throws this exception: 在我降级后,还将spock-core / spring降级为0.6-groovy-1.8,并将groovy-all降为1.8,并触发我的测试,它引发了以下异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someAnotherBean' defined in file ../SomeBean.class: Instantiation of bean failed;
org.springframework.beans.factory.BeanCreationException:创建文件../SomeBean.class中定义的名称为“ someAnotherBean”的bean时出错:bean的实例化失败; nested exception isorg.springframework.beans.BeanInstantiationException: Could notinstantiate bean class [..SomeBean.class]: No default constructorfound;
嵌套异常isorg.springframework.beans.BeanInstantiationException:无法实例化bean类[..SomeBean.class]:未找到默认构造函数; nested exception is java.lang.NoSuchMethodException:..SomeBean.()
嵌套的异常是java.lang.NoSuchMethodException:.. SomeBean。()
This bean contains contructor which i used to intizialize final fieds in class: 这个bean包含构造函数,我曾经在类中初始化最终的结局:
@Component
@PropertySource("classpath:someproperties.properties")
public class HeaderFactory {
private final SomeObject someObject;
public HeaderFactory(@Value("${someProperty1}") String someProperty1, @Value("${someProperty2}") String someProperty2) {
SomeObject someObject = new SomeObject(someProperty1,someProperty2);
this.someObject = someObject;
}
}
Everything worked pretty well before I dowgraded spring version. 在我升级春季版本之前,一切工作都很好。 Any ideas?
有任何想法吗?
You could create a configuration class and define the problematic beans there: 您可以创建一个配置类并在那里定义有问题的bean:
@Configuration
class MyConfig {
@Value("${someProperty1}") String prop1;
@Value("${someProperty2}") String prop2
@Bean
public SomeBean someBean() {
SomeBean bean = new SomeBean(prop1, prop2);
return bean;
}
}
Documentation here: https://docs.spring.io/spring/docs/3.2.0.RELEASE/spring-framework-reference/html/new-in-3.0.html#new-feature-java-config 此处的文档: https : //docs.spring.io/spring/docs/3.2.0.RELEASE/spring-framework-reference/html/new-in-3.0.html#new-feature-java-config
I resolved my issue by this way: 我通过以下方式解决了我的问题:
@Component
public class UrlBuilder {
private final String host;
private final String port;
private final String protocol;
@Autowired
public UrlBuilder(Environment env) {
this.protocol = env.getRequiredProperty("app.server.protocol").toLowercase();
this.serverHost = env.getRequiredProperty("app.server.host");
this.serverPort = env.getRequiredProperty("app.server.port", Integer.class);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.