spock

How to refactor common Geb test sequences

≡放荡痞女 提交于 2019-12-01 22:36:16
Suppose I have multiple Geb/Spock tests that beings with logging in. For example: @Stepwise Class AddNewPictureSpec extends GebSpec { def "User at login page"() { given: "User beings from login page" to LoginPage } def "User gets redirected to Main page"() { given: "User at Login page" at LoginPage when: "User signs in" signIn "username", "pw" to MainPage then: at MainPage def "other test sequences follow...."() { } } And another test spec with the exact same start sequence: @Stepwise Class EditPictureSpec extends GebSpec { def "User at login page"() { given: "User beings from login page" to

Spock: mock a method with varargs

无人久伴 提交于 2019-12-01 21:05:48
This question is an offshoot of this Q&A: Test Groovy class that uses System.console() The problem: Spock framework incorrectly checks arguments of the mocked method with varargs. Steps to reproduce: 1) Create groovy project 2) Create an interface: interface IConsole { String readLine(String fmt, Object ... args) } 3) Create spock test: class TestInputMethods extends Specification { def 'test console input'() { setup: def consoleMock = GroovyMock(IConsole) 1 * consoleMock.readLine(_) >> 'validResponse' when: // here we get exception "wrong number of arguments": def testResult = consoleMock

Gradle sonarqube not recognizing groovy tests

*爱你&永不变心* 提交于 2019-12-01 20:58:56
I have a multi-language project with tests implemented using Java (JUnit) and Groovy (Spock). plugins { id "org.sonarqube" version "2.2.1" } apply plugin: 'idea' apply plugin: 'java' apply plugin: 'groovy' apply plugin: 'jacoco' ext { spockVersion = '1.1-groovy-2.4-rc-3' groovyVersion = '2.4.7' } group = "at.softwarecraftsmen.poc" version = "1.0.0-SNAPSHOT" repositories { mavenCentral() } dependencies { testCompile "junit:junit-dep:4.11" testCompile "org.codehaus.groovy:groovy-all:$groovyVersion" testCompile "org.spockframework:spock-core:$spockVersion" } jacoco { toolVersion = "0.7.8+" }

Java test class with many private methods

前提是你 提交于 2019-12-01 15:07:50
问题 I have a class that has the responsibility of importing contracts from a CSV to database. The class itself has only one public method that starts the import and the other methods are all private (because only the class itself will use, and they hold the logic). I'm starting to make tests for this class using Spock and there are many private methods, how should I test it? Should I turn them into public to test? Test only the main method, the public one? Whats the best? 回答1: In theory, your

How to get name of currently running test in spock?

一笑奈何 提交于 2019-12-01 15:05:23
In JUnit 3, I could get the name of the currently running test like this: public class MyTest extends TestCase { public void testSomething() { assertThat(getName(), is("testSomething")); } } How do I do this in spock? I would like to use the test name as a key in a shared resource so that tests don't interfere with each other. One solution is to leverage JUnit's TestName rule: import org.junit.Rule import org.junit.rules.TestName class MySpec extends Specification { @Rule TestName name = new TestName() def "some test"() { expect: name.methodName == "some test" } } This requires JUnit 4.7 or

Spock Framework: problems with spying

放肆的年华 提交于 2019-12-01 08:24:17
I have an issue with using Spy in Spock, it either doesn't work as it should or my understanding is wrong so I'm trying to clarify this. Consider this code (Java): public class CallingClass { public String functionOne() { //does stuff return "one"; } public String functionTwo() { String one = functionOne(); return "some string " + one; } } Now I want to test the fact that functionTwo calls functionOne as well as define the returned value from functionOne (imagine for instance if functionOne is really complicated and I don't want to execute it in my test just want to stub it and set it to

Asserting on a list of items in Spock

旧时模样 提交于 2019-12-01 06:06:42
Using Spock 0.7 with Grails 2.04. Trying to set up a testing environment. I need some help in regards to testing a list of objects. I have a list of location objects. I want to test a date on each of those objects. I am iterating over but not sure how to make the test fail if the dates are not equal. Is there a good way to test objects in a list? I have listed below my then block of code. then: weatherList != null weatherList.empty != null weatherList.size() == 3 weatherList.each { Calendar today = Calendar.getInstance(); today.clearTime() if(it.forecastDate != today) { return false } } Peter

Verify Spock mock with specified timeout

心已入冬 提交于 2019-12-01 04:42:05
In Mockito there is option to verify if mock method has been called, and specify timeout for this verification ( VerificationWithTimeout ), for example: verify(mock, timeout(200).atLeastOnce()).baz(); It there any equivalent to such functionality in Spock? th3morg I was trying to use PollingConditions to satisfy a similar scenario (which wasn't helpful), but instead found satisfaction in Spock's BlockingVariables . To verify that SomeService.method() is invoked at least once in function ClassBeingTested.method() within a given timeout period: def "verify an interaction happened at least once

Spock without maven or gradle

霸气de小男生 提交于 2019-12-01 04:26:24
I have a standard Java project that uses ant for building. I would like to add spock testing to this project without disrupting the current process. What is the minimum set of changes that would allow me to start to integrate spock testing into this project. There is not much available information on spock integration into legacy projects using ant. If you have a mix of jUnit and Spock tests in your test package (like we do), you can use the joint compilation feature of groovyc , something like this: <groovyc srcdir="${testSrcDir}" destdir="${testTarget}" classpathref="testCompileClasspath">

Can Spock Mock a Java constructor

邮差的信 提交于 2019-12-01 04:06:42
Trying to broaden the appeal of Spock at work and run into this issue. Actually trying to write Unit Tests for a Groovy class, but one that calls out to Java. A static method calls a private constructor. The code looks like: private MyConfigurator(String zkConnectionString){ solrZkClient = new SolrZkClient(zkConnectionString, 30000, 30000, new OnReconnect() { @Override public void command() { . . . } }); } "SolrZkClient" is from third party (Apache) Java library. Since it tries to connect to ZooKeeper, I would like to mock that out for this Unit Test (rather than running one internally as part