Solved

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

Posted on 2012-03-15
3
436 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now