Skip to content
loading...

Introduction to Unit Testing with Java

Christian Vasquez on April 22, 2018

Cover image by Hans-Peter Gauster on Unsplash We all have been there: we spent so much time into a project, we make sure to run each and every p... [Read Full]
markdown guide
 

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.

 

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.

 
 

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..

 

Thank you, Karl!

That's really helpful 😄

 

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.

 

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.

 

Hahahha, thanks Manuel!

I'm glad you found it useful 🤓

 

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

 

Hey Christian,

Which IDE do you prefer? :)

Good article!

 

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.

 

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...

 
 
 

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

 
code of conduct - report abuse