When I try to run the code below from Eclipse/STS I get the following exception:
Caught: groovy.lang.MissingMethodException: No signature of method:
groovy.json.JsonBuilder.message() is applicable for argument types:
(ie.makalu.scripts.TestJsonBuilder$_run_closure1) values:
[ie.makalu.scripts.TestJsonBuilder$_run_closure1#d05c13]
but when I run it from the shell it works fine. Can anyone suggest why that may be?
I've tried this in Groovy 1.8.6 and Groovy 2.0 using STS version 2.9.2
package ie.makalu.scripts
import groovy.json.JsonBuilder
def json = new JsonBuilder()
json.message {
header {
from('mrhaki') // parenthesis are optional
to 'Groovy Users', 'Java Users'
}
body "Check out Groovy's gr8 JSON support."
}
println json.toString()
I would advise against using Eclipse STS - it seems to me that too many people are constantly having issues with it. Instead I would install standard Eclipse or Eclipse for Java Development (anything from a eclipse community but not a specialized STS stuff) and install Groovy (and if you need Grails) plugin on top. It works much better and you are free to install any additional plugin that might not be working under STS version.
Also I would suggest to try IntelliJ, it has nice integration with Groovy (and Grails).
Related
I have built a complete groovy project with JPA repo for data persistence operations. Now I want to invoke the methods from the script in Mule 4 without changing anything in the groovy project.
eg. CustomerService.groovy (pseudo code)
import com.example.dao.CustomerDao.groovy
... other imports...
#Service
class CustomerService {
#Autowired
CustomerDao cDao
#Transactional
publid Customer createCustomer(Customer customer) {
return cDao.save(customer)
}
... other methods...
}
CustomerDao.groovy
import spring.JPA (the original import path may vary)
#Repository
class CustomerDao implements JPARepository<Customer, Integer> {
}
This project is working in Mule 3. In Mule 3 we have an Invoke component which could be used to invoke the methods from the groovy script. The Mule 4 the Invoke component is compatible only with Java and not groovy.
The Scripting module's 'Execute' component can invoke a groovy script but not sure how to invoke the method. Is the any work around for this in Mule 4?
Problems occured as of now
In 'Execute' component if I import another file I get the error as
Unalbe to resolve class com.example.dao.CustomerDao
# line 3 column 1,
import com.example.dao.CustomerDao.groovy
^
Found a solution for similar problem https://help.mulesoft.com/s/article/Compilation-exception-in-Mule4-x-when-using-Groovy-script-with-Import-statement but unable to implement it.
In the article, he developer had an issue with an apache dependency, which he/she could get from mvn repo. I am trying to import a groovy file which I have developed. So unable to add it in the dependency. I tried adding the groovy project in my local repo and fetch it but it didn't work. Moreover, when this Mule 4 application will be deployed on CloudHub it will have an issue as it won't be able to access my local repo.
Need a solution to add a spring-groovy project to Mule 4
Thanks :)
The problem is that the Groovy script executed by the Scripting Module is trying to import a class, which is not in Java's classpath. So it throws the error. The class is in another Groovy source file. What you can do is to compile the Groovy code into an actual Java class. You can try one the methods described in the Groovy documentation to integrate a Maven plugin with your Mule application pom.xml file.
Another alternative if you have a large number of Groovy classes is to create a separate project to build a Jar library, then use the solution from the KB article you mentioned.
I am using cucumber-jvm to perform some functional tests in Kotlin.
I have the standard empty runner class:
#RunWith(Cucumber::class)
#CucumberOptions(features=[foo],
glue=[bar],
plugin=[baz],
strict=true,
monochrome=true)
class Whatever
The actual steps are defined in another class with the #ContextConfiguration springframework annotation.
This class also uses other spring features like #Autowire or #Qualifier
#ContextConfiguration(locations=["x/y/z/config.xml"])
class MyClass {
...
#Before
...
#Given("some feature file stuff")
...
// etc
}
This all work fine in cucumber version 4.2.0, however upgrading to version 6.3.0 breaks things. After updating the imports to match the new cucumber project layout the tests now fail with this error:
io.cucumber.core.backend.CucumberBackendException: Please annotate a glue class with some context configuration.
It provides examples of what it means...
For example:
#CucumberContextConfiguration
#SpringBootTest(classes = TestConfig.class)
public class CucumberSpringConfiguration {}
Or:
#CucumberContextConfiguration
#ContextConfiguration( ... )
public class CucumberSpringConfiguration {}
It looks like it's telling me I can just add #CucumberContextConfiguration to MyClass.
But why?
I get the point of #CucumberContextConfiguration, it's explained well here but why do I need it now with version 6 when version 4 got on fine without it? I can't see any feature that was deprecated and replaced by this.
Any help would be appreciated :)
Since the error matches exactly with the error I was getting in running Cucumber tests with Spring Boot, so I am sharing my fix.
One of the probable reason is: Cucumber can't find the CucumberSpringConfiguration
class in the glue path.
Solution 1:
Move the CucumberSpringConfiguration class inside the glue path (which in my case was inside the steps package).
Solution 2:
Add the CucumberSpringConfiguration package path in the glue path.
The below screenshot depicts my project structure.
As you can see that my CucumberSpringConfig class was under configurations package so it was throwing me the error when I tried to run feature file from command prompt (mvn clean test):
"Please annotate a glue class with some context configuration."
So I applied solution 2, i.e added the configurations package in the glue path in my runner class annotation.
And this is the screenshot of the contents of CucumberSpringConfiguration class:
Just an extra info:
To run tests from command prompt we need to include the below plugin in pom.xml
https://github.com/cucumber/cucumber-jvm/pull/1959 removed the context configuration auto-discovery. The author concluded that it hid user errors and removing it would provide more clarity and reduce complexity. It also listed the scenarios where the context configuration auto-discovery used to apply.
Note that it was introduced after https://github.com/cucumber/cucumber-jvm/pull/1911, which you had mentioned.
Had the same error but while running Cucumber tests from Jar with Gradle.
The solution was to add a rule to the jar task to merge all the files with the name "META-INF/services/io.cucumber.core.backend.BackendProviderService" (there could be multiple of them in different Cucumber libs - cucumber-java, cucumber-spring).
For Gradle it is:
shadowJar {
....
transform(AppendingTransformer) {
resource = 'META-INF/services/io.cucumber.core.backend.BackendProviderService'
}
}
For Maven something like this:
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/services/io.cucumber.core.backend.BackendProviderService</resource>
</transformer>
</transformers>
A bit more explanation could be found in this answer
We have been building our application using groovy 2.3.6. Now because of some platform level issues we are advised to downgrade our groovy version to 2.2.1. I am facing no. of issues regarding this downgrade.
groovy is not able to infer the type of it variable in ver 2.2.1 so if i have code something like this
names.any { sliceName.endsWith(it) }
it gives me exception
[Static type checking] - Cannot find matching method java.lang.String#endsWith(java.lang.Object)
Secondly all the default method that i had used in collections no longer seem to exist
positions.any { it.primary }
groovy is unable to find the any method on list.
One way would be turn off static type checking, which will expose the code to a lot more runtime errors.
Is there any way to resolve these errors, without turning off static type checking. Also are these features only added in groovy 2.3.6 like default groovy methods and type inference for it variable
If you go back to an old version, old bugs will bite you.
Try giving the static compiler more of a hint
names.any { String it -> sliceName.endsWith(it) }
Apologies for the newbie question, but how do you install HTTPBuilder for Groovy?
I've added the http-builder-0.7.jar, http-builder-0.7-source.jar, and http-builder-0.7-javadoc.jar to GROOVY_HOME/lib.
Is there anything else I need to do? The HTTPBuilder website isn't clear.
Code run from GroovyConsole:
import groovy.grape.Grape
Grape.grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org')
I get this in response:
groovy.lang.MissingMethodException: No signature of method: static groovy.grape.Grape.grab() is applicable for argument types: (java.lang.String, java.lang.String, java.lang.String) values: [org.codehaus.groovy.modules.http-builder, http-builder, 0.7]
Possible solutions: grab(java.lang.String), grep(), grab(java.util.Map), grab(java.util.Map, [Ljava.util.Map;), wait(), dump()
EDIT 2:
#Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org')
Response:
java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpRequestBase
at ConsoleScript6.run(ConsoleScript6:4)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpRequestBase
... 1 more
The following example works for me out of the box:
#Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org')
println http
You need to remove any of the dependency jars you added directly to GROOVY_HOME\lib. Manually adding the jars there could create conflicts and cause these types of errors. Check to see if you have manually added the HttpClient libraries to the lib, remove them as well and try again.
EDIT: When using IntelliJ, I have been able to reproduce this behavior once. I already had a single #Grab annotation added to my Groovy script. When I added a second, it didn't seem to download or import the new library.
First of all, if you add a second #Grab, you need to wrap it in the #Grapes annotation like the following (my first mistake):
#Grapes([
#Grab(group='org.codehaus.gpars', module='gpars', version='1.2.1'),
#Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
])
After that, I found my solution here: Intellij IDEA not importing dependencies from #Grab in Groovy project, which explains than when using IntelliJ and you encounter this issue, try placing your cursor next to the #Grapes annotation and selecting Alt+Enter then choose the 'Grab the Artifacts' option.
java.lang.NoClassDefFoundError:
org/junit/internal/AssumptionViolatedException at
org.spockframework.runtime.JUnitSupervisor.error(JUnitSupervisor.java:92)
at
org.spockframework.runtime.BaseSpecRunner.invokeRaw(BaseSpecRunner.java:318)
at
org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:297)
at
org.spockframework.runtime.BaseSpecRunner.runSpec(BaseSpecRunner.java:90)
at
org.spockframework.runtime.BaseSpecRunner.run(BaseSpecRunner.java:81)
at org.spockframework.runtime.Sputnik.run(Sputnik.java:63) at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
I have a simple test case that calls a method,
def 'some test for empty Lists'(){
setup:
List<Object> clauses=new ArrayList<Object>()
when:
String someString=builder.buildString(clauses, "someName", "AND");
then:
noExceptionThrown()
someString==""
}
Assume builder is injected via spring. I can't make out why this happens. It happens only for a few test cases.
Any help is much appreciated :) I use spock 0.6 with groovy 1.8.
The solution for spock 1.0 with jUnit 4.12
As soon as I added hamcrest-core-1.3 to my test classpath the error disappeared.
What was the problem?
I observed the same error described in the question, but I managed to get spock 1.0 groovy 2.4 to work with junit-4.12.
The problem was that junit-4.12 depends on hamcrest.
In fact org.junit.internal.AssumptionViolatedException implements the interface org.hamcrest.SelfDescribing.
I've got the same error with spock tests using a simple Eclipse java project with these versions:
spock-core-1.0-groovy-2.3
junit-4.12
The exception was thrown only when there was a test failures, but was working for the successful ones
I've found that it was a Junit version compatibility matter. i resolved it using junit-4.7 and also tested the following versions:
junit-4.6 => ok
junit-4.5 => ok
junit-4.4 => ko
junit-4.0 => ko
Hope it helps
I had the same compatibility issue while trying to run my spocks in STS:3.6.4 and JBoss Developer Studio Version: 7.1.1.GA.
It was resolved using junit-4.7 instead of junit-4.12
Other instructions can be found at https://code.google.com/p/spock/wiki/GettingStarted#Eclipse