Integration Test Coverage with Sonar

I love to develop test driven, but even more I love code analysis tools that show me what to improve. Sonar is quite “popular” and common for Java developers and I’m using it already for years in different projects.

If you want to see integration test coverage, you have to set the jacoco maven plugin together with the fail safe plugin in your maven config (pom.xml):

    <build>
       <plugins>
            <!-- integration tests -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- for integration test coverage in sonar -->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>jacoco-initialize</id>
                        <goals>
                            <goal>prepare-agent-integration</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <properties>
      <!-- needs to be set for jacoco's forked process -->
        <argLine>${env.MAVEN_OPTS}</argLine>
    </properties>

argLine is used as additional param in the javaagent command line section jacoco uses (needs to be set, otherwise the maven opts are not set for the forked process – OutOfMemoryException and so on…). See jacoco’s documentation for details.

The result in Sonar is pretty amazing:

sonar-jacoco

Advertisements

Thread dumps, memory dump, and stack traces

1. Getting a Stack Trace

jstack -l [pid]

jstack is for java processes

2. Getting a Thread Dump

kill -3 [pid]

on unix

3. Surviving a OutOfMemoryException in Production

Sometimes you see an “OutOfMemoryException” in production and you need a memory dump of this process now to analyze the memory usage

jmap -J-d64 -dump:live,format=b,file=solr-memory-dump.bin [pid]
jmap generates a memory dump file solr-memory-dump.bin that can be used for further analysis

jhat -J-d64 -J-Xmx4096m solr-memory-dump.bin
jhat opens HTTP server on port ‘7000’