spring-boot - Spring Boot 1.4,Spock和application.properties

标签 spring-boot spock spring-test

我正在尝试使用Spock为Spring Boot 1.4.0编写一些测试,并且未获取我的application-test-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/test/groovy/resources:


# JWT Key
jwt.key=MyKy@99

最后是我的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测试我的类(class),但现在它为null。

看起来Spring Boot 1.4进行了很多更改以进行测试,所以也许我没有正确连接起来?

最佳答案

您的测试代码有几处错误;首先,您的依赖关系很糟糕-Spock 1.0不支持@SpringBootTest注释,因此不会初始化任何上下文,也不会进行任何后处理,因此,空指针异常:不会自动接线。

在Spock 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
    }
}

关于spring-boot - Spring Boot 1.4,Spock和application.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38544788/

相关文章:

mysql - Spring Hibernate - 为简单的博客应用程序设置 mysql 表

java - Spock 中注释驱动和全局扩展之间的区别是什么

groovy - 使用 Spock 在多个返回结果中抛出 IOException

java - 使用 TestNG 和 Spring Transaction 进行并发测试

java - Spring Boot - 加载多个 YAML 文件

java - 如何在 spring boot 中从 message_ru_RU.properties 加载俄语(西里尔字母,utf-8)文本?

unit-testing - Grails/Spock : How to mock single method within class where method is called from within the class itself?

java - 使用 Spring Boot 1.4 MockMVC Controller 方法匹配时,'Cannot subclass final class java.lang.String'

使用 MongoDB 进行 Spring Web 服务测试

spring-boot - 如何通过例如配置默认 application.properties定制启动器?