DZone
DevOps Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > DevOps Zone > Multiple Libraries for Assertions in Your Test Classpath

Multiple Libraries for Assertions in Your Test Classpath

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

Michal Davidek user avatar by
Michal Davidek
·
Apr. 29, 16 · DevOps Zone · Tutorial
Like (4)
Save
Tweet
2.43K Views

Join the DZone community and get the full member experience.

Join For Free

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 AssertJ. Mockito (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
@Test
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());
            }
        }
    }
}
Testing Assertion (software development) Library Classpath (Java)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Basic Convolutional Neural Network Architectures
  • Maven Tutorial: Nice and Easy [Video]
  • Modernize Legacy Code in Production: Rebuild Your Airplane Midflight Without Crashing
  • What Is HttpSession in Servlets?

Comments

DevOps Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo