son*_*rin 5 spring-test spock spring-boot
我正在尝试使用Spock为我的Spring Boot 1.4.0编写一些测试,并且我的应用程序测试属性文件没有被选中.
我在我的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') {
}
Run Code Online (Sandbox Code Playgroud)
然后我有这个
/ src目录/测试/常规/资源:
# JWT Key
jwt.key=MyKy@99
Run Code Online (Sandbox Code Playgroud)
最后我的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
}
}
Run Code Online (Sandbox Code Playgroud)
哪个测试这个类:
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
我最初在我的测试中实例化了TokenUtility,但是application-test.properties从未被加载(我假设因为jwtKey为null).所以我正在尝试@Autowired我的课程,但现在这是空的.
看起来Spring Boot 1.4在测试中发生了很大的变化,所以也许我没有正确连接它?
您的测试代码有几个问题; 首先,你的依赖是坏的 - Spock 1.0不支持@SpringBootTest
注释,所以不会初始化上下文,也不会进行后处理,因此空指针异常:什么都不会自动装配.
在Spock 1.1中添加了对该注释的支持,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'
}
Run Code Online (Sandbox Code Playgroud)
然后,您的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
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4304 次 |
最近记录: |