2016-07-23 63 views
3

我想用Spock爲我的Spring Boot 1.4.0編寫一些測試,並且我的應用程序測試屬性文件未被拾取。彈簧啓動1.4,spock和application.properties

我有這個在我的gradle產出:

dependencies { 

    compile('org.springframework.boot:spring-boot-starter-data-jpa') 
    compile('org.springframework.boot:spring-boot-starter-security') 
    compile('org.springframework.boot:spring-boot-starter-web') 
    compile 'org.codehaus.groovy:groovy-all:2.4.1'  
    testCompile('org.springframework.boot:spring-boot-starter-test') 
    testCompile('org.spockframework:spock-spring:1.0-groovy-2.4') { 
} 

然後,我有這

/src目錄/測試/常規/資源:

# JWT Key 
[email protected] 

最後我Spock測試:

@SpringBootTest(classes = MyApplication.class, webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT) 
@TestPropertySource("application-test.properties") 
public class TokenUtilityTest extends Specification { 

    @Autowired 
    private TokenUtility tokenUtility 

    def "test a valid token creation"() { 
     def userDetails = new User(username: "test", password: "password", accountNonExpired: true, accountNonLocked: true, 
     ); 

     when: 
     def token = tokenUtility.buildToken(userDetails) 

     then: 
     token != null 
    } 
} 

這是檢驗這個類:

@Component 
public class TokenUtility { 

    private static final Logger LOG = LoggerFactory.getLogger(TokenUtility.class); 

    @Value("${jwt.key}") 
    private String jwtKey; 

    public String buildToken(UserDetails user) { 
     return Jwts.builder() 
         .setSubject(user.getUsername()) 
         .signWith(SignatureAlgorithm.HS512, jwtKey) 
         .compact(); 
    } 

    public boolean validate(String token) { 
     try { 

      Jwts.parser().setSigningKey(jwtKey).parseClaimsJws(token); 
      return true; 

     } catch (SignatureException e) { 
      LOG.error("Invalid JWT found: " + token); 
     } 
     return false; 
    } 
} 

我原來實例化的TokenUtility在我的測試,但application-test.properties從未加載(我假設,因爲jwtKey爲空)。所以我正在測試@Autowired我的課程,但現在它是空的。

它看起來像Spring Boot 1.4改變了很多測試,所以也許我沒有正確的接線了嗎?

回答

6

測試代碼有幾個錯誤;首先,你的依賴關係很糟糕 - Spock 1.0不支持@SpringBootTest註釋,所以沒有上下文將被初始化,並且不會進行後期處理,因此空指針異常:什麼都不會自動佈線。

在斯波克1.1,這仍然是發佈,候選人被添加了對註釋的支持,所以你必須使用:

dependencies { 
    compile('org.springframework.boot:spring-boot-starter-data-jpa') 
    compile('org.springframework.boot:spring-boot-starter-security') 
    compile('org.springframework.boot:spring-boot-starter-web') 
    compile group: 'io.jsonwebtoken', name: 'jjwt', version: '0.6.0' 

    compile('org.codehaus.groovy:groovy') 

    testCompile('org.springframework.boot:spring-boot-starter-test') 
    testCompile('org.spockframework:spock-core:1.1-groovy-2.4-rc-1') 
    testCompile('org.spockframework:spock-spring:1.1-groovy-2.4-rc-1') 
    testCompile group: 'com.h2database', name: 'h2', version: '1.4.192' 
} 

然後,你的application-test.properties路徑是錯誤的並且應該是/application-test.properties,因爲它位於類路徑的根目錄中:

@SpringBootTest(classes = DemoApplication.class, 
       webEnvironment = WebEnvironment.RANDOM_PORT) 
@TestPropertySource("/application-test.properties") 
public class TokenUtilityTest extends Specification { 

    @Autowired 
    TokenUtility tokenUtility 

    def "test a valid token creation"() { 
     def userDetails = new User("test", "password", Collections.emptyList()); 

     when: 
     def token = tokenUtility.buildToken(userDetails) 

     then: 
     token != null 
    } 
}