Spring @Value annotation not working with mockito mock

I am using spring annotation @Valueand setting values ​​for some fields in the class A.

I am writing unit tests for this class A. In the test class, I annotate the link for the class Ausing Mockito @Spy. I set the values ​​as system properties and then callMockitoAnnotations.initMocks(this).

My expectation is that the spire object will have fields initialized with values ​​from the system properties using annotation @Value. But this does not happen.

Please can anyone explain?

+3
source share
2 answers

Mockito Spring ! ! , java.

springockito, Spring, Mockito Spring. Spring .

, Spring JUnit .

+3

, :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/context.xml")
public class ContextTest {

    private boolean started = false;

    @Spy
    @Autowired
    private Baz baz;

    @Before
    public void before() {
        if (!started) {
            MockitoAnnotations.initMocks(this);
            started = true;
        }
    }

    @Test
    public void spy() {
        Assert.assertEquals("value", baz.getProperty());
        Mockito.verify(baz).getProperty();
    }

}

spring (- SpringJUnitRunner), Mockito ( MockitoAnnotations.initMocks(instanceOfTestClass)).


Baz.java spring :

package foo.bar;

import org.springframework.beans.factory.annotation.Value;

public class Baz {

    @Value("${property:test}")
    private String property;

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

}

context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>my.properties</value>
        </property>
    </bean>

    <bean id="baz" class="foo.bar.Baz" />

</beans>

my.property :

property=value

maven (pom.xml):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>spring-test</groupId>
    <artifactId>my.spring.test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.2.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
                    <scope>test</scope>
        </dependency>
    </dependencies>
</project>

:

+ spring-test
  - pom.xml
  + src/main/java
      + foo.bar
         - Baz.java
  + src/main/resources
      - context.xml
      - my.properties
  + src/test/java
      + foo.bar
         - ContextTest.java
+2

All Articles