In this video tutorial, I will show you how to run JUnit tests Eclipse.
I will discuss the following topics:
• Create a Unit Test
• Running the Test
Please subscribe to this channel 🙂
Time – 00:00
Hi, this is Chad (shod) with luv2code.com. Welcome back to another tutorial on Eclipse. In this video, you’ll learn how to use JUnit in Eclipse.
Time – 00:12
I’ll cover the following topics: I’ll show you how to create a unit test and also how to run a unit test. Okay. Let’s get started.
Time – 00:23
We need to create a method to mask a credit card number. The credit card number is a 16-digit number. We only want to show the last 4 digits, and we’ll replace the other digits with an X. See the input number and the output number.
Time – 00:35
To test the code, we’ll make use of a testing framework called JUnit. This is available at junit.org. Eclipse has built-in support for JUnit, so there is nothing additional we have to download. In order to use JUnit, we have to create a test case. Inside of the test case, we’ll setup the input, we’ll execute the method, and then compare the result against the expected output. When you run the test, it will give you a pass-fail indicator. If you pass, you’ll see the green bar. If you fail, you’ll see the red bar. Of course, our goal is to see the green bar.
Time – 01:09
For this tutorial, I have an empty project. What I’ll do first is create a simple step for the method. The method will be called Mask and it will take a string for the credit card number. When doing unit testing, you’ll provide just a simple implementation of the method. Then, you’ll come back and you’ll add the real functionality to the method later.
Time – 01:31
Now, let’s add a new unit test for this. First, we’ll add a new source folder and call it test. Then, we’ll also add a new package using the same package name as our class. Then, we’ll go through and add an actual JUnit test called CreditCardUtilsTest. We’ll select the actual class that we’re going to test against CreditCardUtils, and we’ll keep all of the other defaults and then we’ll simply hit Finish.
Time – 02:16
Now, let’s add some code for this actual test. The first thing we’ll do is we’ll setup with the actual credit card number that we’re going to use for testing. Stub out some coding for executing assert. Here, I’ll go through and I’ll execute using this credit CreditCardUtils.mask method.
Time – 02:38
I’ll pass in the credit card number. Then, I’ll come over here and I’ll setup the expected value that should be returned, and then I’ll check to see if those two values are equal, so I’ll do assertEquals and give the expected value, the actual result.
Time – 02:59
Now, let’s go ahead and run the test. We’ll just do a right click. We’ll select Run As and JUnit test. In the top left corner, you’ll see the results. No, we have the red bar meaning that we failed, and we expect that because we didn’t put any real implementation code in our CreditCardUtils class.
Time – 03:18
All right, so let’s go ahead and add some real code. I’ll move into this method. I’ll remove that old piece of code and now I’ll say return. I’ll give the mask of the first 12 x’s and then I’ll do a substring, and I’ll get the last 4 characters of this number.
Time – 03:43
Now, what I like to do is go ahead and run the unit test. I’ll right click, say Run As, JUnit test, and then I’ll just check for the results in the top left corner, and wow we have a green bar. That’s great, so our unit test passed.
Time – 03:58
However, right now our unit test passed a very simple case. What happens if we give it bad data? Like we pass on the no value. What happens? I’m going to add another unit test here called test_nulls, and this will basically set the credit card number to null and it’ll call the method.
Time – 04:12
Now, this should give us an IllegalArgumentException. We have that setup as expected for this test, so let’ go ahead and save this and then we’ll run it and we’ll see what the results are. Run As, JUnit test, and now on the top left we have red, okay. One of the tests failed, so we passed in a null value and it didn’t like that no value. We need to fix this, so how can we do that?
Time – 04:38
In our code, we need to check the credit card number and see if it’s null. If it’s null, we’ll return a IllegalArgumentException and we’ll tell the user they must provide a credit card number. Let’s run our test one more time. Right click, say Run As, JUnit test, and then we’ll check the results in the top left, and good job. We have all green, so that means our unit test passed.
Time – 05:01
This wraps up our video on running JUnit testing Eclipse. Please subscribe to our channel to view more videos on Eclipse and Java. Click the thumbs up to like our video. Also, visit our website luv2code.com to download the Java source code used in this video.