We are looking for a way to fail the build if there are any unit tests with out any sort of verifications. IntelliJ seems to "inspect" these tests by specifying the list of valid assertions.
org.junit.Assert assert.*|fail.*
junit.framework.Assert assert.*|fail.*
org.junit.jupiter.api.Assertions assert.*|fail.*
org.assertj.core.api.Assertions assertThat
com.google.common.truth.Truth assert.*
com.google.common.truth.Truth8 assert.*
org.mockito.Mockito verify.*
org.mockito.InOrder verify
org.junit.rules.ExpectedException expect.*
org.hamcrest.MatcherAssert assertThat
Is there any "Maven" equivalent plugin which we can hook up to the build? Thanks in advance!
Related
So I have a test suite written in JUnit, like below, basic mantra is that all the test cases which are listed in #Suite.SuiteClasses are going to be included in for running of the tests.
package bddDemo.TestSuite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
#RunWith(Suite.class)
#Suite.SuiteClasses({
//serenityBDDDemo.features.calculator.MathsUsingAnAngularCalculator.class,
//serenityBDDDemo.features.API.automateRESTAPI.class
})
public class TestSuite {
}
This runs perfectly well in maven, I can do mvn clean verify and what not, but when I tried to convert this project to a Gradle project, suite is not run, instead all the tests are run, weather are not marked for inclusion for running in the suite(which is kinda embarrassing).
Can you suggest a way to mark few tests cases for running and make a test suite in a gradle?
I had done a simple project, trying to understand how ParameterizedTest and ValueSource works.
From the below picture it finds the import path, but it throws an error when I try to run the code:
Also the gradle file:
Here is a link to the entire project.
You need to put junit-jupiter-params in the testCompile source set.
junit-jupiter-params exports types like #ParameterizedTest and #ValueSource that are needed at compile (and run~) time.
See also: Missing org.junit.jupiter.params from JUnit5
Starting with version 5.4.0-M1 JUnit Jupiter provides an aggregator artifact that bundles all available Jupiter-defining artifacts for easy consumption. See https://sormuras.github.io/blog/2018-12-26-junit-jupiter-aggregator.html for details.
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.
I'm working on a Java project that uses Gradle as its build system.
I want to add some bdd tests using Cucumber-JVM. Following this example I was able to configure Gradle's build.gradle to have a task called cucumber, and I was able to execute that task using "gradle cucumber".
But what I am looking for is a way to have Gradle run that task automatically during its test phase (where it runs all the other regular unit tests). I also want the build to be flagged as failed if any of the cucumber tests fail (strict=true).
Is this possible? I don't know a lot about Gradle and Google so far has produced nothing really useful.
Okay so I figured out how to achieve this. Simply add this to build.gradle:
test << {
javaexec {
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--strict', '--monochrome', '--plugin', 'pretty', '--glue', 'com.mypackage', 'src/test/resources']
}
}
All you really need are the feature file and the steps java files that implement the glue code. You do not need the xxxCukesTest.java file with a #RunWith annotation since Gradle ignores it. You may want to keep it anyway because it enables you to run tests from your IDE.
Works really neat!
I wrote a unit test that passes when I run it in Eclipse, but fails when I do "maven install".
I use JUnit 4, Mockito 1.9.5, Maven 3.0.4, JRE 1.7._51, Sunfire 2.15.
The assert that seemingly fails is:
assert string1.equals(string2);
Answer
Java keyword assert must be activated to work.
They can be activated at run-time by way of the -ea option on the java command, but are not turned on by default.
What does the Java assert keyword do, and when should it be used?
Some advices
For string comparision use equals
assert string1.equals(string2)
How do I compare strings in Java?
Use junit assertions in test
assertEquals(string1, string2)
assert vs. JUnit Assertions
For best results use AssertJ - Fluent assertions for java
assertThat(string1).isEqualTo(string2);