我想用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改变了很多测试,所以也许我没有正确的接线了吗?