我想用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改變了很多測試,所以也許我沒有正確的接線了嗎?