101 on JUnit and Unit testing
Credits to @bryanminear
JUnit is a testing framework/library for Java (programming language). It is used for both unit testing and UI testing. This helps us define the flow of execution of our code.
What is Unit Testing?
"Unit testing is the process of examining a small "unit" of software (usually a single class) to verify that it meets its expectations or specification" (Washington Courses, 2019)
Setting up JUnit
By downloading the newest version of Eclipse, you already have JUnit installed.
If not here's a guide on how to set it up: Tutorial
Creating a JUnit Test Case in Eclipse
To use properly JUnit you need to create a separate .java file. In this file is were you are actually going to test your class, so name it something significant for example, BoundedQueueTest since in the class im going to test is named BoundedQueue.
**I'm taking this class from the Introduction to Software Testing book by Paul Ammann and Jeff Offutt.
In the left menu of Eclipse right click on the main class you're testing and select New, then JUnit Test Case.
A new menu will appear and make sure you select New JUnit 4 Test and select the Next option.
After this another menu of method names will show. In this menu you can choose which methods of your main class you want to test. If you can't find one that suits your test criteria you can always add more manually later.
After this you'll have your Test created.
Eclipse already generates the name of the methods you selected to test like seen on the photo below.
To actually test the method you just need to follow the actually flow of the methods established on the class. Don't worry if the test doesn't pass because if it doesn't you might just found a flaw in the method and you can always improve your code.
Running a JUnit Test
It's very simple just select the Test .java class and click on the little Arrow that's beside the Play green button. Select the Run As option of the menu and finally select the JUnit Test option.
Now you've successfully ran the test. A new side menu called JUnit will apear were you can see the results of your test cases.
They will appear in green if the test for that method passed and in red if they failed.
In the button you can see a time of console with debug or error messages that can help you figure out why the test failed of passed.
GitHub Repository
References:
https://courses.cs.washington.edu/courses/cse143/11wi/eclipse-tutorial/junit.shtml
Comments
Post a Comment