Solved

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

Posted on 2012-03-15
3
439 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

856 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