DEV Community

Cover image for Introduction to Unit Testing with Java

Introduction to Unit Testing with Java

Christian Vasquez on April 22, 2018

_Cover image by [Hans-Peter Gauster](https://unsplash.com/@sloppyperfectionist) on [Unsplash](https://unsplash.com/)_ We all have been there: we s...
Collapse
 
itzsaga profile image
Seth

Great intro. I hit an error on line 11 of the final code. I get an Error:(11, 16) java: Math() has private access in Math. IntelliJ's linter is yelling about it as well. My Java knowledge is minimal so I'm wondering how would I fix this error? I'm guessing it has something to do with line 2 of Math.java.

Collapse
 
chrisvasqm profile image
Christian Vasquez • Edited

Thanks for letting me know, Seth!

That's my fault.

Try removing this from the Math.java file:

private Math() {}

The entire class should be like this now:

public final class Math {

    public static int add(int firstNumber, int secondNumber) {
        return firstNumber + secondNumber;
    }

    public static int multiply(int multiplicand, int multiplier) {
        return multiplicand * multiplier;
    }

    public static double divide(int dividend, int divisor) {
        if (divisor == 0)
            throw new IllegalArgumentException("Cannot divide by zero (0).");

        return dividend / divisor;
    }
}

In case you or someone else also wonders why, the private Math() {} refers to the constructor of our Math class, I made it private at the beginning because all it's methods are static, which prevents anyone from trying to instantiate it. But later on I decided to also add an example where we had the need to use an object and I forgot to update it hahaha.

Collapse
 
itzsaga profile image
Seth

That works. Thanks!

Collapse
 
khmarbaise profile image
Karl Heinz Marbaise

Hi, just a small hint. In case you add a dependency in Maven which is only intended for testing, which TestNG is, you should do it like this:

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.14.2</version>
    <scope>test</scope>
</dependency>

Apart from that if suggest to name a test class *Tests.java you have add an example to use the most recent versions of maven-surefire-plugin (2.21.0+). Otherwise this will not being executed as test. The best it to name it *Test.java this will work with older versions well..

Collapse
 
chrisvasqm profile image
Christian Vasquez

Thank you, Karl!

That's really helpful 😄

Collapse
 
stealthmusic profile image
Jan Wedel • Edited

Just to give some other options: We’ve just started using JUnit 5, the best thing is actually @DisplayName to price a readable test name. Also, we switched to AssertJ that has a pretty neat fluent API.

Collapse
 
mmarinez20 profile image
Manuel Mariñez

Very well explained as usual (> ._.)> Kuddos!. This is really helpful since Im trying to implement a new testing framework for the folks at work, wish you luck and here your reward.

Collapse
 
chrisvasqm profile image
Christian Vasquez

Hahahha, thanks Manuel!

I'm glad you found it useful 🤓

Collapse
 
johancamilol profile image
Johan Camilo Lopez

Great post

Collapse
 
gaous profile image
Gaous Muhammad Saklaen

Great article!

Collapse
 
capochiani profile image
Francesca Capochiani 🌺

Hey Christian,

Which IDE do you prefer? :)

Good article!

Collapse
 
chrisvasqm profile image
Christian Vasquez

Hi Francesca, I would say that IntelliJ IDEA is my favorite overall. But I've also found text editors with plugins to be really good as well with a couple of plugins like VS Code.

Collapse
 
navonf profile image
Navon Francis

Awesome guide. This is a great refresher for me as I have not wrote some unit tests in a while 😬

Collapse
 
mohammad747 profile image
mohammad hashemi

One of the best articles that I found on the whole web, Thank you, sir.
But I got "Error:(3, 34) java: package org.graalvm.compiler.debug does not exist" when I type expectedExceptions.

Collapse
 
chrisvasqm profile image
Christian Vasquez

Hey Mohammad,

Thank you very much!

I'm not entirely sure what might cause it, but it seems you are missing a dependency.

In case it might help you, here's a repository with the project I used while making this article: github.com/chrisvasqm/intro-unit-t...

Collapse
 
prabhsinghb profile image
prabh-singh-b

This is a great post. thank you very much Christian Vasquez

Collapse
 
tulja profile image
tulja

Great Article, Thank you!