我是春天的新手,下面是我想做的事情:
我使用的是一个基于maven的库,它有自己的Spring上下文和自动处理的字段。
它的bean配置文件是src/test/resources/cucumber.xml。
我在图书馆里有个这样的课:
@Component
public class ConfigContainer {
@Value("${lc.service.uri}")
private String lcServiceUri;
@Value("${lc.service.port}")
private Integer lcServicePort;
}
}然后,我有一个测试应用程序(基于maven和黄瓜),其中我想使用库中的自动处理字段。
我的应用程序的bean配置文件是src/test/resources/cucumber.xml。
我正在使用@RunWith(Cucumber.class)进行黄瓜测试。
我有以下maven依赖项:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
<version>1.2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>theInternalLibrary</groupId>
<artifactId>internal-api-tests</artifactId>
<version>1.0.15-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
</exclusion>
<exclusion>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
</exclusion>
<exclusion>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.6.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>当我尝试使用@ContextConfiguration @ContextConfiguration和 @Autowired 注释将这个注入应用程序时,对象值是 null**.**
@ContextConfiguration(locations = {"classpath:cucumber.xml"})
public class CleanupTest {
@Autowired
protected ConfigContainer configContainer;
}但是,如果我使用new ClassPathXmlApplicationContext("cucumber.xml")**,注入对象,那么该对象将正确地使用预期值初始化。**
public class CleanupTest {
protected ApplicationContext context = new ClassPathXmlApplicationContext("cucumber.xml");
private ConfigContainer configContainer = (ConfigContainer)context.getBean("configContainer");
}请您解释一下为什么会发生这种情况,以及如何使用spring注解注入字段?
谢谢。
发布于 2016-02-03 09:34:51
我真的很想念黄瓜-春天的依赖:
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
<version>1.2.4</version>
<scope>test</scope>
</dependency>在添加这个依赖项之后,它可以很好地处理spring注释。
谢谢你给我指明了正确的方向。
发布于 2016-02-02 16:43:22
您需要用@RunWith(SpringJUnit4ClassRunner.class)注释您的测试类。否则,JUnit只创建带有默认构造函数的测试类实例。
https://stackoverflow.com/questions/35158411
复制相似问题