小编典典

Spring Boot 1.4,Spock和application.properties

spring-boot

我正在尝试使用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
为null)。因此,我正在尝试@Autowired测试我的课程,但现在它为null。

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


阅读 323

收藏
2020-05-30

共1个答案

小编典典

您的测试代码有几处错误;首先,您的依赖关系很糟糕-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
    }
}
2020-05-30