Create a task where src/test/groovy be able to access src/test/java

Hello

I am working with Gradle and about testing with Spock Framework (Groovy)

I have in my project:

  • src/main/java
  • src/main/resources
  • src/test/java
  • src/test/resources
  • src/test/groovy (For Spock)

I have no problems.

In src/test/groovy I have my Test classes written on Groovy, all compile and pass. For some test classes exist or use some UtilsTest classes located in other package.

If I move the util package from src/test/groovy to src/test/java and of course change the file from .groovy to .java. I can’t compile anymore. Practically src/test/groovy can’t access src/test/java about compilation.

But If I move the util package from src/test/java to src/test/groovy (keeping the .java extension) all work again.

Through Gradle, is possible create a task based on Test where src/test/groovy can access src/test/java?

I don’t want have in src/test/groovy a .java file, they should be located on src/test/java.

Remember they are utils classes only created for testing. So move them in to src/main/java is not an option.

Thanks

My guess is the class you moved to src/test/java depends on something in src/test/groovy. In this case Groovy code can access Java code but not the other way around. The exception is if you put both Java and Groovy code in src/test/groovy then we automatically utilize joint compilation.

Thanks by the reply Mark

Sorry by my delay.

My guess is the class you moved to src/test/java depends on something in src/test/groovy

No, really sure that Groovy code depends on Java.

What I need is have test code on src/test/java and src/test/groovy

It assuming the following:

  • In src/test/java I have Test code based in Java which would use some Utils Test classes written in Java
  • In src/test/groovy have Test code based in Groovy which would use the same Utils Test classes written in Java and located on src/test/java

Test Java code does not use Spock and Groovy Code does use Spock.

What extra configuration in the IDE (STS/Eclipse) or in Gradle I need to let any code from src/test/groovy see any code from src/test/java and viceversa.

Thanks

Is the problem you’re having only in the IDE? Are you able to build with Gradle via the command line? What you are describing should work just fine however I’m not certain how STS handles this.

Hi Mark

it fails for both scenarios, with IDE and command line.

I am with the impression to do something extra about configuration.

If you have suggestions… let me know…

Thanks!

What does the compilation error say when using the Gradle build?

Hi pledbrook

Check the following (through terminal):

gradle test
TOMCAT_HOME: /Users/manueljordan/whiteroom/apache/tomcat/8.0.26
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava
/Users/manueljordan/whiteroom/sts-workspace-laboratorio/Laboratory/web/web-25/src/test/java/com/manuel/jordan/rest/persona/support/PersonaRestControllerSupportTest.java:24: error: cannot find symbol
	public static final String ROOT_URI_XMLEXTENSION = PersonaRestControllerSupport.ROOT_URI + ControllerSupportTest.XML_EXTENSION;
	                                                                                           ^
  symbol:   variable ControllerSupportTest
  location: class PersonaRestControllerSupportTest
/Users/manueljordan/whiteroom/sts-workspace-laboratorio/Laboratory/web/web-25/src/test/java/com/manuel/jordan/rest/persona/support/PersonaRestControllerSupportTest.java:29: error: cannot find symbol
	public static final String ROOT_URI_JSONEXTENSION = PersonaRestControllerSupport.ROOT_URI + ControllerSupportTest.JSON_EXTENSION;
	                                                                                            ^
  symbol:   variable ControllerSupportTest
  location: class PersonaRestControllerSupportTest

....
more and more

In this case Java depends in other .java classes located on src/test/groovy but it can’t be reach it from src/test/java.

It happens in vice versa if I move all the .java classes from src/test/groovy to src/test/java. Therefore the .groovy files located on src/test/groovy can’t reach the .java classes from src/test/java.

I am with the impression something is missing, some extra configuration in the IDE or through Gradle.

Let me know if you need more data (the same error stack trace shown above appears in the IDE too, of course in the Console view.)

Thanks

This is expected. Code in src/test/java can’t see the code in src/test/groovy, because the Java compilation happens first. However, your original report said that code in src/test/groovy couldn’t see the classes from src/test/java, which shouldn’t be the case.

Hi pled brook

This is expected. Code in src/test/java can’t see the code in src/test/groovy, because the Java compilation happens first.

