Previously I wrote an article about building C/C++ applications with maven and maven NAR plugin. However that article left out the part of running the unit tests, therefore I’ll present in this article a way of integrating the build process with Google’s unit test framework. So basically will show you how to quickly integrate maven NAR plugin with Google’s unit test framework.

First, since google test is a third party library and it does not seem to be mavenized, we have to do this by ourselves. For this, the easiest way is to download the library sources and turn them into a nar maven dependency which will reference from the projects we want to test with. The latest version (at this time) can be downloaded from this link. Once downloaded, you’ll need to extract the header & the source files into the specific NAR project structure (follow this link to see the required structure of a NAR project). Once this is done, there are two more things to be taken care of. First would be to create a pom.xml file, to build and package the library into a maven artifact (which can be either installed locally, added as a module to your project, or put in your company’s artifact repository for later dependency resolving). For this here is a sample pom.xml file, which should be edited and changed according to your preferences and naming conventions:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>com.pixelsrising</groupId>
    <artifactId>thirdparty</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<groupId>com.pixelsrising.thirdparty</groupId>
    <artifactId>gtest</artifactId>
    <packaging>nar</packaging>
<version>1.0-SNAPSHOT</version>

<properties>
    <skipTests>true</skipTests>

    <!-- parent POM will disable deployment to make sure no useless artifacts will be deployed
    however we reenable deployment here, to be able to deploy new builds of the engine in
    artifactory -->
    <skipDeploy>false</skipDeploy>
</properties>

<build>
    <defaultGoal>install</defaultGoal>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <skip>${skipDeploy}</skip>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-nar-plugin</artifactId>
            <version>2.1-SNAPSHOT</version>
            <extensions>true</extensions>
            <configuration>
                <layout>NarLayout20</layout>
                <libraries>
                    <library>
                        <type>shared</type>
                    </library>
                </libraries>
                <linker>
                    <sysLibs>
                        <sysLib>
                            <name>pthread</name>
                        </sysLib>
                    </sysLibs>
                </linker>
            </configuration>
        </plugin>
    </plugins>
</build>

<distributionManagement>
    <snapshotRepository>
    <id>yout snapshot repository id here</id>
    <name>yout snapshot repository name here</name>
    <url>yout snapshot repository url here</url>
    </snapshotRepository>
</distributionManagement>

</project>

You will notice is a fairly usual NAR pom.xml file, the only addition being adding the linker section to the plugin config. This is required since the unit test library depends on the pthread library.
The second remaining thing would be to fix the compile errors and build/install/deploy the artifact. You can remove the gtest-all.cc file since the build process will include the required files anyway. Then you will need to fix the include references so the project will build correctly.

This being completed, you now have the gtest library as a maven artifact. You can add it now as a dependency to other projects, and you can write tests with it. To adding it as a dependency, you will need to add it something like this:

<dependencies>
    <dependency>
    <groupId>com.pixelsrising.thirdparty</groupId>
    <artifactId>gtest</artifactId>
    <version>$1.0-SNAPSHOT</version>
    <type>nar</type>
    </dependency>
</dependencies>

once this done, you can start using it in tests, like this:

#include <gtest/gtest.h>

TEST(dummyTest, AllwaysTrueTest) {
    EXPECT_EQ(1, 1);
}

However in order to be able to actually run the tests, they should be added in the pom.xml file in the config section of the maven-nar-plugin (the example shows how to add the dummyTest):

<tests>
    <test>
        <name>dummyTest</name>
        <link>shared</link>
    </test>
</tests>

Please note however that is sufficient to add only one test, as it seems like google test framework will run all the tests anyways.

A sample module based github repository featuring a library project using googletest and the googletest itself as a module can be found at this  link. The sample was built and run with netbeans.

Happy unit testing