DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

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 · Tutorial
Like (4)
Save
Tweet
Share
2.58K 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

  • Load Balancing Pattern
  • TDD: From Katas to Production Code
  • Top 10 Best Practices for Scaling Your Application: Expert Tips for Optimizing Performance and Ensuring Reliability
  • How to Create a Dockerfile?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: