How to run buildr tests with -ea flag (enable assertions) - buildr

Does anyone know how to enableassertions during testing? I'm trying to use buildr to for a lucene based project and I get the following exception:
[junit] Test class requires enabled assertions, enable globally (-ea) or for Solr/Lucene subpackages only:
I've tried from the command line:
JAVA_OPTS=-ea buildr
and putting the following in my buildfile:
ENV['JAVA_OPTS'] ||= '-enableAssertions'
I'm using the java version of buildr (1.4.12)

ugh, 2 seconds after submitting this I figured out the solution:
test.using :java_args => [ '-ea' ]

I had same issue with SOLR and eclipse, solution is exactly the same.
In package explorer right click on project:
Run As->Run Configurations...->JUnit->ProjectName->Arguments(second tab)->VM arguments
Type in :-ea

Related

Gradle how to exclude 1 test file in a multi module project setup?

So we have a multi module project setup, with test all scattered in multiple modules, now we want to execute all of them but exclude 1 test file.
How could we achieve this?
I tried the following:
gradle test -PexcludeTests=*SpecificTests
but the tests get still executed.
for running a singular test I managed to fix it this way:
gradle :multi-module:test --tests '*SpecificTests'
but unfortunately the equivalent for executing all tests but 1 cannot be made with this.
Condition: we need a command we cannot use the testing filter
You can use a project property and define an optional exclude filter, which you can then use from the command line -PexcludeTests='*SpecificTests'.
test {
if (project.hasProperty('excludeTests')) {
exclude(project.property('excludeTests'))
}
}

Hamcrest assertion fails in Maven test cases

I am using Hamcrest Matchers to check if two beans have same property Values. It is working fine in Eclipse but fails when I run using cmd. Following is the stack trace for the same. Can anyone please help?
java.lang.NoSuchFieldError: NONE
at org.hamcrest.DiagnosingMatcher.matches(DiagnosingMatcher.java:12)
at org.hamcrest.beans.SamePropertyValuesAs.hasMatchingValues(SamePropertyValuesAs.java:63)
at org.hamcrest.beans.SamePropertyValuesAs.matchesSafely(SamePropertyValuesAs.java:31)
at org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:55)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:12)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
My code is as follows:
assertThat(actualBean, samePropertyValuesAs(expectedBean));

How to pass system properties to the tests in gradle in the smart way?

build.gradle
tasks.withType(Test){
systemProperties=System.properties
println systemProperties['param']
}
Now I can either pass parameters in the command line:
gradle test -Dparam=10
or put them in gradle.properties:
systemProp.param=15
Ideally I would like to put the defaults in the gradle.properties, and be able to overwrite them from the command line. Unfortunately if I do that, the gradle.properties has precedence, and -Dparam=10 is ignored.
Could you offer any solutions on that?
https://issues.gradle.org/browse/GRADLE-2122
It works since 2.12 or 2.13 "the smart way" already!
The example above is working, the command line -D option overdrives the defaults in gradle.properties
I am using gradle 2.12 and sharing how I used it:
test {
// support passing -Dsystem.property=value to bootRun task
systemProperties = System.properties
}
I have JUnit tests that I wanted to skip unless a property was used to include such tests. Using JUnit Assume for including the tests conditionally:
//first line of test
assumeThat(Boolean.parseBoolean(System.getProperty("deep.test.run","false"),true)
Doing this with gradle required that the system property provided at the time of running gradle build, shown here,
gradle build -Ddeep.test.run=true
was indeed passed through to the tests.
Hope this helps others trying out this approach for running tests conditionally.

Gradle: try to skp sonarqube results in error

I have a gradle script that uses Sonarqube plugin (org.sonarqube). If I let the publish task depends on it, it works fine.
The problem is to run sonarqube only if a condition is true. So I tried (as described in gradle documentation) all of these three statements:
sonarqube.enabled (false)
sonarqube.enabled=false
sonarqube.onlyIf { false }
Each results in an error, here the one I got trying the first statement
FAILURE: Build failed with an exception.
* Where:
Build file 'D:\Eclipse\workspace3.6\at.mic.hermes\build.gradle' line: 208
* What went wrong:
A problem occurred evaluating root project 'at.mic.hermes'.
> Could not find method enabled() for arguments [false] on org.sonarqube.gradle.SonarQubeExtension_Decorated#412196.
To be sure to have not typo in the code I tried all statements with the test task, e.g.
test.enabled(false)
and this results (as expeted) in
:test SKIPPED
Any ideas what I made wrong / what must be changed? Thx in advance!
Frank
I think the problem come from a name conflict. There are 2 objects named 'sonarqube':
The SonarQube task
The SonarQube extension
It seems to not break your build, but here when you write sonarqube.enabled it access to the extension (according to your stacktrace).
The solution is probably to disambiguate using tasks.sonarqube.enabled. See https://docs.gradle.org/current/userguide/more_about_tasks.html#N11143

Dependency Injection failure during mvn install loading standalone run

I have a RESTful service created using Spring MVC. I created an integration test to test my Rest controller, using spring-test mvcmock.
When I am running this test using run as Junit. It is running fine.
However, when the same test is getting executed during mvn install, using surefire plugin. This test is complaining throwing fatal error:
No qualifying bean of type ...
When I checked previous logs it says that it loaded 0 dependecy.
This was the log :
DEBUG XmlBeanDefinitionReader - Loaded 0 bean definitions from location pattern [classpath*:service-test-context.xml]
However, I again run this test class as Junit and I could see "loaded 23 beans at the same log line."
Question: Could you please suggest, what can be the issue?
Note:
This is multi maven module application and dependency of another module is not getting injected.
I was getting same issue, when I was running this test as Junit. Then I added dependent modules in eclipse > build > project and issue got resolved in run as JUnit. Now getting this issue while doing mvn install.
Does your surefire configuration contain an includes parameter?
"A list of elements specifying the tests (by pattern) that should be included in testing. When not specified and when the test parameter is not specified, the default includes will be
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
"
Easy fix is to rename your test runner so that it ends in Test.java. Then
mvn test
will pick it up.
Updated
An alternative is this:
"test:
Specify this parameter to run individual tests by file name, overriding the includes/excludes parameters. Each pattern you specify here will be used to create an include pattern formatted like **/${test}.java, so you can just type "-Dtest=MyTest" to run a single test called "foo/MyTest.java". The test patterns prefixed with a ! will be excluded.
This parameter overrides the includes/excludes parameters, and the TestNG suiteXmlFiles parameter. Since 2.7.3, you can execute a limited number of methods in the test by adding #myMethod or #my*ethod. For example, "-Dtest=MyTest#myMethod". This is supported for junit 4.x and testNg.
Since 2.19 a complex syntax is supported in one parameter (JUnit 4, JUnit 4.7+, TestNG):
"-Dtest=???Test, !Unstable*, pkg/**/Ci*leTest.java, Test#testOne+testTwo?????, #fast*+slowTest"
"-Dtest=Basic*, !%regex[..Unstable.], !%regex[..MyTest.class#one.|two.], %regex[#fast.|slow.*]"
The Parameterized JUnit runner describes test methods using an index in brackets, so the non-regex method pattern would become: #testMethod[]. If using #Parameters(name="{index}: fib({0})={1}") and selecting the index e.g. 5 in pattern, the non-regex method pattern would become #testMethod[5:].
Type: java.lang.String
Required: No
User Property: test
"
So if your test runner was named foo/MyVeryOwnRunner.java you could do the following
mvn clean test -Dtest=MyVeryOwnRunner

Resources