How to test compatibility among difference version of library in Java

Sorravit Bunjongpean
4 min readMay 16, 2021

TL:DR You can use maven profile to switch between dependencies versions

I happen to come across a compatibility problem with older version of lib in my project so I have to find a way to test it.

So, I come up with a solution on testing difference version of library against the same test case in the same project by using maven profile.

I will demonstrate how I do it with an example project here. (The naming might not make so much sense but again it’s just an example project)

First,I create two projects.

  1. Common
  2. UserService

UserService will use a model from Common library.

In version 1.0-SNAPSHOT we have a module with no toString method in Common.

and a simple application which takes Json and convert to object

Which will get you a simple result with no error.

Now Let’s say I want to actually see what’s in the module. So I create the toString and call it version 1.1-SNAPSHOT

Then I upgrade the dependency in UserService Module

Run the test without having to change anything.

No issue with the test.

Now run the main class, we will get a nice looking result

Ok even if I didn’t upgrade Common version to 1.1 in my UserService It wouldn’t be an issue it wouldn’t break anything.

Now imagine that Somehow I have to change the name of the field like lastName to surname and call it 1.2-SNAPSHOT

The project will looks like there’s no issue (except in tests) but when you run you will get UnrecognizedPropertyException (of cause this is intentional)

And If I want to switch back to the old version I have to manually goes to pom file and update version number (And perhaps reload maven)

OK, that’s the introduction for the test project which I’ll be using in this article.

Setting up

Here’s the code for my test project.

Make sure to build version 1.0, 1.1, 1.2 and store it in your local repository before proceed to the solution step.

cd common
git checkout tags/1.0
mvn install
git checkout tags/1.1
mvn install
git checkout tags/1.2
mvn install
git checkout main
cd ..

Solution starts here

Create a profile in maven

*Note: maven tab in IntelliJ before Using profile

Add profile to pom

<profiles>
<profile>
<id>Version 1.2</id>
<dependencies>
<dependency>
<groupId>com.sorravit.example</groupId>
<artifactId>common</artifactId>
<version>1.2-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>Version 1.1</id>
<dependencies>
<dependency>
<groupId>com.sorravit.example</groupId>
<artifactId>common</artifactId>
<version>1.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</profile>

<profile>
<id>Version 1.0</id>
<dependencies>
<dependency>
<groupId>com.sorravit.example</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</profile>
</profiles>

Clicl in the reload Button in maven

You will get this

To switch between them, Click on the checkbox in front of the profile name

You can run test using right click and run in test folder, run using maven like the picture above or use command line what ever method you prefer.

mvn test -P"Version 1.2"

When you run test with the current main branch Version1.0 and 1.1 should fail and Version 1.2 should be the only version that pass. Feel free to edit test test to make Version1.0 and 1.1 pass by changing “surname” back to “lastName”

String json = "{\"name\":\"first\",\"lastName\":\"Last\",\"age\":12,\"workPlace\":{\"name\":\"work Place\",\"address\":\"Address\"}}";

Now you can run difference version of lib by only changing the checkbox

If you have to run it in a CI/CD and you need to test with multiple version you could do something like this

mvn test -P"Version 1.0"
mvn test -P"Version 1.1"
mvn test -P"Version 1.2"

THE END

--

--