For this course, we are expected to implement tests to a Java program. For that reason, we will be using JUnit. As Eclipse is the framework I am familiarized with, it will we be the one I’ll use during this course. Also, Eclipse comes with an extension that allows the developer to create tests. In my case, I did not need to install anything else besides the Eclipse I already had.
First you create the Java class that you want to test. In this case, we will be using the class BoundedQueue.java that can be found in the codes of Chapter 3 from the course book.
For creating the test class we go to File >> New >> JUnit Test Case as shown in the image below.
The following window will appear:
In Source Folder, we make sure we are on the same folder as the class we aim to test; same goes for the Package. On Name, we write the name we want for our testing class. In this case, it will be BoundedQueueTest.java. On the field Superclass, we make sure it says java.lang.Object. And in Class under test, we select the class we want to test; in this case is BoundedQueue.java.
What I know so far, is that every method that you create is a test. The interface will only tell you if the test was passed or not. Also, every method is like a small main method in the class. For that reason, there is no communication between test methods.
So, we create a method called SetUp() in which we will instantiate those variables that we will be using along the class. Before this method, we need to write @Before (not sure why, if you don’t it marks it as an error). Eclipse will ask you to import a class in order to use this, so just do so.
Then we continue with the test methods; for which you need to write @Test before them (also not sure why). These methods can be named however you like, but I suggest to explain what you are trying to do in the name.
In the previous example, we tried to create take out an element of an empty BoundedQueue. When we run this, Eclipse will tell us that the test failed and why it failed:
For the complete code go to: https://github.com/florethge/Software-Quality-and-Testing/blob/master/UnitTestChapter3/BoundedQueueTest.java