FAQ Database Discussion Community


Could not mock return value of service

grails,spock
i have a problem with an controller test. I try to mock a return value of a service method, but it do not return a specific object and call the service method anyway. The Test-Method: def "shouldReturnStatus"() { given: controller.repairService.getRepair('123465') >> repair; when: controller.status(); then: response.text == '{"currentStatus":"Repair was found.","repairFound":true}'...

How to test a grail service that has resultTransformer within withCriteria(Spock).?

unit-testing,grails,spock
In the below class, I just need RevShareFormula.withCriteria to return a result, but getting the exception in resultTransformer() method. Can anyone tell me how to Mock the below method so that i get some result from withCriteria Here is the class: class PartnerFinancialService { def getPartnerPayeeRevenuShareDetails(long partnerPayeeId, def contextTypeCode) {...

Testing for the Default Value

grails,spock
I am just starting to write tests for a Grails app and I have watched this and read this and would like to use the tests to check that the constraints have been set properly. Now, while at it, I would figure one should also test if the default value...

Is it possible to visit and modify AST nodes via spock extension?

java,groovy,spock
Spock has great feature: Argument Constraints. And I trying to transplant this feature for my task: // JSON body template for comparsion [ id: _ as Long, inn: !null, name: _ as String, addresses: 1.._ * [city: _ as String, street: _ as String, house: _ as Integer] ] Also...

Spock framework only one exception condition is allowed per 'then' block

groovy,spock
org.spockframework:spock-core:1.0-groovy-2.4 Gradle 2.4 Groovy 2.3.10 I am using gradle and spock framework to test my java code. However, the function I am testing can throw 3 different exceptions that I need to test for. However, in my then clause I list the 3 exceptions and if any of them are...

How to test the catch block code in Spock unit test Grails

unit-testing,grails,groovy,spock
Hi i am having my controller action like this and i want to test the catch block of my action tst with the help of Spock Controllers Unit test in Grails class AbcController{ def tst(Long id) { Abc abc = Abc.get(id) try{ ................ println "Try block" .................. } catch(DataIntegrityViolationException e){...

Gradle/Spock/Geb org.openqa.selenium.firefox.NotConnectedException on Linux but works on Windows

selenium,gradle,spock,geb
I have a gradle script which sets the geb.build.baseUrl to be a project property. This also does not work with a static url. This works fine on Windows, but on Linux I'm having the following issue: org.openqa.selenium.WebDriverException at LoginTest.groovy:9 Caused by: org.openqa.selenium.firefox.NotConnectedException at LoginTest.groovy:9 org.openqa.selenium.WebDriverException Caused by: org.openqa.selenium.firefox.NotConnectedException When run...

Spock verifying an exception thrown by mock together with mock interaction

groovy,mocking,spock
The problem I encountered is when I try to verify in the then block that an exception has been thrown, and that call on a mock has been made. Look at the setup below: class B { def b(A a) { a.a() } } class A { def a() {...

java.lang.AbstractMethodError: org.jboss.arquillian.config.descriptor.impl.EngineDefImpl.getDeploymentExportExploded()Ljava/lang/Boolean;

java,jboss,spock,wildfly-8,jboss-arquillian
When I try to deploy my artifact during integration test I'm getting this error. @RunWith(ArquillianSputnik) class WebServiceTest extends Specification { @Deployment static WebArchive createDeployment() { ShrinkWrap.createFromZipFile(WebArchive.class, new File("target/project.war")) } } where arquillian.xml is: <arquillian xmlns="http://jboss.org/schema/arquillian" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jboss.org/schema/arquillian...

Groovy version mismatch running spock test in Gradle project in Eclipse

eclipse,groovy,gradle,spock,eclipse-kepler
I have a Groovy and Gradle project in Eclipse Kepler with a few classes and a single Spock spec. I can run the Gradle build from the shell, and it works fine, including the Spock spec. I now wanted to try running the Spock spec in Eclipse, either in the...

How do I mock a transient service in my domain?

grails,spock
I am using Grails 2.4.4. I have a domain with a transient service defined like so: class CustomField { transient myRepoService // other properties here static transients = [ 'myRepoService' ] static constraints = { name validator: { val, obj -> if (obj.nameExists(val)) { return false } } } protected...

Grails 2.3.9 - Error: ClassNotFoundException: grails.plugin.spock.test.GrailsSpecTestType

grails,grails-plugin,spock,grails-2.3
Whenever I enter the grails command: test-app I get this error: Error executing script TestApp: java.lang.ClassNotFoundException: grails.plugin.spock.test.GrailsSpecTestType (Use --stacktrace to see the full trace) In my BuildConfig.groovy I have: grails.project.dependency.resolution = { ... plugins { ... test ":spock:0.7" } } I've tried replacing it with compile ":spock:0.7" . I've also...

Why Spring @Qualifier does not work with Spock and Spring Boot

java,spring,testing,spring-boot,spock
I'm trying to write a test for controller in the Spock. @ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = [Application.class, CreateUserControllerTest.class]) @WebAppConfiguration @Configuration class CreateUserControllerTest extends Specification { @Autowired @Qualifier("ble") PasswordEncryptor passwordEncryptor @Autowired UserRepository userRepository @Autowired WebApplicationContext context @Autowired CreateUserController testedInstance def "Injection works"() { expect: testedInstance instanceof...

spockframework: check expected result after every feature

groovy,automated-tests,spock,geb
I am using spockframework and geb for test automation. I would like to execute after every feature a simple check to be sure that no error dialogs are shown, I have added the following cleanup() method: def cleanup() { expect: $('.myErrrorDialogClass').isEmpty() } The code is executed after every feature but...

Grails Spock Unit Test - What is “1 *” and why “too few invocations”?

unit-testing,grails,spock
I see the following code within a Unit Test for a controller that I have inherited from others: when: "When the controller executes a registration" controller.index() then: "the signup should show registration again" 1 * controller.cService.getRegions() >> [] 1 * controller.dService.chkAvail(_) >> "AVAILABLE" 1 * controller.uService.createUser(_) >> { a-> throw...

grails create-controller junit tests rather than spock

grails,junit,spock
I am going through a few books on Grails 2 and all use junit in their unit tests but I notice that when I run create-controller, the auto generated unit tests are spock based. Is it possible to have the script crate junit tests instead? I notice the only option...

Geb WaitFor Element's class to refresh issue

spock,geb
I have a JSF/ADF page which has a "button" which starts of as <a id="pt1:r1:0:proBut" class="xfe p_AFDisabled" style="text-decoration:none;"> <span id="pt1:r1:0:pgl13" class="x26j x1a">Proceed</span> </a> Another button is then pressed which changes this button to enabled. <a id="pt1:r1:0:proBut" class="xfe" href="#" onclick="return false;" style="text-decoration:none;"> <span id="pt1:r1:0:pgl13" class="x26j x1a">Proceed</span> </a> You can see there...

Grails 2.4.4: services does not inject: integration test

grails,integration-testing,spock
I have service with dependency service like: class ParserService { def depService; private def parseLine(lineParts) { ... def set = depService.findItemByName(tmpModule.name);//depService == null ... I try to implement integration test like: @TestFor(ParserService) class ParserServiceTest extends IntegrationSpec { def "should not parse comment"() { when: ... def resultList = service.parseAnnotations(inputStream); resources.groovy:...

Using RestTemplate in Spring Boot Spock specification with customized Jackson

spring-mvc,spring-boot,spock,spring-test-mvc
I have a Spring Boot app that's using a customized Jackson ObjectMapper, and I would like to use the same customized mapper during testing. I used spring boot's method of automatically registering Module Beans, like this: @Configuration public class JacksonConfig { @Bean public Module jodaMoneyJackson() { SimpleModule jodaMoneyModule = new...

Array assertion in Spock

groovy,spock
I have a list of some objects - let's assume companies. Now I want to check if this list contains companies with some names but not taking order into consideration. Currently I'm using such construction: companyList.name.sort() == ["First", "Second"] Is there any operator in Spock or Groovy that allows me...

groovy spock mocking spring autowired beans

spring,unit-testing,groovy,autowired,spock
I have bean: @Service public class EQueueBookingService { @Autowired public EQueueBookingClient eQueueBookingClient; And I try to write some test for this bean EQueueBookingService using Spock. https://code.google.com/p/spock/wiki/SpockBasics My mock is class EQueueBookingServiceTest extends Specification { @Autowired EQueueBookingService testedService; EQueueBookingClient eQueueBookingClient = Mock(EQueueBookingClient); def setup() { testedService.eQueueBookingClient = eQueueBookingClient; } and test...

Spock's @Narrative and @Title annotations

groovy,spock
Spock provides @Narrative and @Title annotations that you can use to provide a class-level description of your test, e.g. @Narrative('description of spec') @Title('title of spec') class ExampleSpec extends Specification { // tests omitted } What is the difference between these two? In other words, why do we need both of...

Execute Spock Test in Different Environments

groovy,spock
I have a Selenium test, which is executed with the help of Spock framework. In general it looks like this: class SeleniumSpec extends Specification { URL remoteAddress // Address of SE grid Capabilities caps // Desired capabilities WebDriver driver // Web driver def setup() { driver = new RemoteWebDriver(remoteAddress, caps)...

Spock Framework: problems with spying

java,unit-testing,groovy,spock,spock-spy
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...

Spock - @Subject annotation and util | helper class that's being tested

unit-testing,groovy,spock
I got a question regarding convention of tests in Spock. Let us say we got A class to test, which is util class with static methods. Classic way to test it, would be A.method() in each test method. However, in Spock we got @Subject annotation (and also Groovy let us...

Spock @Unroll annotation

grails,grails-2.0,spock
During a recent code review, the question came up regarding whether the @Unroll annotation belongs at the class level or the method level. The class in question requires @Unroll on most of its methods but not all of them. Does declaring @Unroll incur any performance penalties if declared at the...

Getting “Too few invocations” on unit test with spock

spock
For simplicity let's take a very simple class: public class TestingClass { public void method1(){ System.out.println("Running method 1"); method2(); } public void method2(){ System.out.println("Running method 2"); } } Now I'm writing a simple test, which checking that when we invoke method1(), method2() is invoked: class TestingClassSpec extends Specification { void...

Validate() returns false but it has been saved in the database

grails,gorm,spock
I am starting to use grails and I have stumble into a behaviour that I don't understand. Given this domain class class Team { String name Date since Date until static hasMany = [teamMembers:Player] static constraints = { name nullable: false, blank: false, unique: true until nullable: true until(validator: {...

Gradle Plugin Spock Testing

gradle,spock
I'm writing a plugin for Gradle 2.3 which includes Groovy 2.3.9, and I am attempting to test it using Spock 1.0-groovy-2.3. I'm getting the following error because Spock is trying to use Groovy 2.3.10. The important bit. groovy.lang.GroovyRuntimeException: Conflicting module versions. Module [groovy-all is loaded in version 2.3.9 and you...

Geb/Spock login each def

spock,geb
Is there a easy way in Geb/Spock to ensure a login happens before all functional tests? For instance my login test looks like def "login"() { when: to Login and: login(username,password) then: at Dashboard where: username | password "X" | "X" } This is quite alot of code to put...

Groovy metaclass add multiple methods based on invocation order not mock

groovy,spock
I am trying to overwrite method Call on the groovy sql class and i am able to do it.But i need to have different implementations based on order. Sql.metaClass.call = {String sql, List params, Closure c -> c(mockResultSet)} //first time should call this method Sql.metaClass.call = {String sql, List params,...

Failing to save during Test on Controller

unit-testing,grails,testing,grails-2.0,spock
I have been struggling with this for quite sometime and it seems like a common problem, with no solution that worked for me. What I am trying to do is test the controller which calls a save(), and inside the save() the method calls a service to save the employee...

Converting a mock to JSON in Spock

json,grails,spock
One of the objects I've mocked must be converted into JSON but Spock does not seem to support the mocking of convertions. How can I choose which JSON will be returned? Example of what I would like to achieve: def "convert as JSON"() { when: def product = Mock(Product) println(product...

grails spock test mock CommonsMultipartFile

unit-testing,grails,groovy,spock
I've following old method written in code, which is for accessing request object in service class such as: def someServiceMethod() { .... def webUtils = WebUtils.retrieveGrailsWebRequest() def request = webUtils.getCurrentRequest() MultipartHttpServletRequest mpr = (MultipartHttpServletRequest) request CommonsMultipartFile file = (CommonsMultipartFile) mpr.getFile("file") .... } This is my unit test code for serivce...

Write Spock test cases for Spring boot application

java,spring,groovy,spock
I am working on spring boot application. I have to write test cases for it. I haven't written test cases before, so someone suggested using spock framework for it. I explored spock and i think it is more related to groovy language. Can i write spock test cases for my...

Grails, Integration Testing, mocking a bean method while testing a service with Spock

grails,integration-testing,spock
I'm having an issue performing an integration test in Grails 2.4.5. The service I want to test looks like this: class MyService { def myBean def serviceMethod() { myBean.beanMethod() } } where myBean is defined in resources.groovy: beans = { myBean(MyBeanImpl) } And here comes the integration test: MyServiceIntegrationSpec extends...

How to use EqualsVerifier in a Spock test

testing,junit,spock,equalsverifier
I have been writing my tests using spock. But to test Equals Hashcode contracts, I am trying to use EqualsVerifier. So my test code looks like: def "test equals hashcode contract"() { EqualsVerifier.forClass(Content.class).verify(); } But this does not look like its running with spock. How can I workaround this? I...