简体   繁体   中英

Spring context does not initialize with @ContextConfiguration but does initialize with new ClassPathXmlApplicationContext

I'm new to Spring and here's what I'm trying to do:

I'm using a maven based library which has its own Spring context and autowired fields.
Its beans config file is src/test/resources/cucumber.xml
I have a class like this in the library:

@Component
public class ConfigContainer {
   @Value("${lc.service.uri}")
   private String lcServiceUri;
   @Value("${lc.service.port}")
   private Integer lcServicePort;   
   }
}

Then I have a test application (based on maven and cucumber) where I want to use autowired fields from the library.
The beans config file of my application is src/test/resources/cucumber.xml
I'm using @RunWith(Cucumber.class) to run the cucumber test.
I have the following maven dependencies:

<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>

When I try to inject this ConfigContainer into my application using the @ContextConfiguration and @Autowired annotations, the object value is null .

@ContextConfiguration(locations = {"classpath:cucumber.xml"})
public class CleanupTest {
   @Autowired
   protected ConfigContainer configContainer;
}

But if I inject it using new ClassPathXmlApplicationContext("cucumber.xml") , then the object is correctly initialized with the expected values.

public class CleanupTest {
   protected ApplicationContext context = new ClassPathXmlApplicationContext("cucumber.xml");
   private ConfigContainer configContainer = (ConfigContainer)context.getBean("configContainer");
}

Could you please explain to me why this is happening and what to do to be able to inject the field using spring annotations?
Thanks.

You need to annotate your test classes with @RunWith(SpringJUnit4ClassRunner.class) . Otherwise JUnit just creates an instance of the test class, with the default constructor.

I was actually missing the cucumber-spring dependency:

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-spring</artifactId>
    <version>1.2.4</version>
    <scope>test</scope>
</dependency>

It works fine with spring annotations after adding this dependency.
Thanks @Grogi for pointing me in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM