Only institute to provide recording daily after class to students. Only institute you can practice whole day under faculty guidance. Only institute where test and mockup tests are taken regularly. Only institute provides all mobile nos of old students on website.

JUnit Testing

JUnit 4
  • It is an open-source testing framework for java programmers.
  • It is one of the unit testing frameworks. Current version is junit4.

  • What is testing?
  • Process of checking the functionality of Application whether it is working as per requirements.

  • What is the Unit Testing?
  • Testing of single entity (Class or Method) is called as unit testing.
  • To perform unit testing we need to create test cases.
  • The unit test case code ensures that the program logic works as expected.

  • Types of Unit Testing
  • Manual: Executing test case without any tool. Time consuming, less reliable.
  • Automated: Executing test case with tool such as JUnit4. it is Fast and more reliable.

Consider the class A having method add. Now we have to perform testing on add method, whether it is working correct or not.

package com;
			public class A {
	
			int add(int a,int b){
		
			int c = a+b;
			return c;
		
			}
			}

Now we will make separate Test class for A as ATest and we will write test case for add method

package com;

public class ATest {

		static void addTest(){
			
			A a = new A();
			int result=a.add(12, 10);
			
			//expeted -22
			if(result==22){
				System.out.println("Test case: Pass");
			}else{
				System.out.println("Test case: Fail");
			}	
		}
		
		public static void main(String[] args) {
			
			ATest.addTest(); 
		}
}

Requirements – JUnit 4.jar ,Extend TestCase class
Follow Following Steps –

  • Add Junit jar into bulid path as –
    Right click on Project->Properties->Libraries->Add Library->JUnit->Junit-4->ok.
  • Write Following Test class –

  • package com.automated;
    
    				import junit.framework.TestCase;
    
    
    			public class ATest extends TestCase{
    
    			A a;
    			@Override
    				protected void setUp() throws Exception {
    				System.out.println("in a setUp...");
    					a = new A();
    				}
    				public void testAdd(){
    		
    				assertEquals(25,a.add(5, 20));	
    				}
    				@Override
    			protected void tearDown() throws Exception {
    			System.out.println("in a tearDown...");
    			a=null;
    			}
    	
    		}}


    package com.automated;
    
    import junit.framework.TestCase;
    
    
    public class ATest extends TestCase{
    
    	A a;
    	@Override
    	protected void setUp() throws Exception {
    		System.out.println("in a setUp...");
    		a = new A();
    	}
    	public void testAdd(){
    		
    		assertEquals(25,a.add(5, 20)); //positive
    		assertNotSame(24,a.add(2, 20));//negative
    		
    		
    	}
    	@Override
    	protected void tearDown() throws Exception {
    		System.out.println("in a tearDown...");
    	    a=null;
    	}
    	
    }