首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring上下文不使用@ContextConfiguration初始化,而是使用新的ClassPathXmlApplicationContext初始化

Spring上下文不使用@ContextConfiguration初始化,而是使用新的ClassPathXmlApplicationContext初始化
EN

Stack Overflow用户
提问于 2016-02-02 16:14:04
回答 2查看 2K关注 0票数 0

我是春天的新手,下面是我想做的事情:

我使用的是一个基于maven的库,它有自己的Spring上下文和自动处理的字段。

它的bean配置文件是src/test/resources/cucumber.xml

我在图书馆里有个这样的课:

代码语言:javascript
复制
@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依赖项:

代码语言:javascript
复制
<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**.**

代码语言:javascript
复制
@ContextConfiguration(locations = {"classpath:cucumber.xml"})
public class CleanupTest {
   @Autowired
   protected ConfigContainer configContainer;
}

但是,如果我使用new ClassPathXmlApplicationContext("cucumber.xml")**,注入对象,那么该对象将正确地使用预期值初始化。**

代码语言:javascript
复制
public class CleanupTest {
   protected ApplicationContext context = new ClassPathXmlApplicationContext("cucumber.xml");
   private ConfigContainer configContainer = (ConfigContainer)context.getBean("configContainer");
}

请您解释一下为什么会发生这种情况,以及如何使用spring注解注入字段?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-03 09:34:51

我真的很想念黄瓜-春天的依赖:

代码语言:javascript
复制
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-spring</artifactId>
    <version>1.2.4</version>
    <scope>test</scope>
</dependency>

在添加这个依赖项之后,它可以很好地处理spring注释。

谢谢你给我指明了正确的方向。

票数 0
EN

Stack Overflow用户

发布于 2016-02-02 16:43:22

您需要用@RunWith(SpringJUnit4ClassRunner.class)注释您的测试类。否则,JUnit只创建带有默认构造函数的测试类实例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35158411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档