我了解Spock 中的子句数据驱动测试where
。但我如何扩展它以使用一个where
进行多个测试呢?
例如,我有一组想要针对不同版本的库运行的测试:
@Unroll
def "test1 #firstlibVersion, #secondLibVersion"() {...}
@Unroll
def "test2 #firstlibVersion, #secondLibVersion"() {...}
...
Run Code Online (Sandbox Code Playgroud)
where 子句可能如下所示:
where:
[firstlibVersion, secondLibVersion] <<
[['0.1', '0.2'], ['0.2', '0.4']].combinations()
Run Code Online (Sandbox Code Playgroud)
我不想在每个测试中重复同样的 where 子句。我可以通过读取测试中的环境变量并使用不同的环境变量多次运行测试套件来实现这一点(测试矩阵样式,因为 travis 等 CI 服务支持它)。
但我更愿意直接在测试中执行此操作,这样我就不必多次运行测试套件。Spock 以某种方式支持这一点吗?
我有一个 Spring Boot 1.5.1.RELEASE 项目,使用 Spock 1.1 进行集成测试。我有一个基本控制器:
\n\n@RestController("/words")\npublic class WordsController {\n\n @RequestMapping(method = RequestMethod.GET)\n @ResponseBody\n public ResponseEntity getAllWords() {\n return ResponseEntity\n .status(HttpStatus.OK)\n .contentType(MediaType.APPLICATION_JSON)\n .body("Your list of words go here");\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n我正在尝试使用以下方法测试端点:
\n\n@ContextConfiguration(classes = MyApplication.class)\n@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)\n@ActiveProfiles("test")\nclass WordsControllerTest extends Specification {\nRESTClient restClient = new RESTClient("http://localhost:3000", ContentType.JSON)\n\n def "test the GET endpoint is available"() {\n when:\n def response = restClient.get(\n path: \'/words\',\n requestContentType: JSON\n )\n\n then:\n response.status == 200\n }\n
Run Code Online (Sandbox Code Playgroud)\n\n这是我的应用程序主类:
\n\n@SpringBootApplication\npublic class MyApplication {\n\n public …
Run Code Online (Sandbox Code Playgroud) 在我的测试中,我有一些只需要在某些情况下运行的功能方法。我的代码看起来像这样:
class MyTest extends GebReportingSpec{
def "Feature method 1"(){
when:
blah()
then:
doSomeStuff()
}
def "Feature method 2"(){
if(someCondition){
when:
blah()
then:
doSomeMoreStuff()
}
}
def "Feature method 3"(){
when:
blah()
then:
doTheFinalStuff()
}
}
Run Code Online (Sandbox Code Playgroud)
我应该注意,我使用的是自定义 spock 扩展,即使以前的功能方法失败,它也允许我运行规范的所有功能方法。
我刚刚意识到的事情以及我发表这篇文章的原因是因为“功能方法 2”由于某种原因没有出现在我的测试结果中,但方法 1 和 3 却出现了。即使someCondition
设置为true,它也不会出现在构建结果中。所以我想知道为什么会这样,以及如何使这个功能方法成为有条件的
是否可以编译带有常量 @Sql 注释的常规代码?
下面的代码是简单的测试,用 Spock 编写。
@Sql(statements = ["""
INSERT INTO pracownik ($Fields.KOMPETENCJA_ID, nr_ewid)
values (1, 'A');
INSERT INTO typ_zadania (id, kod) values (1, 'KOD');
"""]
)
def "should add new qualification"() {
//test code omitted
}
Run Code Online (Sandbox Code Playgroud)
当我想运行测试方法时,我在编译时收到错误:
Groovyc:预期 ' INSERT INTO pracownik ($Fields.KOMPETENCJA_ID, nr_ewid) 值 (1, 'A'); INSERT INTOtyp_zadania(id,kod)值(1,'KOD'); 成为 @org.springframework.test.context.jdbc.Sql 中 java.lang.String 类型的内联常量`
我认为带美元符号的多行字符串被评估为 GString 对象,但语句字段是字符串数组的类型。
我可以在groovy代码中使用多行字符串中的java注释常量吗?
我正在使用 Spock 和 Groovy 来测试一个类:
public class Animal {
public void findAnimal() {
findAnimalInZoo();
}
private void findAnimalInZoo() {
if (!isGoodWeather) {
throw Exception;
}
}
private boolean isGoodWeather() {
return "good".equals(Weather.getWeather());
}
}
Run Code Online (Sandbox Code Playgroud)
和Weather
班级:
public class Weather {
public static String getWeather() {
return instance.getWeather();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在方法的每个测试用例中findAnimal()
,我想指定调用时返回的值Weather.getWeather()
。
def "when it is good weather then expect no exception"() {
setup:
// What should I do for Weather.getWeather()?
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到呢?
几天前我问了一个关于存根 kafka.send() 方法的未来响应的问题。@kriegaex在这里正确回答并解释了这一点\n虽然我遇到了另一个问题,但我如何测试这个未来响应的 onSuccess 和 onFailure 回调。这是正在测试的代码。
\n\nimport org.springframework.kafka.core.KafkaTemplate;\nimport org.springframework.kafka.support.SendResult;\nimport org.springframework.util.concurrent.ListenableFuture;\nimport org.springframework.util.concurrent.ListenableFutureCallback;\n\npublic class KakfaService {\n\n private final KafkaTemplate<String, String> kafkaTemplate;\n private final LogService logService;\n\n public KakfaService(KafkaTemplate kafkaTemplate, LogService logService){\n this.kafkaTemplate = kafkaTemplate;\n this.logService = logService;\n }\n\n public void sendMessage(String topicName, String message) {\n ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(topicName, message);\n future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {\n\n @Override\n public void onSuccess(SendResult<String, String> result) {\n LogDto logDto = new LogDto();\n logDto.setStatus(StatusEnum.SUCCESS);\n logService.create(logDto)\n }\n @Override\n public void onFailure(Throwable ex) {\n LogDto logDto = …
Run Code Online (Sandbox Code Playgroud) 我创建了一个多模块maven项目,我正在尝试编写和执行特定模块的一些测试.我试图将所有测试代码放入一个单独的模块,但我想知道这是否是正确的方法,如果是这样,我如何设置maven构建/测试周期,所以mvn安装将使用这些测试?
任何人都可以解释一下标志是什么意思 - >在spock框架中?
对于exaple,我们有如下代码:
given:
UserService service = Stub()
service.save({ User user -> 'Michael' == user.name }) >> {
throw new IllegalArgumentException("We don't want you here, Micheal!")
}
Run Code Online (Sandbox Code Playgroud)
我知道这段代码是做什么的,但我不知道角色在这段代码中是如何签名的.
我从几天开始使用Spock,在所有的文档中,我看到测试都是用Groovy编写的.有没有办法在普通的Java类中使用Spock?因为我们要求只使用Java作为编程语言.
我的堆栈:
我们想开始在Spock测试框架中编写测试。我遵循了此方法,但是没有成功。当我尝试运行所有测试时,我的Spock测试未运行。
我能够进行一项测试。我可以在测试上“右键单击”并运行它。但是,如果我尝试运行整个groovy软件包(或Java软件包下的某些软件包),它将不会运行那些groovy测试。它不会运行意味着出现以下错误:
Jul 24, 2019 8:33:47 AM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to discover tests
java.lang.NoClassDefFoundError: org/junit/platform/engine/support/discovery/SelectorResolver
at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:69)
at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:168)
at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:155)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.engine.support.discovery.SelectorResolver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 8 more
Jul 24, 2019 8:33:47 AM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-vintage' failed …
Run Code Online (Sandbox Code Playgroud) spock ×10
groovy ×7
java ×4
spring-boot ×2
testing ×2
unit-testing ×2
geb ×1
jvm ×1
maven ×1
multi-module ×1
spring-kafka ×1