spock

Using Spock to mock private static final variables in Java

…衆ロ難τιáo~ 提交于 2019-12-21 09:24:18
问题 I'm trying to write some Spock tests with Groovy to test some Java code (specifically a servlet Filter). I have some private static and private static final variables that I would like to mock, but I can't determine if there is a way to do this. I know metaClass is available for methods, is there anything similar for variables? For instance, I have: public class MyFilter implements Filter { private static WebResource RESOURCE; private static final String CACHE_KEY = "key-to-be-used-for-cache"

Easiest way to read Excel files in groovy?

我们两清 提交于 2019-12-21 04:06:58
问题 Are there any warappers/utils available to read Excel files in Groovy. I am looking for something similar to Groovy SQL's rows function as shown in below spock test example. My intention is to use this for data driven testing using excel in Spock test framework import groovy.sql.Sql import spock.lang.* class DatabaseDriven extends Specification { @Shared sql = Sql.newInstance("jdbc:h2:mem:", "org.h2.Driver") // normally an external database would be used, // and the test data wouldn't have to

Can I override RESTClient default “HttpResponseException” response to >399 Return Codes?

人走茶凉 提交于 2019-12-21 03:37:50
问题 I'm using the Groovy RESTClient class to do write some (spock) Acceptance tests for Java WebServices I've been authoring. One frustration I've had is in testing the responses... 200 Status's are easy: when: def result = callServiceWithValidParams() then: result.status == 200 But with 400+ I'm forced to either wrap in a try-catch , or test for the HttpResponseException that RESTClient throws by default. when: callWithInvalidParams() then: def e = thrown(Exception) e.message == 'Bad Request'

Mock final class in Spock

荒凉一梦 提交于 2019-12-21 01:06:21
问题 Can spock mock final classes? If so, how? Search results brought up this gist, which would seem to imply so, but I can't find any examples of doing so. I've also found forum posts that say mocking final classes isn't supported. 回答1: This specification: @Grab('org.spockframework:spock-core:1.0-groovy-2.4') @Grab('cglib:cglib-nodep:3.1') import spock.lang.* class Test extends Specification { def 'lol'() { given: def s = Mock(String) { size() >> 10 } expect: s.size() == 10 } } ends with the

Using PowerMock with Spock

别来无恙 提交于 2019-12-20 14:19:57
问题 I have a class with a few static methods.I need to Mock these static methods. I know PowerMock does this,but I was not able to find any tutorials/materials that shed some light on "Spock+PowerMock" integration. I prefer Spock to Junit,hence the conundrum. Is there a way of getting these 2 frameworks to play ball?Any help is much appreciated.Sample code,even more so. Update: Current Status of the Approach Spock behaving weirdly 回答1: I was stuck here for a while too. After searching for hours,

Executing Specific Geb Tests according to environment

自闭症网瘾萝莉.ら 提交于 2019-12-20 10:55:52
问题 I have a set of Spec tests I am executing within a Grails Project. I need to execute a certain set of Specs when I am on local, and another set of Spec when I run the pre-prod environment. My current config is executing all my specs at the same time for both environements, which is something I want to avoid. I have multiple environments, that I have configured in my GebConfig: environments { local { baseUrl = "http://localhost:8090/myApp/login/auth" } pre-prod { baseUrl = "https://preprod

Spock: can an interaction defined in setup() be replaced in a test case?

笑着哭i 提交于 2019-12-18 18:37:50
问题 I'm struggling to understand something about Spock interactions in a Groovy unit test. I have the following types: public interface Bar { public String getMessage(); } public class Foo { private Bar bar; public void setBar(Bar bar) { this.bar = bar; } public String getMessage() { return bar.getMessage(); } } and I then wrote the following Groovy/Spock test: class FooSpec extends Specification { private Bar bar; private Foo foo; def setup() { bar = Mock(Bar) { getMessage() >> "hello" } foo =

How to test a Grails Service that utilizes a criteria query (with spock)?

混江龙づ霸主 提交于 2019-12-18 12:32:48
问题 I am trying to test a simple service method. That method mainly just returns the results of a criteria query for which I want to test if it returns the one result or not (depending on what is queried for). The problem is, that I am unaware of how to right the corresponding test correctly. I am trying to accomplish it via spock, but doing the same with any other way of testing also fails. Can one tell me how to amend the test in order to make it work for the task at hand? (BTW I'd like to keep

How to unit test logging error with Spock framework in groovy

浪尽此生 提交于 2019-12-18 04:14:53
问题 So I have a class that has a method that logs a message: class Car { private Logger logger = LoggerFactory.getLogger(Car.class); void startCar() { logger.error("car stopped working"); } } How can I test that the error was logged using the spock testing framework? class CarTest extends Specification { def "test startCar"() { given: Car newCar = new Car(); when: newCar.startCar(); then: // HOW CAN I ASSERT THAT THE MESSAGE WAS LOGGED??? } } 回答1: you could check for an invocation of error on the

Determine order of execution of Spock tests

旧街凉风 提交于 2019-12-18 03:56:19
问题 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. 回答1: 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