Create a Java class & Test Class

Create a Java class & Test Class


Posted in : Java Posted on : November 23, 2010 at 6:55 PM Comments : [ 0 ]

This section contains the detail about how to Create a Java class & Test Class

Create a Java class & Test Class

Create a package "devmanuals.junit " first & then the below class inside it :

package devmanuals.junit;

public class MyClass {
public int multiply(int x, int y) {
return x * y;
}
}

Select your new class, right mouse click and select New ->others-->(Inside JUnit folder)JUnit Test case, following window will appear :

Click next & select the methods ,you want to test, as follows :

  • Click 'Finish'. A Class 'MyClassTest.java' will open automatically in front.

  • This Class "MyClassTest.java" is automatically created by eclipse to test your code.

  • Given below the automatically generated code :

  • package devmanuals.junit;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    
    public class MyClassTest {
    
    @Test
    public void testMultiply() {
    fail("Not yet implemented");
    }
    
    }
  • You need to edit this code to make it testable as given in the next slide :

  • package devmanuals.junit;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    
    public class MyClassTest {
    
    @Test
    public void testMultiply() {
    MyClass tester = new MyClass();
    assertEquals(50, tester.multiply(10, 5));
    }
    }
    
  • The "assertEquals" method compares expected value with actual value..

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics