Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-03-15
3
Medium Priority
?
468 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
  • 2
3 Comments
 
LVL 36

Accepted Solution

by:
mccarl earned 1500 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 36

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

Industry Leaders: 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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

971 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