spring boot 1.4、spock 和 application.properties [英] spring boot 1.4, spock and application.properties
问题描述
我正在尝试使用 Spock 为我的 Spring Boot 1.4.0 编写一些测试,但我的 application-test-properties 文件没有被选中.
我的gradle里有这个:
依赖项{编译('org.springframework.boot:spring-boot-starter-data-jpa')编译('org.springframework.boot:spring-boot-starter-security')编译('org.springframework.boot:spring-boot-starter-web')编译'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 密钥jwt.key=MyKy@99
最后是我的 Spock 测试:
@SpringBootTest(classes = MyApplication.class, webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT)@TestPropertySource("application-test.properties")公共类 TokenUtilityTest 扩展规范 {@自动连线私人令牌实用程序令牌实用程序def "测试一个有效的令牌创建"() {def userDetails = new User(username: "test", password: "password", accountNonExpired: true, accountNonLocked: true,);当:def token = tokenUtility.buildToken(userDetails)然后:令牌 != 空}}
哪个正在测试这个类:
@Component公共类令牌实用程序{private static final Logger LOG = LoggerFactory.getLogger(TokenUtility.class);@Value("${jwt.key}")私人字符串 jwtKey;公共字符串 buildToken(UserDetails 用户) {返回 Jwts.builder().setSubject(user.getUsername()).signWith(SignatureAlgorithm.HS512, jwtKey).compact();}公共布尔验证(字符串令牌){试试{Jwts.parser().setSigningKey(jwtKey).parseClaimsJws(token);返回真;} catch (SignatureException e) {LOG.error("发现无效的 JWT:" + token);}返回假;}}
我最初在我的测试中实例化了 TokenUtility,但从未加载 application-test.properties(我假设因为 jwtKey 为空).所以我正在尝试@Autowired 我正在测试的班级,但现在为空.
看起来 Spring Boot 1.4 的测试变化很大,所以也许我没有正确连接它?
你的测试代码有几个问题;首先,您的依赖项很糟糕 - Spock 1.0 不支持 @SpringBootTest
注释,因此不会初始化上下文,也不会进行任何后处理,因此出现空指针异常:不会自动装配任何内容.>
Spock 1.1 中添加了对该注释的支持,它仍然是候选版本,因此您必须使用它:
依赖项{编译('org.springframework.boot:spring-boot-starter-data-jpa')编译('org.springframework.boot:spring-boot-starter-security')编译('org.springframework.boot:spring-boot-starter-web')编译组:'io.jsonwebtoken',名称:'jjwt',版本:'0.6.0'编译('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 组:'com.h2database',名称:'h2',版本:'1.4.192'}
那么,你的 application-test.properties 的路径是错误的,应该是 /application-test.properties
因为它在类路径的根目录中:
@SpringBootTest(classes = DemoApplication.class,webEnvironment = WebEnvironment.RANDOM_PORT)@TestPropertySource("/application-test.properties")公共类 TokenUtilityTest 扩展规范 {@自动连线令牌实用程序令牌实用程序def "测试一个有效的令牌创建"() {def userDetails = new User("test", "password", Collections.emptyList());当:def token = tokenUtility.buildToken(userDetails)然后:令牌 != 空}}
I am trying to write some tests for my Spring Boot 1.4.0 with Spock and my application-test-properties file is not being picked up.
I have this in my 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') {
}
Then I have this in
/src/test/groovy/resources:
# JWT Key
jwt.key=MyKy@99
And finally my Spock test:
@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
}
}
Which is testing this class:
@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;
}
}
I originally instantiated the TokenUtility on my test but the application-test.properties was never loaded (I am assuming since jwtKey was null). So I am trying @Autowired my class under test, but now that is null.
It looks like Spring Boot 1.4 changed a lot for testing, so perhaps I am not wiring this up correctly?
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
}
}
这篇关于spring boot 1.4、spock 和 application.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!