Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Exploring Eclipse support for JUnit

In this article we will see a complete overview regarding JUnit unit testing development using only Eclipse

It is well-known to all developers that one of the basics factors to consider during the development of any software, small or big, or any application for that matter, is the testing procedure that verifies and validates it. Now, testing too can occur in various forms and undergo different procedures.

So, how does one verify software or applications? One of the basic methods is to make use of a test suite or software that allows us to test other software/applications. We shall take a look at some of this and also run a simple test procedure by using JUnit.

Software tests – What are they?

Software tests are basically carried out with a piece of software that results in the validation of the code results and also the sequence of execution of the code. The former is called state testing, with the latter being called sequence testing.

How do software tests help?

Unit testing of software allows coders and developers to verify if the logic they have implemented in creating a program is correct or if it needs change. Having a high amount of test coverage of the code allows a developer to add features to their software, without the worry of having to perform manual tests after every step.

What are the various types of testing?

Testing is a very broad term that consists of several types of test methods. Some of the more common ones are –

  • Unit Testing – Unit testing implies the testing if a small portion of the code that provides certain functionality to the entire program. In this regard, any kind of external dependency that may be present is removed.
  • Integration Testing – An integration test, also referred to as a functional test, is one where we test the behavior of many different components together. The use of integration testing is to verify if the entire system as a whole is working together.
  • Performance Testing – The benchmarking of software components is achieved by means of performance testing.

Testing frameworks for Java

If you are a Java software/application developer and want to test your code, it is certain that you will be looking for a framework that can assist you. In this regard, JUnit and TestNG are perhaps two of the most popular test units available for Java. However, in this article, we will keep ourselves to the use of JUnit.

What is JUnit?

JUnit happens to be a Java library that allows one to perform unit testing of Java code. Usually, JUnit is something that isn’t actually a part of Java standard class libraries. However, it happens to be included with Eclipse.

So, if you are someone that does not make use of Eclipse, you can always get it from the JUnit website.

Using Eclipse and creating a JUnit test case

For using JUnit, you first need to make a different .java file in the project, which will test one of the existing classes.

  • In the Eclipse window, go to the Package Explorer area on the left side. Then right click on the class that requires testing and further click on JUnit Test Case.

New JUnit Test Case

  • Soon a dialog box pops up that allows for the creation of the test case. However, note and ensure that the option chosen at the top is JUnit 4. Then click on Next.

New JUnit Test

  • Next, you will encounter a number of methods that can be checked for testing. Eclipse also helps you in creating ‘stub’ test methods that can be filled in later. Click on Finish.

Creating a new stub

  • JUnit now asks you if you wish to attach its library to the project. Select ‘perform the following action: Add JUnit 4 library to the test path’ and finally press OK.

adding junit libs

Once you are done with this step, what you will have is a JUnit test case file. You should change the import org.junit.Test; statement to import org.junit.*;

Writing of JUnit tests

Every JUnit test file should test the particular behavior of any class that happens to be under the test. Say, a certain ArraylistTest can be used to check if array elements can be added to a list and then retrieved. As such, testing methods should certainly be short and target only specific aspects.

Assertions are used in the testing of JUnit. These are nothing but statements that check whether conditions are true or false. If all of the assertions in the method happen to be true, then the method under test is said to pass.

Now, JUnit provides many different methods that may be used for assertion. These have been listed for you.

 assertTrue(test) 
 assertTrue("message", test) 
 

– The test method fails provided the given Boolean test is not true.

 assertFalse(test) 
 assertFalse("message", test) 
 

– This test method fails if the given Boolean test is not false.

 assertEquals(expectedValue, value) 
 assertEquals("message", expectedValue, value) 
 

– If the two values provided are not equal to one another, this test method fails. The first value is meant to be the value that you expect as a result while the second value is the actual resultant value obtained from the test. The Equals method is used if these are objects.

 assertNotEquals(value1, value2) 
 assertNotEquals("message", value1, value2) 
 

– If the two values provided are equal, then this test method fails.

 assertNull(value) 
 assertNull("message", value)   
 

- This particular test method fails if the given value is not null.

 assertNotNull(value) 
 assertNotNull("message", value)  
 