Ok, I see it has sense. And it is reflected about the order of execution of the tasks that the console/terminals show when I execute the gradle test command

However, your original report said that code in src/test/groovy couldn’t see the classes from src/test/java, which shouldn’t be the case

In that situation, I moved all the packages with any .java file to src/test/java. Totally sure through the View Explorer that I can see in src/test/groovy only packages containing .groovy files.

Now I get:

com.manuel.jordan.rest.persona.PersonaFindOneRestControllerTest  findOneById accept json xml FAILED
    groovy.lang.MissingPropertyException at PersonaFindOneRestControllerTest.groovy:302

com.manuel.jordan.rest.persona.PersonaSaveOneRestControllerTest  initializationError FAILED
    java.lang.NullPointerException

com.manuel.jordan.rest.persona.PersonaUpdateOneRestControllerTest  initializationError FAILED
    java.lang.NullPointerException

97 tests completed, 97 failed
:test FAILED

Where that 302 line is

uriRequestReport << [ControllerSupportTest.findOneByIdTemplateFactory(PersonaRestControllerSupport.URI_ID, ControllerSupportTest.ID_URI_VALUE)]

That classes are located on src/test/java

If I use the grade test --info I get

com.manuel.jordan.rest.persona.PersonaFindOneRestControllerTest > findOneById accept json xml FAILED
    groovy.lang.MissingPropertyException: No such property: ControllerSupportTest for class: com.manuel.jordan.rest.persona.PersonaFindOneRestControllerTest
        at com.manuel.jordan.rest.persona.PersonaFindOneRestControllerTest.findOneById accept json xml(PersonaFindOneRestControllerTest.groovy:302)

com.manuel.jordan.rest.persona.PersonaFindOneRestControllerTest STANDARD_ERROR
    Feb 17, 2016 8:21:48 AM org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSiteNoUnwrap invoke
    INFO: com.athaydes.spockframework.report.internal.HtmlReportCreator failed to create report for com.manuel.jordan.rest.persona.PersonaFindOneRestControllerTest
    java.lang.ArrayIndexOutOfBoundsException: 0
    	at org.spockframework.runtime.extension.builtin.UnrollNameProvider.evaluateExpression(UnrollNameProvider.java:74)
    	at org.spockframework.runtime.extension.builtin.UnrollNameProvider.nameFor(UnrollNameProvider.java:53)
    	at org.spockframework.runtime.extension.builtin.UnrollNameProvider.getName(UnrollNameProvider.java:44)
    	at org.spockframework.runtime.extension.builtin.UnrollNameProvider.getName(UnrollNameProvider.java:30)

and more

com.manuel.jordan.rest.persona.PersonaUpdateOneRestControllerTest > initializationError FAILED
    java.lang.NullPointerException: Cannot get property 'featureRuns' on null object

Some ideas?

Thanks in advance.

Did you import ControllerSupportTest? That would explain the MissingPropertyException. I would expect a different error if the class was imported but not found.

Also, note that these are runtime issues, not compilation ones.

Hi

Did you import ControllerSupportTest? That would explain the MissingPropertyException

Yes, I did check that a lot of times… totally sure the correct packages have been imported

Also, note that these are runtime issues, not compilation ones.

I think that is a secondary consequence from the “missing” import packages.

Well… now all is working…

For one .groovy file (among others)

Just how playing from:

import com.manuel.jordan.domain.*
import com.manuel.jordan.context.*
import com.manuel.jordan.rest.support.*
import com.manuel.jordan.domain.factory.*
import com.manuel.jordan.controller.support.*
import com.manuel.jordan.rest.persona.support.*

to

import com.manuel.jordan.domain.*;
import com.manuel.jordan.context.*;
import com.manuel.jordan.rest.support.*;
import com.manuel.jordan.domain.factory.*;
import com.manuel.jordan.controller.support.*;
import com.manuel.jordan.rest.persona.support.*;

How you can see, I just added the ;

and all work.

Wondered … some idea?

Even more interesting, I executed gradle clean (how I did do many times before) and I’ve removed the ; from the import declarations (assuming that the same exceptions should appear again…) and all still work fine.

I am confused…

Thanks to both by your support.