2017-08-16 42 views
1

我正在嘗試編寫具有從屬性文件注入字段值的類的測試。我試圖利用TestPropertySource註釋在運行TestNG測試時在那裏獲取值,但似乎忽略了我的屬性文件。TestNG測試不使用TestPropertySource注入@Value

有幾十個類似的問題,我試圖仔細閱讀並儘可能地嘗試它們的實現。看來我的問題是尚未略有不同,雖然,這裏的原因:

你有什麼需要做的就是一個單元,被測與他們的屬性設置@Value註釋字段?我能否以某種方式要求春天向我提供我的班級實例而不是自己的實例?

這裏是一個最小的repro。

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(均maintest文件夾)

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> 

回答

2

以下是我解決了這個。簡而言之,我相信您錯過了擴展org.springframework.test.context.testng.AbstractTestNGSpringContextTests並通過@Autowire註釋使用依賴注入作爲對象Foo的部分。由於您正在實例化Foo對象,因此這些值未被注入到該對象中,這就解釋了爲什麼斷言失敗。

主要。java的

package com.rationaleemotions.stackoverflow.qn45716815; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; 
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.web.support.SpringBootServletInitializer; 
import org.springframework.context.annotation.PropertySource; 

@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class}) 
@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); 
    } 
} 

之所以使用@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})可以在這個主題中找到:Mongo tries to connect automatically to port 27017(localhost)

更新:排除蒙戈的配置是可選的,你不需要做這個,如果你有一個爲Mongo正確設置的項目。

這裏的FooTest.java的樣子

package com.rationaleemotions.stackoverflow.qn45716815; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.test.context.SpringBootTest; 
import org.springframework.test.context.TestPropertySource; 
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; 
import org.testng.Assert; 
import org.testng.annotations.Test; 

@TestPropertySource("classpath:application.properties") 
@SpringBootTest 
public class FooTest extends AbstractTestNGSpringContextTests{ 
    @Autowired 
    private Foo foo; 

    @Test 
    public void sanityCheck() { 
     Assert.assertNotNull(foo); 
    } 

    @Test 
    public void testProperty() { 
     Assert.assertEquals(foo.ExposeProp(), "checkcheck"); 
    } 
} 

這裏是我的Maven依賴看起來像

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-web</artifactId> 
    <version>1.5.6.RELEASE</version> 
    <exclusions> 
     <exclusion> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-logging</artifactId> 
     </exclusion> 
    </exclusions> 
</dependency> 
<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-test</artifactId> 
    <version>1.5.6.RELEASE</version> 
    <scope>test</scope> 
</dependency> 

之所以添加排除到春季啓動 - 啓動日誌記錄可以在這裏找到:Disable Logback in SpringBoot

更新:排除logback是可選的,如果您有一個正確設置用於使用logback的項目,則不需要這樣做。

下面是輸出的,當我跑這個測試:

objc[36167]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java (0x1026784c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x1027404e0). One of the two will be used. Which one is undefined. 
log4j:WARN No appenders could be found for logger (org.springframework.test.context.BootstrapUtils). 
log4j:WARN Please initialize the log4j system properly. 

    . ____   _   __ _ _ 
/\\/___'_ __ _ _(_)_ __ __ _ \ \ \ \ 
(()\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 
\\/ ___)| |_)| | | | | || (_| | )))) 
    ' |____| .__|_| |_|_| |_\__, |//// 
=========|_|==============|___/=/_/_/_/ 
:: Spring Boot ::  (v1.5.6.RELEASE) 


=============================================== 
Default Suite 
Total tests run: 2, Failures: 0, Skips: 0 
=============================================== 
+0

THX的答案,要檢查它在一個位。 (我對Mongo彈出的方式感到困惑,並沒有意識到鏈中有與mongo相關的依賴關係。) – Jeroen

+0

@Jeroen - 如果它回答了你的問題,請不要忘記接受答案。 –

+0

'@ SpringBootTest' +'...擴展了AbstractTestNGSpringContextTests' +'@ AutoWired'屬性取得了訣竅。 Mongo的東西,Logback的東西,和Log4j的東西並不需要讓我的測試通過。 – Jeroen