Running on Java 17+35-2724 (Preview)
Home of The JavaSpecialists' Newsletter

2.16 Creating an ArrayShufflerTest

Juppies 2 by Dr Heinz M. Kabutz

To create a test class for our ArrayShuffler, we click on the name of the class and press our magic key ALT+Enter. One of the options is to create a JUnit 4 test class. (At the time of recording, JUnit 5 was already available, but since the rest of the project used JUnit 4, we chose to stay with that.)

The test class has the same package as the class we are testing, and the name is the same, except with "Test" at the back. Thus, we get the ArrayShufflerTest. We can select "Default Method Bodies" which creates the test class with a method testShuffle(). This generated method already has some sample code that we can modify to write our test. It also adds a constructor for the class (remember that a constructor is like a method that is called when we make a new object with "new". It has the same name as the class we are constructing and no return type.) Since the constructor does not do anything, we can delete it. No idea why NetBeans created that empty constructor for it - but now it's gone.

Our testShuffle() method creates an int array with values 0 through 9. It then calls shuffle and checks whether any of the indexes are different. We do this by assuming that the array is not shuffled and that none of the values in the array are different to the indexes. We thus declare a boolean variable "different" which we set to false. We then compare all the indexes in the array to the values and if any of them differs, then we set the boolean variable to true. Once we have looked at all the indexes we confirm that at least one of the values is different, thus that "different" is true. If "different" is still false, then they are all the same, and we fail.

There is a possibility that even though we have shuffled the array, that it is the same as before, but that is rather unlikely. For that to happen, the random would have to return the sequence 9, 8, 7, 6, 5, 4, 3, 2, 1, thus keeping each element in its place. By increasing the size of the array, we could reduce the probability of such a sequence to be infinitesimally small.

When we run the testShuffle() method, we see that our test passes. That's what we want. But we confirm that the test is running correctly by commenting out the call to shuffle() the array by adding two forward slashes at the start of the line - //. When we comment out a line of code, the code is not executed. When we run it again, we see that the test now fails. Satisfied, we uncomment the shuffle() call and are ready for the next step.

Comments

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Table of Contents

We hope you enjoyed this tutorial. If you did, you will also enjoy our courses. We suggest you start with Extreme Java - Advanced Java, followed by Extreme Java - Concurrency Performance for Java 8.



About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack 21

Superpack 21 Our entire Java Specialists Training in one huge bundle more...

Free Java Course

Free Juppies 2 Course
Juppies 2 - a course for complete beginners more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...

Java Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.