Solved

Java - how do i write a test case for java application, Junit ???

Posted on 2012-03-15
3
441 Views
Last Modified: 2012-06-21
OK. As Yan's help, I almost finish 50% of my work, now I need to the best way how to write a test case in java. My lecture mentioned about Junit, but this is only for J2EE web-based application only. I think.
for me, just a simple schooladdressbook application, please give me some idea how to do it. gave me a good example, would be very very appreicated. Yan, I hope you can help me for this as well as you help me from the very beginnning.
0
Comment
Question by:unistudent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 37728059
JUnit, is NOT just for J2EE

I would suggest you look at using it, it is quite commonly used. If you are using an IDE, it can probably create the template test class for you. You would need to provide a bit more detail about your code, the sort of tests that you want to do, your environment/IDE for me to help much further.
0
 

Author Comment

by:unistudent
ID: 37728111
Thanks for the tips.
For example, I have a Java application in which have a method to remove the duplicate record from the ArrayList, how do i write a Junit for this. There are two main methods defined in the Java Main program like below. My problem is I dont know how to create a Junit to test for sort and remove duplicate functions. Please advise. example codes are very much appreicated.

public class SchoolAddressBook {

  public SchoolAddressBook(ArrayList<Contact> ar){
        this.ar = ar;
    }


  void sortSchoolAddressBook() {
        Collections.sort(ar);
    }

  void removeDuplicates() {
             ArrayList<Contact> ar1 = new ArrayList<Contact>();
              for(Contact c : ar){
                  if(!ar1.contains(c))ar1.add(c);
              }
              ar = ar1;
          }


   public static void main (String args[]) {
0
 
LVL 35

Expert Comment

by:mccarl
ID: 37731825
You would create a separate class to the above that looks something like this...


public class SchoolAddressBookTest {
  @Test
  public void testSorting {
    List<Contact> testList = new ArrayList<Contact>();
    testList.add(new Contact(.....));   // Fill in some test data here
    testList.add(new Contact(.....));   // Fill in some test data here
    testList.add(new Contact(.....));   // Fill in some test data here

    SchoolAddressBook testAddrBook = new SchoolAddressBook(testList);

    testAddrBook.sortSchoolAddressBook();

    assertEquals("Addison", testAddrBook.get(0).getName);  // Change this to whatever method you have of retrieving the first contact's name
    assertEquals("Brown", testAddrBook.get(1).getName);  // Change this to whatever method you have of retrieving the second contact's name
    assertEquals("Cochrane", testAddrBook.get(2).getName);  // Change this to whatever method you have of retrieving the third contact's name
  }

  @Test
  public void testDuplicateRemoval {
    // Do similar to the above but load the testList with duplicates and 'assert' that they have been removed after calling the removeDuplicates() method
  }
}

Open in new window


And include junit jar's and add appropriate import statements. Then depending on your environment/IDE, you should be able to execute this class as a junit test.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question