Running parameterized Junit Test from Ant throws IllegalArgumentException - ant

I'd like to run the JUnit tests in our project from the command line using an Ant target. Up until now we ran the tests manually from Eclipse without any issues using the embedded JUnit in Eclipse.
After finally having figured out how the set the classpath I now get failed Tests for all classes that use the Parameterized Runner from JUnit 4.11.
While running the test target (ant test) the only output is "FAILED" after the name of the Testclass, even with option -v.
Generating testreports shows the following Exception:
java.lang.IllegalArgumentException: wrong number of arguments
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
What does this mean?
The version are:
- JDK 1.6
- ant 1.9.3
- JUnit 4.11
My build.xml is over 400 lines long so I'm not sure if it makes sense to post it here. Let me know if you need parts of the build.xml.
Update 13.05.2015: Here's a sample section from on of our JUnit Test classes that fail. The #Parameters section contains only one entry which is pretty useless in this case but this class still fails when run from ant.
#Parameters
public static Iterable<String[]> testData() {
return Arrays
.asList(new String[][] { { "a-rules-filename" } });
}
#Parameter
public String RULES_FILE_NAME;
Our Test class is annotated like this:
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
#RunWith(Parameterized.class)
public class OurRulesTest {

The Exception is thrown due to a missing constructor in the test class.
It seems that when using the Parameterized Runner you need to specify a constructor with as many arguments as you have parameters.
For some reason this works in Eclipse without specifying a constructor.

Related

cucumber test, jhipster and oauth: ClientRegistrationRepository bean not found

I have a problem with running Cucumber tests in a khipster project (I can reproduce it with jhipster as well) when I configure it to use oauth2.
I create the project with the following configuration file (I call it mono.jdl):
application {
config {
applicationType monolith
authenticationType oauth2
baseName helloworld
buildTool maven
packageName com.example.helloworld
testFrameworks [cucumber]
}
entities *
}
I generate the project with a command: khipster import-jdl mono.jdl.
I create a very simple Cucumber test. I create a feature file (src/test/features/kuku/kuku.feature):
Feature: just a test
Scenario: my scenario
And one two three
and a file with steps (src/test/kotlin/com/example/helloworld/cucumber/stepdefs/KukuStepDefs.kt):
package com.example.helloworld.cucumber.stepdefs
import io.cucumber.java.en.When
class KukuStepDefs : StepDefs() {
#When("one two three")
fun magic() {
println("four five six")
}
}
I try to run the integration test with a command ./mvnw integration-test. However, it fails with a following error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.helloworld.web.rest.LogoutResource required a bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' that could not be found.
The following candidates were found but could not be injected:
- Bean method 'clientRegistrationRepository' in 'OAuth2ClientRegistrationRepositoryConfiguration' not loaded because OAuth2 Clients Configured Condition registered clients is not available
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' in your configuration.
How can I fix this problem?
The solution is to find CucumberContextConfiguration class. It contains such annotation:
#ContextConfiguration(classes = [HelloworldApp::class])
We must change it to:
import com.example.helloworld.config.TestSecurityConfiguration
(...)
#ContextConfiguration(classes = [HelloworldApp::class, TestSecurityConfiguration::class])

Load-test Spock spec, running it concurrently multiple times

I have written integration tests in Spock that I would like to reuse for load testing. I haven't had any luck with programmatically executing Spock tests. I need to run an entire spec as a single unit which will be executed concurrently to create load.
Previous posts on stack overflow on this topic are obsolete (I tried a bunch of them with no luck).
Example Spec:
class MySpec extends Specification {
def 'test'() {
expect: 1+1 == 2
}
}
I want to be able to run this in something like (executed, succeeded and failed are AtomicInteger):
executor.submit(() -> {
try {
executed.addAndGet(1);
Result result = mySpecInstance.run() // <-- what should this be.
if (result.wasSuccessful()) {
succeeded.addAndGet(1);
} else {
failed.addAndGet(1);
log.error("Failures encountered: {}", result.getFailures());
}
} catch (RuntimeException e) {
log.error("Exception when running runner!", e);
failed.addAndGet(1);
}});
I've tried the answer in this post which throws
Invalid test class 'my.package.MySpec':
1. No runnable methods]
I tried using the new EmbeddedSpecRunner().run(MySpec.class) which throws
groovy.lang.MissingMethodException: No signature of method: spock.util.EmbeddedSpecRunner.runClass() is applicable for argument types: (Class) values: [class my.package.MySpec]
Possible solutions: getClass(), metaClass(groovy.lang.Closure)
I am using JDK8 with Groovy 3.0.4 and spock-2.0-M3-groovy-3.0 (spock-junit4).
Update:
The answer from post works with Groovy-2.4, Spock-1.3 but not with Groovy-3.0 and Spock-2.0.
Thanks.
Your error did not occur because you used the wrong Spock version, by the way. You can use module spock-junit4 if you want to run the old JUnit 4 API. I just tried, the method works in Spock 1 and still in Spock 2, even though you maybe should upgrade to something that does not rely on an older API and a compatibility layer.
Your error message is simply caused by the fact that you copied & pasted code from the other answer without fixing it. The guy there wrote MySuperSpock.Class which causes the error because if must be MySuperSpock.class with a lower-case "C" or under Groovy simply MySuperSpock because the .class is optional there.
The error message even proves that you had JUnit 4 on the class path and everything was fine, otherwise the code importing JUnit 4 API classes would not have compiled in the first place. And the error message also explains what is wrong and suggests a solution:
Exception in thread "main" groovy.lang.MissingPropertyException: No such property: Class for class: de.scrum_master.testing.MyTest
Possible solutions: class
See? Class MyTest does not have any property called Class. And one possible solution (in this case even the correct one) is to use .class. This gives you a hint. BTW, the syntax MyTest.Class looks like an inner class reference or maybe a property reference to the compiler (to me too).
Update: I just took a closer look and noticed that the solution from the other question you said was working for Spock 1.3 actually compiles and runs, but the JUnit Core runner does not really run the tests. I tried with tests that print something. Furthermore, the result reports all tests as failed.
For simple cases you could use Spock's EmbeddedSpecRunner which is used internally to test Spock itself. Under Spock 1.x it should be enough to have JUnit 4 on the test class path, under Spock 2 which is based on JUnit 5 platform, you need to add these dependencies too because the embedded runner uses them:
<properties>
<version.junit>5.6.2</version.junit>
<version.junit-platform>1.6.2</version.junit-platform>
<version.groovy>3.0.4</version.groovy>
<version.spock>2.0-M3-groovy-3.0</version.spock>
</properties>
<!-- JUnit 5 Jupiter platform launcher for Spock EmbeddedSpecRunner -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${version.junit-platform}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-testkit</artifactId>
<version>${version.junit-platform}</version>
<scope>test</scope>
</dependency>
Then you can run a test like this:
def spockRunner = new EmbeddedSpecRunner()
def spockResult = spockRunner.runClass(MyTest)
println "Tests run: " + spockResult.runCount
println "Tests ignored: " + spockResult.ignoreCount
println "Tests failed: " + spockResult.failureCount
BTW, the *Count getter methods are deprecated in Spock 2, but they still work. You can replace them by newer ones easily, I just wanted to post code which runs unchanged in both Spock versions 1.x and 2.x.
Update 2: If you want to run the same test e.g. 10x concurrently, each in its own thread, in Groovy a simple way to do that is:
(1..10).collect { Thread.start { new EmbeddedSpecRunner().runClass(MyTest) } }*.join()
Or maybe a bit easier to read with a few line breaks:
(1..10)
.collect {
Thread.start { new EmbeddedSpecRunner().runClass(MyTest) }
}
*.join()
I am assuming that you are familiar with collect (similar to map for Java streams) and the star-dot operator *. (call a method on each item in an iterable).

GroovyObject class not found on Tomcat 8 with war of Grails 3.2.0

I deployed a Grails 3.2.0 WAR on Tomcat 8.5.6 and JDK 1.8.0_91 with a simple controller having following code:
package com.test
class MailController {
static responseFormats = ['json']
def index() {
Map headers = (request.headerNames as List).collectEntries { // It fails on this line
return [(it): request.getHeader(it)]
}
println "Incoming email $headers"
render status: 200
}
}
This code fails with the following exception:
Caused by: java.lang.NoClassDefFoundError: groovy/lang/GroovyObject
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.lang.ClassLoader.defineClass(ClassLoader.java:642)
at groovy.util.ProxyGenerator.instantiateDelegateWithBaseClass(ProxyGenerator.java:225)
at groovy.util.ProxyGenerator.instantiateDelegateWithBaseClass(ProxyGenerator.java:193)
at groovy.util.ProxyGenerator.instantiateDelegate(ProxyGenerator.java:185)
at groovy.util.ProxyGenerator.instantiateDelegate(ProxyGenerator.java:181)
at org.grails.web.converters.ConverterUtil.invokeOriginalAsTypeMethod(ConverterUtil.java:161)
at org.grails.web.converters.ConvertersExtension.asType(ConvertersExtension.groovy:56)
at com.test.MailController.index(MailController.groovy:7)
at org.grails.core.DefaultGrailsControllerClass$MethodHandleInvoker.invoke(DefaultGrailsControllerClass.java:222)
at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:187)
at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
... 14 common frames omitted
Caused by: java.lang.ClassNotFoundException: groovy.lang.GroovyObject
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
... 27 common frames omitted
Before building the WAR file, I've changed the embedded tomcat to provided in build.gradle and also commented the groovy-ant dependency related to grails-core#10196
I see a answer here but that didn't worked and the above code is working fine when we run via grails run-app.
Update
I shorted down the issue. It is failing on this part only request.headerNames as List
I am pretty sure the problem is with the use of "as List". Mostly because Grails will overwrite Groovy's asType implementation which makes the "as X" coercion syntax work.
Grails does this to add support for things like JSON for marshalling known Grails types to web transport formats.
Unfortunately, in doing so Grails also breaks any asType function you might have declared yourself. Or in this case Groovy itself already declared for converting an Enumeration into a List.
It's quite annoying as Grails is effectively breaking existing contracts here and forcing you to modify upstream code to allow it to run on Grails.
That or dump Grails because it doesn't play nice with perfectly valid Groovy code.
I believe replacing "as List" with .asType(List) won't even fix the issue as you're still invoking the same code. At best you could try .collect([]) {it} instead. It may not be necessary to add the empty array as the first argument to collect.

