003 Spock和Groovy使用
||约 1 分钟 · 66 字|评论
请注意,本文编写于 554 天前,最后修改于 145 天前,其中某些信息可能已经过时。
003 Spock和Groovy使用
0. 前置条件
maven引入依赖
xml<groovy-all.version>3.0.23</groovy-all.version> <spock.version>2.1-groovy-3.0</spock.version> <dependency> <groupId>org.spockframework</groupId> <artifactId>spock-core</artifactId> <version>${spock.version}</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all --> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>${groovy-all.version}</version> <type>pom</type> </dependency>
在maven的test目录下创建名为groovy的目录,并标记为maven的Test Resource Root
存在如下的handler
java/** * 获取默认的prompt * @param ctx 上下文 * @return result */ public Result<Prompt> getDefaultPrompt(Context ctx) { Result<Prompt> res = Result.success(null, ctx.path()); try (SqlSession vanblogSqlSession = SqlSessionHolder.getVanblogSqlSession()) { SqlSessionHolder.setSession(vanblogSqlSession); promptService = new PromptServiceImpl(); Prompt insertPrompt = promptService.getDefaultPrompt(); if (insertPrompt != null) { vanblogSqlSession.commit(); res.setData(insertPrompt); }else { vanblogSqlSession.rollback(); res.setStatus(500); res.setError("获取默认的prompt失败"); } }catch (Exception e){ res.setStatus(500); res.setError("获取默认的prompt失败"); res.setMessage(e.getMessage()); }finally { SqlSessionHolder.clear(); } return res; }
需要对如上的handler的使用spock测试
1. 基础用法
plaintextclass AiModelHandlerTest1 extends Specification { def "从数据库获取默认的prompt,已知默认的id为7"() { given: def vanBlogSession = SqlSessionHolder.getVanblogSqlSession() def promptService = new PromptServiceImpl() SqlSessionHolder.setSession(vanBlogSession) when: def prompt = promptService.getDefaultPrompt() then: prompt.id == 1 } }
测试未通过
版权声明