我使用Karate framework V 0.7.0来执行一些自动API测试。
我在以下场景中有一个测试返回异常com.intuit.karate.exception.KarateException:reason:不是子字符串
以下是以下情况:
Scenario Outline: Put a number, and check the JSON response body.
Given path '/number/check/'
And param serial = '<number>'
When method GET
Then status 200
And match $ contains {"uid":"#uuid","reference":"#notnull","type":"#notnull","name":"#notnull","description":"#notnull","imageUrl":"#notnull","color":"<color>","reason":"<reason>"}
Examples:
| number | color | reason |
# Case 1
| 1234 | RED | INITIAL_WARRANTY_EXPIRED |
# Case 2
| 5678 | RED | INELIGIBLE_PRODUCT_FAMILY |
# Case 3
| 9012 | RED | ALREADY_EXTENDED |
案例1和案件2正确通过。
案例3返回以下内容:
StackTrace:
com.intuit.karate.exception.KarateException: path: $, actual: '[{"uid":"2cd897fd-1421-4c02-b594-6ee0de38db3d","reference":"a random reference","type":"a random type","name":"a random name","description":"a random description","imageUrl":"a random image url","color":"RED","reason":"ALREADY_EXTENDED"}]', expected: '{"uid":"#uuid","reference":"#notnull","type":"#notnull","name":"#notnull","description":"#notnull","imageUrl":"#notnull","color":"RED","reason":"ALREADY_EXTENDED"}', reason: not a sub-string
at com.intuit.karate.StepDefs.matchNamed(StepDefs.java:540)
at com.intuit.karate.StepDefs.matchContains(StepDefs.java:532)
at ✽.And match $ contains {"uid":"#uuid","reference":"#notnull","type":"#notnull","name":"#notnull","description":"#notnull","imageUrl":"#notnull","color":"RED","reason":"ALREADY_EXTENDED"}
我已经检查手动,这是案例3的 JSON答案:
[{"uid":"2cd897fd-1421-4c02-b594-6ee0de38db3d","reference":"a random reference","type":"a random type","name":"a random name","description":"a random description","imageUrl":"a random image url","color":"RED","reason":"ALREADY_EXTENDED"}]
我不明白为什么仅案例3失败。
有什么想法吗?
非常感谢!
编辑1:“”引用“是一个错误的复制粘贴。抱歉。
答案 0 :(得分:2)
更新
经过调查,完全相同的测试在 JUnit 启动时运行良好,但不是使用 Maven 。这是编码的问题。
解决方案:使用以下命令强制Surefire插件中的编码:
<argLine>-Dfile.encoding=UTF-8</argLine>
pom.xml中的是这样的:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<configuration>
<includes>
<include>api/TestParallel.java</include>
</includes>
<systemProperties>
<cucumber.options>--tags ~@ignore</cucumber.options>
</systemProperties>
<argLine>${argLine} -Dfile.encoding=${project.build.sourceEncoding}</argLine>
<configuration>
</plugin>
此处的文档: https://github.com/intuit/karate/tree/master#troubleshooting-cucumber-ide-support - &gt;的file.encoding。
重要提示: 如果您并行运行测试,并使用以下方法切换环境:
mvn clean test -DargLine="-Dkarate.env=YOUR_ENV"
使用语法很重要...
<argLine>${argLine} -Dfile.encoding=${project.build.sourceEncoding}</argLine>
......而不只是......
<argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>
否则,Maven -DargLine将被POM.xml中的覆盖。