Measuring integration test coverage with Maven - java

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.

Related

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

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.

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.

Using maven to produce production ready output

I have a muti-module maven project, and I created a new module that depends on 3 other modules. (I already have a web app maven module that produces a .war file, now I need this)
This module's output is a .jar, and it has a few resources also which are:
spring context xml file
properties file
Now I want to produce a production ready folder so I can upload it to my server. I am hoping maven can do this for me.
I need the following layout:
myjar.jar
/libs/ (the 3 other maven modules that are dependancies)
/resources
Also, there are some generic dependancies that my parent pom.xml have like slf4j/log4j/ that I also need to package.
It would be cool if I could add a switch to mvn that will produce this like:
mvn clean install production
I plan on running this on my server via the command line.
I think what you are looking for is a Maven Assembly:
https://maven.apache.org/plugins/maven-assembly-plugin/
You can use profiles to disable the generation of the assembly by default (can speed up the development process).
#puce is right in that you may be best to use the Assembly Plugin. What you can't do easily is add another lifecycle 'production' to maven. If you have time you could write a plugin to do this, but you might be better off using a profile called 'production' or 'prod-deploy' to enable the coping into place on the server.
mvn clean install -Pprod-deploy
One thing to remember with maven is that it is very good at building projects in using it's conventions, but it is pretty bad at actually script things to happen out side of the build lifecycle.
I have on several occasions used external scripting tools such as ant/python/bash and groovy to first run the build using mvn then to script the deployment in a more natural language.
The intention of Maven is building not deployment in the sense to production. For this purpose i would recommend things like Chef or Puppet. From a technial point of view it's of course possible to handle such things via Maven. What also possible to build on CI solution like Jenkins. Furthermore it's possible to run a script from Jenkins to do the deployment on production.

Why do I need Maven if I use Eclipse?

I have seen that if I right click on a project in Eclipse and choose to run it on a server, then I can see output which means the project is running.
If everything is working fine without Maven, what's the point of using it. How is it different than simply running it via eclipse?
Maven is a build tool (build manager, in fact), similar to ANT. The main job of any build tool is configure the project, compile using required projects and do the final packaging. A build script in your project gives a blue-print of project's deliverable structure. This frees you from any configurable dependencies on specific IDE like Eclipse. All you need to know is the standard command to perform the build and you can build your code almost anywhere.
Now, back to your question, why wouldn't do it in Eclipse?
For a simple project and small team Maven is an overkill. You can easily communicate the configuration, IDE to use, and instruct any special steps to be taken. In big projects, however, there exits lots of loosely coupled dependencies. To start with, there will be different settings for developer machine build, test build and production build. There are requirements to run automated test, integration tests, store the build package (artifact) to a commonly accessible repository, update versions of various modules.
Obviously, if all the steps mentioned above is done manually there are chances of missing a step. Moreover, the manual process is time consuming.
Ideally, you should prefer a tool which fits the best for you. If you think that you're able to achieve what you required without Maven, it makes sense to not to use Maven/build-tool just because everyone uses it.
It is suggested to study automated deployment, this will give you bigger picture on what all the stuffs that you can do with build tools. And if you do not feel that it adds any value to your current process, you probably don't need Maven or any other build tool right now.
Your question does not make much sense. Do you expect your users to access your application from eclipse? If so that is a very strange set up in my opinion.
Perhaps your question should be about how to build your project. Maven provides you a way to centralize dependency libraries across the enterprise. It lets you automate your build process (most likely in conjunction with a CI server like hudson, cruise control, etc). It lets you automate your unit testing. Maven makes the packaging of app very easy to do. A developer does not have to follow arcane set of steps to package an application. You add the right plugin and maven takes care of it as part of the build life cycle. All of this magic can happen because of the principle of convention over configuration. There are many more benefits, I just named a few.
Maven is not replacing how you run the app, rather how you package the app, automate that process, and manage the dependencies of your app.
Some links on why someone should use maven:-
Why maven ? What are the benefits?
why I use Maven
Why you should use Maven
Use Maven

Pointers on how to to get a test jar to run a separate war for testing (perhaps with cargo?)

Let me explain some of my constraints.
We have a war that has a CXF Soap service and a Spring MVC REST Service. Both the CXF and Spring MVC implementations are in a separate jar and are brought in as dependencies. The REST service has its unit tests in its project.
I was wondering if there was any way to, while doing something like 'mvn clean test' in the REST jar, to have a local version of the war set up and then run the unit tests. Thus, when building in something like Hudson or doing releases, there won't have to be any workarounds (such as deploying a snapshot ear or running a local war manually)? I've seen this done when the tests are within the war using cargo, but not when the tests are separate from the war.
Right now, we're going to take the tests out into a separate jar but that's still not ideal as if something happens to go wrong during a release, that'd mean the REST jar and war were already released. I'd prefer do it the above way, with the tests in the same project as the REST service.
If anyone has any pointers or doc or examples that could help with this, it would be appreciated.
Honestly, I'm not sure I understood all the constraints. Anyway, the recommended way to implement integration tests with Maven is to put them in a separate module (that's much easier, especially if the module under test also have unit tests) that depends on the war/ear under test and to:
Start a container and deploy the war/ear during the pre-integration-test phase
Have Maven run the tests during integration-test
Stop the container during post-integration-test
For the steps #1 and #3, I personally use Cargo. For the step #2, using the Maven Failsafe Plugin is the preferred option (because it won't stop the build if a test fail). At least, this is what I use and I have used the resources below to build my setup.
An alternative approach would be to start/stop an embedded container from the tests. For example, this is doable with Jetty, see Embedding Jetty and Unit Test Servlets with Jetty.
Resources
Functional testing with Maven, Cargo and Selenium
Maven and Integration Testing
Maven and Selenium
Unit Test Servlets with Jetty
Maven has also integration-test phase. Use maven-jetty-plugin to start container and deploy application. Then run your integration test.
Maven Jetty Plugin
Maven and Integration Testing
Update
Tests cannot be in a jar file. Maven surefire plugin could not run them. The tests are part of the project where you run integration test. The tested war file can be set as dependency library. It will be downloaded, deployed and the you run integration tests.

Categories

Resources