- The test method will fail if the given value happens to be null.

 assertSame(expectedValue, value) 
 assertSame("message", expectedValue, value) 
 assertNotSame(value1, value2) 
 assertNotSame("message", value1, value2) 
 

- This assertion statement works along the same lines as assertEquals and assertNotEquals. However, there is a slight difference in the sense that it makes use of the == operator to compare, rather than using the equals method.

 fail() 
 fail("message") 
 

- This assertion causes the test method to fail.

A small example

Here is a simple example that makes use of the assertions mentioned above.

  ArraIntList List = new ArraIntList();
  List.add(43);
  List.add(-4);
  List.add(18);
  List.add(100);
  assertEquals(4, List.size());
  assertEquals(17, List.get(2));
  assertTrue(List.contains(-3));
  assertFalse(List.isEmpty());
  

When making use of comparison assertion, we usually write the expected values to the left and the actual calls on the right. This serves a certain purpose. As such, if a test was to fail, then JUnit returns an error message such as “expected 4, but found 0.”

Here’s another list of tests that can help you better understand.

Listing 1: Testing sets

  @Test // use of annotations that mark methods as tests
  public void TestAddandGet1() {
      ArrayIntgList List = new ArrayIntgList();
      List.add(44);
      List.add(-8);
      List.add(17);
      List.add(91);
      assertEquals(44, List.get(0));
      assertEquals(-8, List.get(1));
      assertEquals(17, List.get(2));
      assertEquals(91, List.get(3));
      assertEquals("second try", 44, list.get(0));   
      assertEquals("second try", 91, list.get(3));
  }
  @Test
  public void TestSize1() {
      ArrayIntgList List = new ArrayIntgList();
      assertEquals(0, List.size());
      List.add(42);
      assertEquals(1, List.size());
      List.add(-3);
      assertEquals(2, List.size());
      List.add(17);
      assertEquals(3, List.size());
      List.add(99);
      assertEquals(5, List.size());
      assertEquals("second try", 4, List.size());   
  }
  @Test
  public void TestisEmpty1() {
      ArrayInList lists = new ArrayInList();
      assertTrue(lists.isEmpty());
      lists.add(44);
      assertFalse("should have one element", lists.isEmpty());
      lists.add(-8);
      assertFalse("should have two elements", lists.isEmpty());
  }
  @Test
  public void TestisEmpty2() {
      ArrayInList lists = new ArrayInList();
      lists.add(44);
      lists.add(-8);
      assertFalse("should contain two elements", lists.isEmpty());
      lists.remove(1);
      lists.remove(0);
      assertTrue("after the removal of all the elements", lists.isEmpty());
      lists.add(44);
      assertFalse("should have just one element", lists.isEmpty());
  }
  

Running the JUnit test cases

Now that you have written your JUnit test methods, you must be prepared to run them. Now, there are basically two ways that you can implement this.

The first method is to click on the Play button in the toolbar present at the top. A dropdown menu will be seen where you can Run As ---- JUnit Test.

The second method entails you having to right click on the JUnit tests case class and then choosing Run As ---- JUnit Test.

Following this, you shall have a new window pane that will show the test results for each of your methods. If all the tests have passed, you will see a green bar, or else a red bar if any of your tests have resulted in a failure. Details about the failure pertaining to a certain test can also be found by clicking the name of the failed test. The details appear in the pane below.

A pint to note here would be that most people associate a red bar with bad. However, this is simply not true. Getting a red bar means that you have found a big that needs to be looked into and fixed. Trying to getting a green bar after fixing your code can, in fact, be very good for you.

If you still want to see more regarding unit testing, please refer some of our other articles:

Conclusion

Testing is one of the most important parts of development of any software or application. Thus, they need to be carried out correctly and with great care as well. Better testing also implies the filtration of bugs from your code. While there are certainly many methods that one can resort to, in terms of having their code tested, unit testing is the smallest and the first step to any testing that takes place.

In the above tutorial, we have seen how one can implement very simple unit test procedures for Java application development by making use of JUnit. We have also covered on how to implement the various assertions that are provided by JUnit for the implementation of testing processes on our code.



Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login