EclEmma, powermock and Java 7 issue - unit-testing

I am using Powermock but when I run Eclemma coverage, the Powermock test cases are not considered in the final score because of known issues with EclEmma.
The work around for this which I search and other answers on stackoverflow suggest to have:
#Rule
public PowerMockRule rule = new PowerMockRule();
static {
PowerMockAgent.initializeIfNeeded();
}
Then add jars like powermock-module-javaagent, powermock-module-junit4-rule-agent.
After doing this when I am running my code then finding error:
java.lang.VerifyError: Expecting a stackmap frame at branch target 7
For this answers suggest to have javassit of various versions. But I am unable to get that work and getting the same error.
My pom.xml looks like:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-javaagent</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule-agent</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.21.0-GA</version>
<scope>test</scope>
</dependency>
How can I get PowerMock test cases to be included in final Eclemma score ?

My favorite non-answer: don't waste your time trying.
Instead, take this as another hint that PowerMock should not be used; and use your time and energy to learn how to write better production code that can be tested without the need to PowerMock.
For example, start looking into those videos; to understand that code that avoids calls to static or new (in an untestable) way is very often bad code; and that improving this code not only helps you to get rid of PowerMock, but also to improve your over all product.
And in case you find leaving PowerMock to hard; my suggestion: then put your tests into two buckets (one for those that really use PowerMock for things only PowerMock can do; and one for all others); and only measure coverage for the later one. This would also open a "path forward": simply avoid using PowerMock for any new things you. Instead, you could turn to mockito, which has improved a lot of the last years; even allowing you things (experimentally) like overriding final methods.

I figured out a way, may be not clean but I think it works out. So the problem statement was how to get Powermock get included in EclEmma ?.The first change in approach was move to maven approach of Eclemma i.e. jacoco Please feel free to go through the link below before moving to the answer:
Power mock with jacoco
Basic of Java Instrumentation
In a nutshell, jacoco identifies code coverage by having a lock/updating bytecode/instrumenting the class, when being loaded. And powermock for its working also lock/updates bytecode/instrumenting the class for its functioning, BUT IT DOES THIS FROM THE DISK AND NOT THE MEMORY.
Thus when coverage is given by jacoco, powermock classes are not covered as powermock runs test cases from disk files and jacoco has no idea what powermock is doing.
So the work around it to store the classes instrumented by jacoco on the disk and then powermock runs test through those classes which in turn will let jacoco know that test are running and include them in code coverage.
[PLS FEEL FREE TO CORRECT MY UNDERSTANDING REGARDING THIS]
So the final code changes are:
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>0.7.7.201606060606</version>
<classifier>runtime</classifier>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.6.1</version>
<scope>test</scope>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
// files you want to exclude
</excludes>
</configuration>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>${project.build.directory}/coverage.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
after this running -PcodeCoverage worked out for me.

Related

axistools-maven-plugin not generating source from WSDL

I am using Apache Axis to generate the java source from my WSDL file. The maven run was successful without any errors but no generated classes.
Question: What am I missing here?
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>axistools-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>wsdl2java</goal>
</goals>
<configuration>
<sourceDirectory>/src/main/resources</sourceDirectory>
<outputDirectory>/src/main/java</outputDirectory>
<wsdlFiles>
<wsdlFile>thesourcewsdlfile.wsdl</wsdlFile>
</wsdlFiles>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
I am clearly not sure what is the problem with this configuration.
Try to define source and output directory bases on your maven project, that is, using standard maven properties to point to the right absolute path at runtime, changing your configuration as following:
<sourceDirectory>${basedir}/src/main/resources</sourceDirectory>
<outputDirectory>${basedir}/src/main/java</outputDirectory>
Based on this configuration, the thesourcewsdlfile.wsdl is supposed to be located under src/main/resources/thesourcewsdlfile.wsdl
The full plugin configuration should hence be:
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>axistools-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>wsdl2java</goal>
</goals>
<configuration>
<sourceDirectory>${basedir}/src/main/resources</sourceDirectory>
<outputDirectory>${basedir}/src/main/java</outputDirectory>
<wsdlFiles>
<wsdlFile>thesourcewsdlfile.wsdl</wsdlFile>
</wsdlFiles>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Moreover, the following dependencies must be added to the project:
<dependencies>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxrpc-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
As a general note, it would be much better to place generated sources under the target directory, since they are generated automatically and should not be added to the version control in use. A standard location would then be any desired (meaningful) directory under ${project.build.directory}/generated-sources (where ${project.build.directory} is the standard property pointing to the target directory indeed).
IMHO you are missing the wsdl folder under resources, just add it and run:
mvn clean generate-sources

What is the difference between "Generating reports via build.xml" and "Generating via pom.xml"?

