Build failure on terminal for junit test case - maven

I am new to maven and junit so please bear with me :-)
I have created a sample maven project which contains one simple test in src/test/java as folows
public class AssertUnitTest {
#Test
public void massageTest(){
Assert.assertEquals("abc", "abd");
}
}
when i run the test from eclipse, test fails saying that the expected is abc but found abd.
When i run the same using terminal it is saying BUILD FAILURE
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.assertpackage.AssertUnitTest
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.055 sec <<< FAILURE!
Results :
Failed tests:
messageTest(com.assertpackage.AssertUnitTest): expected:<ab[c]> but was:<ab[d]>
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.712s
[INFO] Finished at: Fri Sep 13 11:36:12 GMT+05:30 2013
[INFO] Final Memory: 9M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire- plugin:2.7.2:test (default-test) on project AssertProject: There are test failures.
[ERROR]
[ERROR] Please refer to /Users/support/Documents/Omniture_Selenium_Project/AssertProject /target/surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
what is this error and how to resolve it and make BUILD SUCCESS

The standard maven-build will run all tests found in src/test/java and if one of them fails (like your example-test) the build will fail.
So to make the build succeed you have to make all tests pass.

Maven uses the test goal of the surefire plugin to run all unit tests by default and fails even if one test fails. The console output pretty much says so:
Failed to execute goal org.apache.maven.plugins:maven-surefire- plugin:2.7.2:test (default-test) on project AssertProject: There are test failures.
If a goal fails to execute, the outcome is a build failure.
So, just make your test not fail or skip tests for now (not recommended).

