spring boot 1.4, spock and application.properties

℡╲_俬逩灬. 提交于 2019-12-05 10:35:35

There are several things wrong with your test code; first, your dependencies are bad - Spock 1.0 does not support @SpringBootTest annotation so no context will be initialized and no post-processing will be done, hence the null pointer exception: nothing will be autowired.

Support for that annotation was added in Spock 1.1, which is still release-candidate, so you'll have to use that:

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'
}

Then, your path to the application-test.properties is wrong and should be /application-test.properties since it is in the root of the classpath:

@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
    }
}

I had the similar problem, but for me, the solution was to change double quotes ".." to single quotes '..' inside the @Value annotation when working with Spock. Please find the example below:

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

PS - This is not the exact answer to the question. I am posting this for someone who faces a similar problem like mine and ends up here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!