【发布时间】:2018-01-24 18:56:04
【问题描述】:
我正在尝试为具有从属性文件注入的字段值的类编写测试。我试图在运行 TestNG 测试时利用 TestPropertySource 注释在其中获取值,但它似乎忽略了我的属性文件。
有许多类似的问题,我尝试仔细阅读并尽可能尝试它们的实现。不过,我的问题似乎略有不同,原因如下:
“@TestPropertSource and @PropertySource don't work for JUnit”:谈论 JUnit 而不是 TestNG(可能相关也可能不相关?),还谈到将属性值注入到 test 类的字段中(相对于被测单元)。
“@TestPropertySource not working?”:讨论如何忽略缺失的属性,而不是如何修复它。
“@TestPropertySource is not loading properties”:还谈到了将值注入到测试类字段中(尝试过
@RunWith或@SpringBootTest,但这也没有帮助)。Google search results 中的其他链接也没有帮助。
您需要做什么才能获得一个带有@Value 注释字段的被测单元,并设置了它们的属性?我可以以某种方式请求 spring 为我提供我的类的实例,而不是自己newing 他们吗?
这是一个最小的复制。
Foo.java
package nl.jeroenheijmans.stackoverflow.testngprops;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Foo {
@Value("${my.prop}")
private String myProp;
public String ExposeProp() {
return myProp;
}
}
FooTest.java
package nl.jeroenheijmans.stackoverflow.testngprops;
import org.springframework.test.context.TestPropertySource;
import org.testng.Assert;
import org.testng.annotations.Test;
@TestPropertySource("classpath:application.properties")
public class FooTest {
@Test
public void sanityCheck(){
Foo foo = new Foo();
Assert.assertNotNull(foo); // Success!
}
@Test
public void testProperty() {
Foo foo = new Foo();
Assert.assertEquals(foo.ExposeProp(), "checkcheck"); // Fail!
}
}
application.properties(在main 和test 文件夹中)
my.prop=checkcheck
Main.java
package nl.jeroenheijmans.stackoverflow.testngprops;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource(value = {"classpath:application.properties"})
public class Main extends SpringBootServletInitializer {
public static void main(String... args) {
SpringApplication.run(Main.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Main.class);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>nl.jeroenheijmans.stackoverflow</groupId>
<artifactId>testngprops</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${org.springframework.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<java.version>1.8</java.version>
<maven.compiler.version>3.5</maven.compiler.version>
<org.springframework.boot.version>1.5.1.RELEASE</org.springframework.boot.version>
<testng.version>6.9.10</testng.version>
<mockito.version>1.9.5</mockito.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
【问题讨论】:
标签: java maven spring-boot testng