Multiple Libraries for Assertions in Your Test Classpath

DZone 's Guide to

Multiple Libraries for Assertions in Your Test Classpath

A brief tutorial on how to set up tests in Java with IntelliJ IDEA.

· DevOps Zone ·
Free Resource

You have multiple libraries on your test classpath providing some assertion classes by default. Real problem is, that you are unable to avoid multiplicity in the assertions libraries due to fact, that mainly used frameworks used in Java world - JUnit and TestNG - have built-in assertions classes. These classes often does not offer best way how to asserts so you have temptation to use another one even for simplest assertion. The most used is in my opinion AssertJMockito (and other mocking libraries) brings another set of assertions. With spring it comes another set of assertions. And have {guava-link} in your classpath as well? Another Assert class there. Are you geeky and have google truth on your test classpath as well? Well, it’s obvious that you have four or five classes which has even same name - Assert. It’s really annoying to even static import right assert with such count of same classes providing similar API.

I am using Intellij IDEA and when you are static importing some class, you have a choice to disable providing a particular class. This comes in handy for newly written tests, but when you are facing older tests you don't have such a choice because there are asserts from another library. It’s clear that if you would like to have a consistent way for asserting a class, you will need to rewrite all the tests which are not passing your criteria and clear your dependencies. This is step one.

Step two is ensuring that your test codebase follows coding standards, but static analysis tools are unable to do this hard work for you. So you need a specialized test (quality assurance test) to check that your test codebase meets the criteria set by coding standards. In one project we have written tests for checking the correct annotations of test classes for integration and unit tests. Also, there can be a test which verifys that you are not importing assertions other than preferred libraries — it’s not so easy to get rid of non-preferred libraries from dependencies in one step.

The test below:

  1. Reads all the test classes from package
  2. Finds if there are dependencies for Google Truth
  3. Prints a message on which classes aren’t following coding standards
  4. The whole project with dependencies can be found here
public void shouldVerifyOnlyOneAssertionLibraryInAllTheTests() throws Exception {
    DependencyVisitor v = new DependencyVisitor();
    for (Class<?> aClass : classes) {
        String resourceName = "/cz/mikealdo/tests/" + aClass.getSimpleName() + ".class";
        InputStream resourceAsStream = aClass.getResourceAsStream(resourceName);
        new ClassReader(resourceAsStream).accept(v, 0);
        Set<String> classPackages = v.getPackages();
        for (String classPackage : classPackages) {
            if (classPackage.contains("com/google/common/truth")) {
                System.err.println("Google Truth library is not allowed for using in class " + aClass.getSimpleName());
classpath, java, jvm

Published at DZone with permission of Michal Davidek , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}