What does the "Maven Surefire plugin" means when it test your build? - java

I am working on understanding Maven and I'm learning about building your Java app with it.
So when I do a :
maven package
It does build my jar as expected but I see in the output console that Maven does build tests (it always say that the test a run and there are no failure).
I researched on the web about that and learned that Maven use a plugin called Maven Surefire. But I can't understand what does that plugin do to my code, what does the tests "means" ? What does the tests do with my code and how it works behind the console ?

The Maven surefire plugin runs the tests you have written. These are usually in the src/test/java folder. If you have none, the plugin does nothing.

Is this only one question? :D
So. Different things are going on.
You create an application with Java. To test the single components / packages / classes that you create most people use JUnit or TestNg. You usually have dedicated test classes that verify your production code behaves as intended without you clicking through all the things on every change.
When you now use maven to run your build the pom.xml file defines a packaging - in your case "jar" since you create a jar file. The packaging defines what set of default plugins run in the defined maven phases. You probably recognize package here. Maven executes all phases up to package and the registered / configured plugins.
To execute those tests maven provides the surefire plugin which supports running JUnit or TestNg tests. If you follow the directory conventions your tests reside in src/test/java and the surefire includes naming convention maven will execute those tests in every build (as this is the best practice). If you also want to write integration tests then there is the failsafe plugin. That plugin is not enabled by default and runs in different maven phases.
So the tests just run your production code - in fact they just do what you implement in the tests. They don't alter it in any way.
The maven introduction documentation has step by step explanations: Maven in 5 Minutes and the Getting Started Guide.
Starting from scratch this is probably a lot. So don't rush this. The build setup and test setup are very important things to have.

Related

Unit test only fails in maven, not iIntelliJ

I'm hitting a weird issue were my unit tests fail when running mvn install but they pass when running in IntelliJ.
I've looked at posts online about debugging maven, but I don't want to debug maven itself, or a maven plugin. I want to debug my unit tests when they have been called by maven. More specifically, I want to debug my code but using the Junit tests as an entry point for the debugging via a breakpoint.
Hope that makes sense.
My issue is relating to using introspection to look at some classes the application is importing from another project. This succeeds when the project is running or running in debug mode, but fails when maven runs the tests as a part of an install. i.e. the code doesn't find the classes in the other project when running under maven...
As this application uses multiple maven projects that all need to be installed, I've had to move my unit tests out into another project just so that the installs don't fail and I can carry on development.
However, for obvious reasons I would like to put my unit tests in each individual project, so am very keen to debug this and find out what is failing.
My project is called infinite-state-machine and can be found here - https://github.com/museadmin/infinite-state-machine
The unit tests are currently in their own project here - https://github.com/museadmin/infinite-state-machine-test
If anyone can advise me how to run maven in debug mode and attach IntelliJ to it so that I can set a breakpoint in my failing unit tests I will be forever in their debt :)
Thanks in advance for any help.
Brad

Executing a maven lifecycle target on artefact installed in local repo

Is it possible to execute a lifecycle target (e.g. integration-test) on an artefact that has been installed within the local repo?
My use case is as follows. I have a multi module project with many modules that dedicated various types of integration testing (compliance test, performance tests, etc). I need to invoke these integration multiple times with different environment configurations. These configurations are expressed as maven profiles and parameterised using properties. I want to avoid recompiling the project over and over again.
I would like to have one build CI job performing the mvn install, then separate CI jobs performing the integration tests, triggered once the build CI job has passed. The integration tests would simply invoke integration-test lifecycle phase of the installed artefact setting the profile and passing the parameters
I have tried pointing mvn at the .pom file within the local repo but this does not work. It fails because it cannot find classes within the artefact's own JAR file (as if it were not being put on the classpath) - a problem that doesn't occur if I have my integration job checkout the tree and invoke the pom.xml within the source tree.
mvn -f ~/.m2/repo/x/y/z/myproj-perftests-x.x.x-SNAPSHOT.pom integration-test -Pmyprofile -Dparam1=blah
No, it is not possible. Maven plugins (normally) only work with project sources.
If your only concern is recompiling project again and again, consider splitting your project into the core part and the integration tests part. Then when running integration tests you'll only need to recompile the integration tests part.

Projects which are accessed via multiple (different) IDEs: Ant or Maven?

Is it true that one of the real benefits of Maven is that its projects structure is clearly defined and therefore it can be opened in any IDE that supports Maven?
We develop in a team consisting of 5 people and we use three different IDEs (IntelliJ, Eclipse, Netbeans). I know that I cannot open an Ant project of Eclipse in Netbeans but with Maven this is possible.
Is this really reason enough to use Maven?
Maven and Ant are two different things.
Ant is a build tool while Maven accumulates the functionality of:
resolving dependencies (you only specify the dependencies, and Maven cares about downloading them in the correct version from the default repositories on the i-net, or the ones you specified explicitly)
a build tool (incorporating Ant),
creating distribution artifacts (containing not only the jar/war/etc. file of the project but also dependencies, resources, documentation),
runtime environment (e.g. starting Jetty and deploying your artifact)
unit testing
integration testing
deployment (including signing, incrementing the version, deploying the artifacts etc.)
and more (look out for plugins on i-net)
Maven uses lifecycles to achieve this. You can trigger certain functionality at certain points in the lifecycle. For example unit tests should be run right after compiling while integration tests (e.g. selenium tests) require more setup, maybe initializing a web server and deploying the WAR file.

gradle multiproject only run test for some submodules

What is the easiest way of setting up a gradle configuration so that I can run tests for only one or two submodules in a multimodule project?
The reason I don't need to test all of them is because they reside in their own repositories and get tested when those are changed. It really increases the turnaround-time to have TeamCity run a shitload of tests that I know will pass.
My initial thought was to set up a task in the top-level build.gradle that has dummy implementations in the modules that don't need to run the tests and performs connectedCheck for the android submodule and runs the tests for the java ones, but I havn't gotten that to work yet. I looked at the gradle documentation for multi-project builds (http://www.gradle.org/docs/current/userguide/multi_project_builds.html) but couldn't figure out how to call the clean and build commands from inside a Closure.
Help? :)

Measuring integration test coverage with Maven

The followings are ok now:
I have a multi-module project in maven with EJB and WAR projects
I want to use JMeter (and later selenium) for integration testing. I can run both from Maven. The JMeter plan is ready, I run it with Chronos maven plugin.
My application is a Java EE application, so I want to test the code with the planned production aplication server, which is Glassfish 2.11. I can create/start/deploy/stop and anything like that with glassfish maven plugin
I have put jmeter and glassfish related build settings into a submodule in maven, which is dependent on all of the other modules, so in build lifecycle it is the least, and for this reason a good point to test the whole application
My problem is, that how can I reach the followings:
deploy NOT instrumented code, but run integration tests on instrumented one
how to get coverage info from application server
I wanted to use emma4it which was created to instrument artifacts. It would be good for me, but i cannot make it instrument the war file in the other submodule. I do not even know anything about the supported arguments of emma4it, since I did not find it at all (just a binary in repository), no documentation and no source (I know I can decompile it)
I want to have a coverage raport at least in maven site, but the top would be to have it in Sonar
Could you give me advice, how to do this? I can provide POM snippets if you need it.
FINALLY. The solution arrived. See at http://www.sonarsource.org/measure-code-coverage-by-integration-tests-with-sonar/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+Sonar+(Sonar)&utm_content=Google+Reader
I currently try to do it.

Categories

Resources