?
Solved

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

Posted on 2012-03-15
3
Medium Priority
?
463 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 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

719 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