The Surefire plugin has a testFailureIgnore parameter which, when enabled, does not cause the build to fail when tests fail.
You could add it on the command line:
mvn install -Dmaven.test.failure.ignore=true
(credit to #PascalThivent)
Or you could bake it right into your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<!-- Build, even if tests fail -->
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
Note that this does not stop the test from being run like others have suggested. The tests are run like normal and all reports are still generated. With this setting it is just no longer required that all tests pass for the project to build. Because of this I tend to include this Surefire configuration in all my poms.

During the build process a certain unit test failed. you can skip the test by adding the ff parameter -Dmaven.test.skip

Maven build lifecycle contains TEST phase which run all jUnit test which belong to project. If test fails build life cycle fails too. This is desirable behavior because you don't want to release application which doesn't pass your tests.
You can fix it by writing test which will pass TEST phase (expected behavior) or you can build application with maven parameter -DskipTests which will skip TEST phase of lifecycle.
* EDIT *
Also I recommended you to look at Docs about maven build life cycle.

Maven proposes three different way to manage failures in reactor builds: fail-fast, fail-at-end and fail-never.
The default way is fail fast, which stops the reactor build after the first failing project. To make BUILD SUCCESSFUL despite of test failures, you can try the fail-at-end policy (using the --fail-at-end (or just -fae) parameter.
Via: Mastering The Maven Command Line – Managing failures

Related

How to resume failed Maven release:prepare from a subsequent phase (and not the failing one)?

I'm running "mvn release:prepare" and have a failure on phase 11, 'scm-commit-release' due to my scm provider (RTC aka jazz) having problems with my loaded workspace:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:3.0.0-M1:prepare
(default-cli) on project driver-component-root-pom: Unable to commit files
[ERROR] Provider message:
[ERROR] Error code for Jazz SCM create changeset command - 9
[ERROR] Command output:
[ERROR] Problem running 'create changeset':
[ERROR] Cannot determine which component to create change set in. Specify a component.
I manually performed equivalent commit operations, and would like to resume building with "-rf release:prepare".
I notice that when building this way, however, Maven knows to start exactly at phase 11/17, and fails again.
[INFO] --- maven-release-plugin:3.0.0-M1:prepare (default-cli) # driver-component-root-pom ---^
[INFO] phase verify-release-configuration
[INFO] starting prepare goal, composed of 17 phases: check-poms, scm-check-modifications,
check-dependency-snapshots, create-backup-poms, map-release-versions, input-variables,
map-development-versions, rewrite-poms-for-release, generate-release-poms, run-preparation-goals,
scm-commit-release, scm-tag, rewrite-poms-for-development, remove-release-poms,
run-completion-goals, scm-commit-development, end-release
[INFO] Resuming release from phase 'scm-commit-release'
[INFO] [prepare] 11/17 scm-commit-release
[INFO] Checking in modified POMs...
.
.
.
(fails exactly the same way again)
I'd like to use "-rf release:prepare" and have Maven start at either phase 12/17 'scm-tag' or 13/17 'rewrite-poms-for-development' and continue onward. (See Maven Release manager prepare phases.)
Two questions: Is this possible to do? How does Maven know how to resume with the particular 'scm-commit-release' phase and not earlier phases?
(Thinking the latter could be hacked to replace 'scm-commit-release' with the desired subsequent phase.)
Thanks.
Well, duh. Figures after I post the question I find something.
I knew there was a 'release.properties' file, but i didn't read it closely enough. Found this encouraging line in it after the third time looking at it:
completedPhase=run-preparation-goals
(I was grepping files for 'phase' and the failed phase name)
I changed that to the phase I wanted to start at, and success! The mvn -rf begins at the phase I wanted it to restart at.

Maven and Sonarqube

A few basic questions on ci/cd pipelines.
When we build java code, do we create jar file before going for sonarqube analysis or does both happen simultaneously. My understanding is sonarqube analysis needs to be performed before maven build. Build should happen only if codequality crosses our quality checks.
Does sonar scanner and maven are used individually or sonar scanner is integrated with maven. I know both are possible but what is the best way that we need artifacts to be created only if code passes quality checks.
How does the sonarqube tell CI system (be it azuredevops or any other system) whether to go for next steps or break if the quality check is failed.
Usually you run your full build (which contains building the jar file or in general artifacts) and the sonar analysis will be done afterwards (unit tests coverage, static code analysis etc.) and no it is not done before it's done afterwards otherwise it would not be possible to integrate results like code coverage of the unit/integration test into the sonarqube analysis.
Technically the sonar scanner can be triggered via the Maven build (it is done via a maven plugin) and often called like this: mvn verify sonar:sonar(assumed that it is configured correctly).
SonarQube has a webhook which will be called/triggered if the quality is not as expected. Most of the time the CI/CD system have a stage which will shows the result of that and makes the final result of the build "red". Also many source code hosting solutions (GitHub, GitLab, Gitea or alike) having indicators which shows that (usually) within a pull request...
Update:
If you run sonar analysis on a project without compiling the code you will get this:
$ mvn clean sonar:sonar
[INFO] JavaClasspath initialization
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.952 s
[INFO] Finished at: 2022-12-04T21:41:34+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar (default-cli) on project kata-fraction:
Your project contains .java files, please provide compiled classes with sonar.java.binaries property, or exclude them from the analysis with sonar.exclusions property. -> [Help 1]
[ERROR]

mvn clean command on my Mac OS throws me error

I am trying to delete documents which are created by doorstop and getting error as "Multiple Root documents" Please refer to below link for more information
https://github.com/doorstop-dev/doorstop/issues/293
Solution for that is to run "mvn clean" command but I am getting below error for "mvn clean" command. Any possible solutions regarding this.
(base) MANOJs-MacBook-Air:~ manojdeshpande$ mvn clean
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.090 s
[INFO] Finished at: 2019-11-21T23:01:40+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/Users/manojdeshpande). Please verify you invoked Maven from the correct directory. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException
The error message is pretty clear:
The goal you specified requires a project to execute but there is no
POM in this directory (/Users/manojdeshpande). Please verify you
invoked Maven from the correct directory
The problem is that Maven can't find a pom file (pom.xml is the maven project configuration file) where you are running the maven clean command, so it's impossible for Maven to know what to do, even if you specified the clean goal.
Maybe your pom file is somewhere else than /Users/manojdeshpande?

Maven build - surefire plugin error - File name too long

I was building project as usual with mvn clean install and following error happened:
(File name too long) -> [Help 1]
Full maven output:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:19 min
[INFO] Finished at: 2017-02-23T13:42:19+01:00
[INFO] Final Memory: 67M/544M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project union-sme-webapp: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test failed: java.lang.RuntimeException: org.apache.maven.surefire.report.ReporterException: When writing report: /home/gondy/projects/xxxxxxx/xxxxxxxxx/webapp/target/surefire-reports/TEST-xx.xxxxx.xxxx.xxxx.webapp.service.document.generator.statics.arrangements.XxxxxxxxxxXxxxxxxXxxxxxxxxxxxXxxxxxxxXxxxxxxxxXxxx.xml (File name too long) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
I am working on Ubuntu 16.04 LTS. My colleagues are building the same project without any problem, it works even on Jenkins CI. What is the problem here? The filename is 144 chars long, which should be fine, ext has limit of 255 charactes.
The problem is, I have encrypted home directory and Ubuntu allows filename only 143 chars long:
From one of authors of eCryptfs:
Empirically, we have found that character filenames longer than 143 characters start requiring >255 characters to encrypt. So we (as eCryptfs upstream developers) typically recommend you limit your filenames to ~140 characters.
https://unix.stackexchange.com/a/32834/82647
So options basically are:
Make filename shorter by renaming methods and classes
Move your project outside encrypted directory
Remove encryption of your home directory
you can always disable file reporting via pom.xml file or console parameters
mvn clean install -Dsurefire.useFile=false -DredirectTestOutputToFile=false -DdisableXmlReport=true
Test results will be logged to console
Other way (untested by me) would be aggregating report, mentioned here maven surefire reporting plugin configuration

mvn package failes even with -DfailIfNoTests=false

I have a maven build which I would like to execute the jar creation for, but mvn package fails due to unit test failures.
I'd like to still build the jar in spite of the fact that some tests fail, I tried
mvn package -Dtest=false -DfailIFNoTests=false
But that fails.
mvn jar:jar seems to work, but I'd rather use the more idiomatic mvn package approach.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.119s
[INFO] Finished at: Mon Mar 11 12:07:16 EDT 2013
[INFO] Final Memory: 9M/234M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.10:test (default-test) on project glusterfs: No tests were executed! (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
The surefire plugin controls test running and failure reporting.
mvn package -Dmaven.test.failure.ignore=true
should compile and run your tests, but not fail the whole build if some of the tests fail. (I've never used this, going on reading docs only.)
You can skip running unit tests with the following:
-Dmaven.test.skip=true

Resources