gpt4 book ai didi

spring-boot - Spring Boot 1.4,Spock和application.properties

转载 作者:行者123 更新时间:2023-12-04 17:44:52 24 4
gpt4 key购买 nike

我正在尝试使用Spock为Spring Boot 1.4.0编写一些测试,并且未获取我的application-test-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/test/groovy/resources:


# JWT Key
jwt.key=MyKy@99

最后是我的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测试我的类(class),但现在它为null。

看起来Spring Boot 1.4进行了很多更改以进行测试,所以也许我没有正确连接起来?

最佳答案

您的测试代码有几处错误;首先,您的依赖关系很糟糕-Spock 1.0不支持@SpringBootTest注释,因此不会初始化任何上下文,也不会进行任何后处理,因此,空指针异常:不会自动接线。

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

然后,您到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
}
}

关于spring-boot - Spring Boot 1.4,Spock和application.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38544788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com