Today is Monday, 20th May 2019

Archive for the ‘Eclipse’ Category


Sample Eclipse Project

This is a sample Eclipse project that you can download and import.

Download Eclipse Project



Java Eclipse Tutorial – Part 9: Running JUnit Tests

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 🙂

Download Source Code

 

# Video Length
1 Installing the Eclipse IDE 03:18
2 Create a Java Application with Eclipse 03:44
3 Exploring the Eclipse User Interface 04:50
4.1 Searching and Navigating Source Code (part 1) 04:18
4.2 Searching and Navigating Source Code (part 2) 06:06
5 Generating Java Source Code 05:12
6.1 Refactoring Code - Extract Constants and Variables 04:13
6.2 Refactoring Code - Extract Methods, Rename Methods and Variables 04:19
7 Debugging Java Source Code 04:34
8 Adding JAR Files 05:17
9 Running JUnit Tests 05:08
10 Importing Projects 01:45
11 Exporting Projects 01:34
12 Setting Eclipse Preferences 04:09
Total 58:27

 

Video Transcript

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.



Java Eclipse Tutorial – Part 8: Adding JAR Files

In this video tutorial, I will show you how to add JAR files to an Eclipse project. I will discuss the following topics:

  • JAR refresher
  • Adding a JAR file
  • Using Classes from JAR file

Please subscribe to this channel 🙂

 

Download Java Source Code

 

# Video Length
1 Installing the Eclipse IDE 03:18
2 Create a Java Application with Eclipse 03:44
3 Exploring the Eclipse User Interface 04:50
4.1 Searching and Navigating Source Code (part 1) 04:18
4.2 Searching and Navigating Source Code (part 2) 06:06
5 Generating Java Source Code 05:12
6.1 Refactoring Code - Extract Constants and Variables 04:13
6.2 Refactoring Code - Extract Methods, Rename Methods and Variables 04:19
7 Debugging Java Source Code 04:34
8 Adding JAR Files 05:17
9 Running JUnit Tests 05:08
10 Importing Projects 01:45
11 Exporting Projects 01:34
12 Setting Eclipse Preferences 04:09
Total 58:27

 

Video Transcript

Time – 00:00
Hi, this is Chad (shod) with luv2code.com. Welcome back to another tutorial on Eclipse. In this video you will learn how to add JAR files to an Eclipse project. I will cover the following topics: JAR refresher, Adding a JAR file and using classes from JAR file. Let’s get started.

Time – 00:23
You can use JAR files to add functionality to your application. There are Java libraries available that are created by 3rd party groups. The most popular is OpenSource Software. The Java libraries are packaged as JAR files. A JAR file is basically a collection of compressed Java class files.

Time – 00:40
In this example, I want to add a StopWatch functionality to my application. When the application is running I would like to find our how long does a certain section of code take to run. Apache Commons provides the StopWatch class. This class had methods to start and stop and you can manage via your code. The documentation for the StopWatch class is available here at Apache’s website.

Time – 01:03
I’ve created a very simple Eclipse project that only has one class right now. It has a main method that will display the word running then it will perform a lengthy process which is basically a call to this method here, that just does a sleep for 3 seconds and then will print out finished. What I’d like to do is add some real StopWatch functionality to this application.

Time – 01:21
The first thing I need to do is I need to download the JAR file from the Apache website. Let me move over to my browser and I have a bookmark set up for it now but basically you want to go to this URL, commons.apache.org/proper/commons-lang/. Once you’re at this site, there’s a link here for download. I select download then I move down to binaries and I choose the binary that I want. I choose commonslangbin.zip. This will ask me to save it to my file system. I’ll go and select yes and I will save to my local file system. I can go look at my downloads directory and I’ll see that this file is now been downloaded to my local file system.

Time – 02:02
I have the file, now I need to extract it, so I just double-click, extract this file, now we have this directory and it has this one file here that I want, commonslang.jar. I copy this file and paste it over in my Eclipse project. First, I start to right-click, I’ll select copy, move over to the Eclipse project, I move to the root folder of the project, and I’ll right-click and select paste. Now we have this file in our project. At this point, Eclipse is not aware that this is a JAR file that we’re going to use in our program. We must explicitly tell Eclipse that this is a new Java library for our project. We can accomplish this with the following steps.

Time – 02:43
I can right-click on my project, choose properties, then I can move over to Java Build Path and then I can select the tab for Libraries. This is where we add additional JARs or additional libraries to our project. Here I’ll select the option Add JARs and I will expand the folder here for our project and I select this JAR file commonslang.jar. That’s the same one that we copied over earlier. I’ll hit okay and I’ll select okay. Now that we have the Commons Lang JAR file added to our project, I want to make use of the StopWatch class that’s defining that JAR file. What I’ll do here is I create a new instance of the StopWatch.

Time – 03:27
Here I have StopWatch, mystopwatch, it was a new StopWatch. But note here, Eclipse has the red underline saying that, “Hey, there’s a problem.” It can’t resolve the type. If I simply float over that, notice it gives you a list of quick fix options. These quick fix options is basically where Eclipse will go and try to figure out how to solve your problem. I like the first option that it give me here, that’s import StopWatch, that’s defined in org.apache.commons.lang3.time. Hey, that’s correct. I select this option and then if I move to the top, we’ll see that Eclipse added the import statement for me automatically and the error message have gone away, very nice.

Time – 04:05
Now I’ll go head and make use of my StopWatch variable that I have. I’ll say mystopwatch. and then I’ll call the start method, that will start the stop watch and then afterwards I’ll say, mystopwatch.stop to stop it. Now I want to do cout print line and I want to print time is, and I say mystopwatch.gettime and this will give us the time in milliseconds. Let’s run the program and check our output. Great, so it displayed the time that it took to run the program which matches our correctly because our lengthy process method had a 3-second delay, works out the 3,000 milliseconds so this looks very good. Our stop watch is working as desired.

Time – 05:11
This wraps up our video on JAR files. You learn how to add JAR files to your Eclipse project. 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.