编程语言
首页 > 编程语言> > java – Spring @Value注释不能与mockito mock一起使用

java – Spring @Value注释不能与mockito mock一起使用

作者:互联网

我正在使用spring @Value注释并为A类中的某些字段设置值.

我正在为这个A类编写单元测试.在测试课中,我用Mockito @Spy注释了A类的参考.我将值设置为系统属性,然后调用MockitoAnnotations.initMocks(this).

我的期望是,间谍对象将通过@Value注释使用系统属性中的值初始化字段.但这不会发生.

请有人解释一下吗?

解决方法:

我有一个类似的测试,我使用以下相关代码:

@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春季课:

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

标签:java,unit-testing,spring,spring-annotations,mockito
来源: https://codeday.me/bug/20190825/1716745.html