spock

Execute some action when Spock test fails

不打扰是莪最后的温柔 提交于 2019-11-29 06:57:27
I'd like to execute some action when Spock test fails. Specifically, take a screenshot. Is it possible? How to do it? Tomasz Dziurko Create a listener class class ExampleListener extends AbstractRunListener { def void error(ErrorInfo error) { println "Actual on error logic" } } then add it to each specification using implementation of IGlobalExtension that is executed for each Spec class GlobalSpecExtension implements IGlobalExtension { @Override void visitSpec(SpecInfo specInfo) { specInfo.addListener(new ExampleListener()) } } and finally create file named org.spockframework.runtime

Unit testing of a class with StaticLoggerBinder

别说谁变了你拦得住时间么 提交于 2019-11-29 04:08:39
I do have a simple class like this: package com.example.howtomocktest import groovy.util.logging.Slf4j import java.nio.channels.NotYetBoundException @Slf4j class ErrorLogger { static void handleExceptions(Closure closure) { try { closure() }catch (UnsupportedOperationException|NotYetBoundException ex) { log.error ex.message } catch (Exception ex) { log.error 'Processing exception {}', ex } } } And I would like to write a test for it, here is a skeleton: package com.example.howtomocktest import org.slf4j.Logger import spock.lang.Specification import java.nio.channels.NotYetBoundException import

How to find port of Spring Boot container when running a spock test using property server.port=0

别来无恙 提交于 2019-11-29 03:36:49
问题 Given this entry in application.properties : server.port=0 which causes Spring Boot to chose a random available port, and testing a spring boot web application using spock, how can the spock code know which port to hit? Normal injection like this: @Value("${local.server.port}") int port; doesn't work with spock. 回答1: You can find the port using this code: int port = context.embeddedServletContainer.port Which for those interested in the java equivalent is: int port = (

How to create Spock mocks outside of a specification class?

廉价感情. 提交于 2019-11-29 03:35:13
We combine Spock tests with Spring's @ContextConfiguration so we can build beans in a spring context and then use Spock for the actual testing. We would like to inject spock mocks into our spring beans. For Mockito there is an extension which allows you to do things like: <mockito:mock id="accountService" class="org.kubek2k.account.DefaultAccountService" /> and then reference this mock to other spring beans. There seems to be no such extension for Spock. Then again building this is probably not too much effort if you know how to create Mocks outside of the Specification class. The only way of

Determine order of execution of Spock tests

◇◆丶佛笑我妖孽 提交于 2019-11-29 03:25:16
Is there a way to set the order in which tests are executed within an Spock Specification? For example: class MySpec extends IntegrationSpec { def 'test A'... def 'test B'... } I want 'test A' to be execute always before 'test B' This is because I'm doing some functional tests with Geb and Spock and data is not rolled back between tests. You can use @Stepwise annotation on a spec and spock will run each of the test definitions of the Spec in the order they are specified. Look at this example . 来源: https://stackoverflow.com/questions/13575972/determine-order-of-execution-of-spock-tests

Mock static method with GroovyMock or similar in Spock

我与影子孤独终老i 提交于 2019-11-28 12:00:25
First-timer here, apologies if I've missed anything. I'm hoping to get around a call to a static method using Spock. Feedback would be great With groovy mocks, I thought I'd be able to get past the static call but haven't found it. For background, I'm in the process of retrofitting tests in legacy java. Refactoring is prohibited. I'm using spock-0.7 with groovy-1.8. The call to the static method is chained with an instance call in this form: public class ClassUnderTest{ public void methodUnderTest(Parameter param){ //everything else commented out Thing someThing = ClassWithStatic

Spock Stepwise - Keep running testsuite after single failure

痞子三分冷 提交于 2019-11-28 06:51:01
问题 When using the Spock @Stepwise annotation, is there any way to configure it to not fail the entire testsuite after a single test fails? 回答1: Decided to just create a new extension called @StepThrough. All I needed to do was subclass StepwiseExtension and take out the line of code that was failing the entire test suite. Pasted code below... StepThrough.groovy package com.test.SpockExtensions import org.spockframework.runtime.extension.ExtensionAnnotation import java.lang.annotation.ElementType

How to integration test auto configuration for a custom Spring Boot style starter library?

女生的网名这么多〃 提交于 2019-11-28 03:58:07
问题 I am writing a library to provide some functionality that is shared between multiple different Spring Boot applications that I work with. I would like to do something similar to the auto-configuration that is provided by the many Spring Boot starter libraries exist. That, or some other simple declarative way to integrate my library with the ApplicationContext of the apps using it. I have found some resources explaining how auto configuration works. I can figure out the above problem. However,

Difference between Mock / Stub / Spy in Spock test framework

試著忘記壹切 提交于 2019-11-27 17:01:04
I don't understand the difference between Mock, Stub, and Spy in Spock testing and the tutorials I have been looking at online don't explain them in detail. Attention: I am going to oversimplify and maybe even slightly falsify in the upcoming paragraphs. For more detailed info see Martin Fowler's website . A mock is a dummy class replacing a real one, returning something like null or 0 for each method call. You use a mock if you need a dummy instance of a complex class which would otherwise use external resources like network connections, files or databases or maybe use dozens of other objects

Integration Test with Spring Boot and Spock

China☆狼群 提交于 2019-11-27 11:13:31
What is the best way to run an integration test (e.g., @IntegrationTest ) with Spock? I would like to bootstrap the whole Spring Boot application and execute some HTTP calls to test the whole functionality. I can do it with JUnit (first the app runs and then the tests execute): @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = MyServer.class) @WebAppConfiguration @IntegrationTest class MyTest { RestTemplate template = new TestRestTemplate(); @Test public void testDataRoutingWebSocketToHttp() { def a = template.getForEntity("http://localhost:8080", String.class)