2016-07-23 65 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 
    } 
}