Earlier I was using build.xml (ant) to run my test cases but now I use pom.xml (maven) to run the test cases.
When I had ant I was able to get testng-xslt reports but after reading many blogs and tutorials I couldnt generate via pom. I wanted to investigate that what is the difference and I saw that I had saxon.jar in my class path but it was missing in pom.xml so I added a dependency. Second thing that I notice that I haven't specified the .xml path in pom.xml(I dont know where to add it in pom).
I am giving both pom.xml and build.xml here, please take a look on both and let me know what I have missed to generate testng-xslt reports via pom.xml but it was present in build.xml and how I can fix that.
build.xml
<target name="testng-xslt-report">
<delete dir="${basedir}/testng-xslt">
</delete>
<mkdir dir="${basedir}/testng-xslt">
</mkdir>
<xslt in="${basedir}/test-output/testng-results.xml" style="${basedir}/testng-results.xsl" out="${basedir}/testng-xslt/index.html">
<param expression="${basedir}/testng-xslt/" name="testNgXslt.outputDir" />
<param expression="true" name="testNgXslt.sortTestCaseLinks" />
<param expression="FAIL,SKIP,PASS,CONF,BY_CLASS" name="testNgXslt.testDetailsFilter" />
<param expression="true" name="testNgXslt.showRuntimeTotals" />
<classpath location="D:\automation\windowsproject\zookeeper\lib\saxon-8.7.jar">
</classpath>
</xslt>
</target>
pom.xml
<build>
<testResources>
<testResource>
<directory>src/test/resource</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>
C:/Users/windowspc/workspace/windows-project/Chrome.xml
</suiteXmlFile>
</suiteXmlFiles>
<testFailureIgnore>
true
</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.testng.xslt</groupId>
<artifactId>testng-xslt-plugin</artifactId>
<version>1.1</version>
<configuration>
<outputDir>C:/Users/windowspc/workspace/windows-project/target/testng-xslt-report</outputDir>
<showRuntimeTotals>true</showRuntimeTotals>
<sortTestCaseLinks>true</sortTestCaseLinks>
<testDetailsFilter>FAIL,PASS,SKIP,CONF</testDetailsFilter>
</configuration>
</plugin>
</plugins>
</reporting>
<pluginRepositories>
<pluginRepository>
<id>testng-xslt-plugin</id>
<url>http://uhftopic.com/maven/</url>
</pluginRepository>
</pluginRepositories>
...
...
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>saxon</artifactId>
<version>8.7</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
There is a new version of this plugin. It's groupId/artifactId as changed. Here is what you can use:
<groupId>org.reportyng</groupId>
<artifactId>reporty-ng</artifactId>
<version>1.2</version>
Here is the project site: https://github.com/cosminaru/reporty-ng/wiki/MavenPlugin
Here is the github repository: https://github.com/cosminaru/reporty-ng and the plugin repository is now here:
<pluginRepository>
<id>reporty-ng</id>
<url>https://github.com/cosminaru/reporty-ng/raw/master/dist/maven</url>
</pluginRepository>
The major differences between ant and maven are:
maven use conventions over configuration
ant need to be fully configured
It means that with ant you can quite easily do anything you need, but you have to write ant script to achieve what you need. On the other hand with maven make a lot of assumptions about your project structure and if your project structure is conform to those assumptions: you can do a lot of usefull jobs like building, testing, packaging, generating doc with almost nothing in your pom.xml.
One important thing to understand with maven is the concept of life-cycle. There are 3 life-cycle defined by maven : the clean-life-cycle, the default-life-cycle and the site-life-cycle.
clean: use it to clean any previously build product
default: use it to build, test, package your project
site: use it to generate documentation and reports
Each life-cycle is a succession of phases. Additionaly, each phase is bound with a plugin-goal (a plugin-goal is at some point similar to an ant target). The concept of life-cycle introduce a dependency between plugin execution (and so you don't have to specify it). For instance maven knows that it must compile the sources and the tests sources before running the tests.
In your case: you need to generate a report, so it is something that can be done during the site-lyfe-cycle. You need to generate a report based on test results, so you need to run the test prior to generate reports. To do that simply run this maven command:
mvn clean test site:site
This command indicate to maven to:
cleaning any previous build product (clean life-cycle)
running the test (default life-cycle) (maven knows that it must compile before running the tests)
generating a site with the documentation and since you have define a reporting plugin : it will be executed during the site generation.
You will find your reports under target/site directory.
Another thing to note: the saxon dependency is a dependency of the plugin, not a project dependency so you must specify it under the <plugin> element:
<plugin>
<groupId>org.testng.xslt</groupId>
<artifactId>testng-xslt-plugin</artifactId>
<version>1.1</version>
<configuration>
<outputDir>C:/Users/windowspc/workspace/windows-project/target/testng-xslt-report</outputDir>
<showRuntimeTotals>true</showRuntimeTotals>
<sortTestCaseLinks>true</sortTestCaseLinks>
<testDetailsFilter>FAIL,PASS,SKIP,CONF</testDetailsFilter>
</configuration>
<dependencies>
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>saxon</artifactId>
<version>8.7</version>
</dependency>
</dependencies>
</plugin>

Enunciate not generating documentation

I am trying to generate enunciate documentation form a mix of classes, all JAX-RS annotated, some coded inn java, some in clojure.
I have built a maven project that simply depends on a war file containing the service classes:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.enunciate</groupId>
<artifactId>maven-enunciate-plugin</artifactId>
<version>1.27</version>
<configuration>
<configFile>src/conf/enunciate.xml</configFile>
<additionalClasspathEntries>
${settings.localRepository}/com/ws/scholar/0.96-SNAPSHOT/scholar-0.96-SNAPSHOT.war
</additionalClasspathEntries>
</configuration>
<executions>
<execution>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.ws</groupId>
<artifactId>scholar</artifactId>
<version>0.96-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
I cannot get any service documentation to generate.
When running mvn enunciate:docs -X, I see my classes "Noticed":
[DEBUG] Noticed class WEB-INF.classes.com.ws.scholar.resources.ClientResource in /.m2/repository/com/ws/scholar/0.96-SNAPSHOT/scholar-0.96-SNAPSHOT.war.
Can anyone offer guidance or corrections to my configuration?
Unfortunately, you can't just add a war as a dependency and have the classes therein get picked up on the classpath. A war isn't a jar.
Instead, you'll probably need to use the attachClasses parameter of the maven-war-plugin to export your classes as a jar in addition to the war. Then you can depend on that jar like this:
<dependency>
<groupId>com.ws</groupId>
<artifactId>scholar</artifactId>
<version>0.96-SNAPSHOT</version>
<classifier>classes</classifier>
</dependency>

Xpath validation fails on automated test with maven / soap-ui plugin

We are using Soap-UI for writing some web-services tests.
I put this XPath validation in one of them:
count(//mynode) > 1
This is working fine while executing from SOAP-UI software, but when the continuous integration (jenkins) execute it through the Maven Soap-UI plugin, I receive this error:
[XPath Match] junit/framework/ComparisonFailure
I guess there is a missing library somewhere but cannot figure what to do.
What is strange is that I do not refers any jUnit tests as I just call URL's of web-services.
Finally I found that there is a junit dependency to add with the help of this thread
Here is the dependency I had to add in my pom.xml file:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
</dependencies>
For the maven-soapui-plugin.
The whole config will looks like:
<plugin>
<groupId>eviware</groupId>
<artifactId>maven-soapui-plugin</artifactId>
<version>4.0.1</version>
<executions>
<execution>
<id>services-customer</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<projectFile>services/customer/smoke-tests.xml</projectFile>
<projectProperties>
<value>IdmpDataEndPointHost=${smoke.dataload.url}</value>
<value>WebServiceEndPointHost=http://${smoke.tomcat.server}:${smoke.tomcat.port}</value>
</projectProperties>
<outputFolder>${project.build.directory}/soapui-results/services/customer</outputFolder>
<junitReport>true</junitReport>
<testFailIgnore>true</testFailIgnore>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
</dependencies>
</plugin>

Using maven2 to build autotools-based C/C++ package

I am working on a collection MATLAB, Java, and C/C++ components that all inter-operate, but have distinctly different compilation/installation steps. We currently don't compile anything for MATLAB, use maven2 for our Java build and unit tests, and use autotools for our C/C++ build and unit tests.
I would like to move everything to a single build and unit test system, using maven2, but have not been able to find a plugin that will allow the C/C++ codestream to remain autotools-based and simply wrap it in a maven build. Having to rip out autotools support and recreate all the dependencies in maven is most likely a deal-breaker, so I'm looking for a way for maven and autotools to play nicely together, rather than having to choose between the two.
Is this possible or even desirable? Are there resources out there that I have overlooked?
I don't really know autotools, but can't you use the maven exec plugin, that lets you execute system commands (or Java programs)? For example:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>exec-one</id>
<phase>compile</phase>
<configuration>
<executable>autogen</executable>
<arguments>
<argument>-v</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
<execution>
<id>exec-two</id>
<phase>compile</phase>
<configuration>
<executable>automake</executable>
<arguments>
<argument>-v</argument>
<argument>[other arguments]</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I didn't test the pom fragment above, but it gives you some hints about how to proceed.
You did overlook the maven cbuild parent suite. take a look at the "make-maven-plugin" section for more details.