当前位置: 首页 > 知识库问答 >
问题:

spring boot 1.4、spock和应用程序。性质

公良英资
2023-03-14

我试图用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') {
}

那么我有这个

/src/test/groovy/resources:

# JWT Key
jwt.key=MyKy@99

最后是我的斯波克测试:

@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;
    }
}

我最初在我的测试中实例化了Token实用程序,但是application-test.properties从未被加载(我假设因为jwtKey是null)。所以我尝试@autowed我的类在测试中,但现在这是无效的。

看起来Spring Boot 1.4在测试中改变了很多,所以也许我没有正确地连接它?

共有2个答案

胡利
2023-03-14

我也有类似的问题,但对我来说,解决办法是更改双引号到单引号“…” Spock时,@Value注释内的code>。请查看以下示例:

@Value('${jwt.key}')
private String jwtKey;

PS-这不是问题的确切答案。我是为一个面临类似问题的人发这篇文章的,就像我的一样,最后来到这里。

卢阳成
2023-03-14

您的测试代码有几处错误;首先,您的依赖关系不好-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
    }
}
 类似资料:
  • 我有一个由Gradle2.4构建的Java库,它将被一些Java6应用程序、一些Java7应用程序、一些Java8应用程序和一些Groovy2.x应用程序使用。因此,为了尽可能地向后兼容,我编写的lib具有和1.6: 但是,我没有理由不能用Groovy/Spock编写单元测试。只要Groovy不是main/compile/runtime类路径的一部分,那么我就可以自由地用任何JVM语言编写测试!我

  • 使用ResourceConfig比应用程序有什么优势(因为ResourceConfig扩展了应用程序)。 在post Jersey 2 multipart formdata的注入源代码中,@Arul Dhesiaseelan回答了将MultiPartFeature添加到两者中以在服务器端启用该特性的问题。有人能解释一下。

  • 我正在尝试为Spring批处理应用程序编写测试,特别是在以下读取器从实现简单行映射器的数据库中获取记录时的交互: 以下是Batch配置中的步骤定义: 除了我尝试使用以下方法进行测试外,其他方法都可以正常工作: 我无法正确地模拟阅读器,我尝试了各种方法,比如更新阅读器的属性,比如,但似乎没有任何效果。 更新读者结果的正确方法是什么? 或者在单元测试期间,是否需要通过另一种方式来正确操作数据库中的记录

  • 我正在开发一个Spring Boot应用程序,我使用application.properties配置db连接、服务器端口等。 例如,如何在application.properties中实现以下XML配置

  • 我正在将一个非常基本的web应用程序部署到Google应用程序引擎。我使用的是Springboot,我可以在本地很好地运行应用程序,但当我部署到Google时,应用程序引擎不会启动实例。我在启动时配置了一个云SQL数据源。 我有云sql配置属性配置src/main/Resources/application.properties.App Engine似乎找不到这些属性,所以它无法正确设置Cloud

  • 我正在学习Spring boot application,并且有使用xml和java配置的Spring应用程序的经验。 我使用的应用程序具有包含UI、服务和DAO的代码基体系结构。所有这些组件都有单独的上下文文件,即。web-applicationcontext.xml或application-servlet-context.xml、service-context.xml和data-context