Grails 3 database-migration-plugin initialization error

I recently added database-migration-plugin to my grails 3.0.11 app. The problem is when I try to run-app I get a following error:
ERROR grails.boot.GrailsApp - Application startup failed
Exception in thread "main" org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'springLiquibase_dataSource':
Invocation of init method failed; nested exception is liquibase.exception.ChangeLogParseException:
java.lang.IllegalArgumentException: Script text to compile cannot be null!
Looks like it can't find changelog.xml in my grails-app/migrations folder. My build.gradle file contains:
buildscript {
dependencies {
classpath "org.grails.plugins:database-migration:2.0.0.RC1"
}
}
and
sourceSets {
main {
resources {
srcDir 'grails-app/migrations'
}
}
}
I also added the following lines in my application.groovy file:
grails.plugin.databasemigration.updateOnStart = true
grails.plugin.databasemigration.updateOnStartFileNames = ['changelog.xml']
I would by very gratefull for any advice how to make database-migration-plugin work properly.
Edit:
I created changelog.xml file using $grails dbm-create-changelog command
I also added to build.gradle (as suggested by $grails plugin-info database-migration command):
dependencies {
compile "org.grails.plugins:database-migration:2.0.0.RC1"
}
then I changed it to (following official documentation):
dependencies {
runtime "org.grails.plugins:database-migration:2.0.0.RC1"
}
and then (as suggested by manual for startup error) I forced liquibase:
dependencies {
compile 'org.liquibase:liquibase-core:3.3.2'
runtime 'org.grails.plugins:database-migration:2.0.0.RC1'
}
and
dependencies {
compile 'org.liquibase:liquibase-core:3.3.2'
compile 'org.grails.plugins:database-migration:2.0.0.RC1'
}
The problem still remains: java.lang.IllegalArgumentException: Script text to compile cannot be null!
We ran into the same problem when upgrading to Grails 3.
A look into the code of the grails-database-migration plugin made clear that the configuration parameter is changed from a list updateOnStartFileNames to a single value updateOnStartFileName.
So when you change your config from
grails.plugin.databasemigration.updateOnStartFileNames = ['changelog.xml']
to
grails.plugin.databasemigration.updateOnStartFileName = 'changelog.xml'
it should work again.
I ran into a similar error. In my case we had some lookup tables where we were populating using a hand crafted script which was included into the main changelog.groovy like:
include file: 'data/001-tablex-data.groovy'
except the file name was incorrect - it should have been 002-... instead. The error is basically the same, but there is no reporting to indicate which included file is not being found/parsed, which is a pain. So if you have manually included files, then look for incorrectly named ones in addition to checking the top-level changelog.groovy or changelog.xml
Ok, I finally found a solution. Maybe it will help someone someday. So what I did was simply delete changelog.groovy (i switched from XML to Groovy) file. Then I generated a new one with $grails dbm-create-changelog changelog.groovycommand. As a final step I run $grails dbm-changelog-sync and everything started to work just fine.
I was facing this issue too and in my case, the problem was the order of that block in build.gradoe
sourceSets {
main {
resources {
srcDir 'grails-app/migrations'
}
}
}
It MUST be before the bootRun, like the below code.
sourceSets {
main {
resources {
srcDir 'grails-app/migrations'
}
}
}
bootRun {
jvmArgs(
'-Dspring.output.ansi.enabled=always',
'-noverify',
'-XX:TieredStopAtLevel=1',
'-Xmx1024m')
sourceResources sourceSets.main
String springProfilesActive = 'spring.profiles.active'
systemProperty springProfilesActive, System.getProperty(springProfilesActive)
}
If you put sourceSets after bootRun your application will not find the migrations file.
Make sure:
You have set up the changelog, i.e., the file grails-app/migrations/changelog.xml exists and is valid.
How you do this depends on your situation. The plugin's documentation has a section for how to create the file initially.
Your datasource is set up to use the database that changelog.xml applies to.
Another potential cause for this problem that we had run into was incorrect capitalization. If changelog.groovy references path/someFile.groovy but the actual name is path/somefile.groovy then you will get this error. Make sure the path name capitalization matches.

