MojoFailureException AssertError using maven install - maven

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);

Related

how to create a gradle test suite without using built-in frameworks

Lets say I created a new and unique Java test framework, or I am retro-fitting a legacy project where the tests are just main(String[] args)-entrypoint classes and I would like to run those with gradle test and have gradle exit with 0 if none of the tests fail/nothing throws.
"without built-in frameworks" - meaning, without useJUnit(), useJUnitPlatform() et al.

Detecting junit tests without assertions/verifications

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!

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.

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

How to get JUnit version

How to know which version of JUnit do I have installed on my PC?
Second question- How do I upgrade it?
This will print the version of jUnit.
java -cp <path-to-junit-folder>/junit.jar junit.runner.Version
The version that you are running depends on the classpath of the running application. It's possible to have multiple versions on the same machine, but if this is in question you can just write a test for it:
import junit.runner.Version;
class SomeTests {
void testJUnitVersion() {
assertEquals("4.8.2", Version.id());
}
}
To upgrade to a newer version of JUnit, you just have to download the new version and replace the old version with the new jar. It's as simple as that.
There isn't really a nice way to tell which version of JUnit you're currently using, unless you're using maven or something like that. In general, you can tell between
JUnit 3: if you're using the junit.framework.* classes
JUnit 4: if you're using the org.junit.* classes (the tests have #Test annotations on them)
In general, JUnit is backward compatible (JUnit 3 tests can be run under JUnit 4), but JUnit 4 is recommended.
Eclipse shows its runner version on JUnit view when you run your tests
In netbeans in the projects window, expand the test libraries folder of a project that has a test in it. It will say the version in the name of the jar file.
With JUnit 5 you can do something like this:
import java.util.ServiceLoader;
import org.junit.platform.engine.TestEngine;
import java.util.Iterator;
 
final ServiceLoader<TestEngine> loader = ServiceLoader.load(TestEngine.class);
final Iterator<TestEngine> iterator = loader.iterator();
final TestEngine testEngine = iterator.next();
final String version = testEngine.getVersion().get();
Upgrade process depends on your project structure. If it’s a Maven project, you can set the version in your pom.xml. If it’s still unclear I recommend creating another question for your specific build environment.

Resources