To obtain the most recent version of JUnit, you can download and install it from the http://junit.org/junit4/ website. As you can see, at the time of this series they are working on JUnit5, so check for the latest version before downloading. From here we can download the most recent version of the JUnit jar file and the Hamcrest file.
- [Voiceover] Let's review how to download and install JUnit. To obtain the most recent version of JUnit, you can download and install it from this website at junit.org/junit4/ As you can see, at the time of this series they are working on JUnit 5. So check for the latest version before downloading. Here it says "Work on JUnit 5 - Milestone 1 has started." From this page we can download the most recent version of the JUnit JAR file.
Let's click on "Download and Install" under the "Welcome." As you can see, under the "Plain-old JAR," where JAR stands for Java Archive, we can download the following JARs and put them into our test classpath. There are two files here: junit.jar and hamcrest-core.jar The Hamcrest file is used in the example provided by this JUnit website, but if you're not going to use any of its classes and methods, you don't need to download it.
The Hamcrest framework extends the capabilities of JUnit testing to include what they call Matchers. In other words, you now have a type of assert statement that says "assert that something equals something else," which is a little different from the assert in JUnit. For this tutorial I'm concentrating on JUnit, but the Hamcrest extension is worth taking some time to review and becoming familiar with its adding capabilities. You can get more information here at hamcrest.org/JavaHamcrest/ For now, I'm gonna download these two files.
I'll click on junit.jar and I'll download the most recent version 4.12. On the right hand side under download I'm going to download the JAR file. I have a message. It says "This type of file can harm your computer." I'm going to say "Keep". And now I'll go back and download the Hamcrest-coreJAR file as well. I'm gonna do the same thing and get the most recent version of the JAR file. I have the same message. I'll click "Keep".
Now both files are downloaded. Next, I'm gonna move them from my download folder to a new folder I'll create called "Junit-example". Let's do that now. I'm gonna use "File Explore" to create my new folder and move these files. So on "File Explorer" I'm gonna go to my "C" drive. Users/Producer I'm gonna right click and create a new folder. The new folder's called "JUnit-example".
Now, I'll go to my downloads and I'm gonna highlight both files right click and copy. I'm gonna go back over to my "JUnit-example" folder and I'm gonna right click and paste. That way I can go ahead and create a program in this folder to show you how to use the "JUnit" framework. Once these files are downloaded you do need to make sure you take time to update your class path to point to the "JUnit-4.12.JAR" location.
Now I'd like to go back to the download page. If I scroll down you'll see there's also information about how to use "JUnit" with "Mavin". If you're using "Mavin" for your projects you can add the dependency for "JUnit" by simply including the group ID "JUnit" the artifact ID "JUnit" and in this case, my version is 4.12. That needs to match the version that you downloaded. And the scope of this dependency should say "test".
At this point, that's all I need to do to be able to use "JUnit". "JUnit" can be used as part of an "IDE", an integrated development environment, or from the command line. If you run from the command line that's one of the reasons we needed to download the two "JAR" files.
- What is JUnit?
- Comparing values with assertions
- Using JUnit with different Java IDEs
- Creating basic unit tests
- Testing for exception handling
- Creating parameterized tests