Grails 2.3.4 War not correctly working in Tomcat 7.0.47

Running the application with grails run-app works fine but after deploying in Tomcat 7 I get following error.
groovy.lang.MissingMethodException:
No signature of method: static com.digithurst.hdspro.web.Responder.respond()
is applicable for argument types: (ResourceListCmd, QueryCmd, groovy.util.ConfigObject)
values: [ResourceListCmd#5c380e, ...]
Possible solutions: respond(HttpResource, java.lang.Object, java.lang.String)
As already said, this works outside of Tomcat. The way the method is called is exactly as it is implemented. The ResourceListCmd implements the interface HttpResource which makes it a perfect fit. This error also occurs if the first parameter is null.
groovy.lang.MissingMethodException:
No signature of method: static com.digithurst.hdspro.web.Responder.respond()
is applicable for argument types: (null, QueryCmd, groovy.util.ConfigObject)
values: [null, ...]
Possible solutions: respond(HttpResource, java.lang.Object, java.lang.String)
More on the environment:
Windows 7 64 Bit
Java 7 U45 x86
Grails 2.3.4
Tomcat 7.0.47
I have already cleaned the .grails and .m2 folders in the user directory and performed a grails clean berfore creating the war file.
[Edit after answer of H3rnst]
Controller:
def index() {
try {
ResourceListCmd configs = configService.search()
respond Responder.respond(configs, new QueryCmd(level: 'list'),
grailsApplication.config.grails.serverURL)
}
catch (Exception e) {
render status: INTERNAL_SERVER_ERROR
}
}
ResourceListCmd:
interface HttpResource {
...
}
abstract class AbstractHttpResource implements HttpResource {
...
}
class ResourceListCmd extends AbstractHttpResource {
...
}
Responder:
class Responder {
static def respond(HttpResource resource, def query, String serverURL) {
...
}
}
Your war (or tomcat server classpath) contain a duplicate or wrong version of jar that contains the class com.digithurst.hdspro.web.Responder. (The version of the class you are using developing an launching with run-app is different from the one tomcat load running your war)
You could try to unpack the war end verify the version of the problematic jar and/or use a tool like jarscan to scan for duplicate classes.
You could even try to use the command dependecy-report and search for duplicate injection of the same lib. Probably two different plugins your are using are incorporating to differente versions of the same lib causing the problem.
marko's suggestion doing run-war actually gave the final clue to solving this thing. It wasn't a problem with Tomcat but rather the environment the app was running in. run-app as well as run-war both use the "development" environment by default and therefore it worked. Only in "production" it did not, which is what is used when deployed to Tomcat.
The actual culprit was part of the configuration and the error message was right, although unexpected. I'm calling the Responder.respond() method with grailsApplication.config.grails.serverURL. The serverURL was only set in "development" mode but not in "production". That's why Groovy/Java complained about the method signature:
(ResourceListCmd, QueryCmd, groovy.util.ConfigObject) vs (HttpResource, java.lang.Object, java.lang.String)
The clue is the last parameter, the first two are correct. However, I would've expected null as the value instead of a completely different type.

Resources