Junit: BY Gayathri S Ap/Cse 20.08.2020
Junit: BY Gayathri S Ap/Cse 20.08.2020
BY
Gayathri S
AP/CSE
20.08.2020
JUNIT
• It is part of unit testing frameworks family collectively known as xUnit. Other examples are
CPPUnit, PHPUnit, JSUnit etc.
• JUnit has now become the standard tool for Test-Driven Development in Java.
• JUnit test generators now part of many Java IDEs (Eclipse, BlueJ, Jbuilder, DrJava etc).
JUNIT (CONTD…)
• JUnit provides Annotation to identify the test methods and utility methods to assert expected
results.
• JUnit tests can be organized into test suites which contains test cases and other test suites.
• Test Case: A Test Case is a java class which is used to test a single class.
• Test Suite :A Test Suit is a java class which contains many test cases or java classes to test
together in a single run.
BENEFITS
• Simple framework for writing automated, self-verifying tests in
Java
• @Before : Several tests need similar objects created before they can run. Annotating a public void
method with @Before causes that method to be run before each Test method.
• @After : If you allocate external resources in a Before method you need to release them after the
test runs. Annotating a public void method with @After causes that method to be run after the Test
method.
• @BeforeClass : Annotating a public static void method with @BeforeClass causes it to be run once
before any of the test methods in the class. This can be used to perform computationally expensive
setup (like logging into a database).
• @AfterClass : Will perform the method after all tests have finished. This can be used to perform
clean- up activities for example be used to disconnect to a database
• @Ignore : Sometimes you want to temporarily disable a test or a group of tests. Methods annotated
with Test that are also annotated with @Ignore will not be executed as tests. Also, you can annotate
a class containing test methods with @Ignore and none of the containing tests will be executed.
Program:1
public class CalculatorTest { Calculator c=null;
@Before
}
Test
@Test
Sub
public void testSub()
function
{ System.out.println("Sub function");
assertEquals("Result",20,c.sub(100,80)) ;
After Test
}
}
Program:
public class CalculatorTest
{ static Calculator c=null;
@BeforeClass O/P :
Before Test
public static void before()
{ System.out.println("Before Test");
c=new Calculator(); Add
}
@AfterClass
function
public static void after() Sub
{
System.out.println("After Test"); }
function
@Test After Test
public void testAdd()
{ System.out.println("Add function"); assertEquals("Result",5,c.add(2,3));
}
@Test
public void testSub()
{ System.out.println("Sub function"); assertEquals("Result",20,c.sub(100,80));
} }
ASSERTION
• All the assertion are in the Assert class.
• void assertSame(boolean condition) The assertSame() methods tests if two object references
point to the same object
• assertArrayEquals(String message,expected array,actual array) Asserts that two byte arrays are
equal. If they are not, an AssertionError is thrown with the given message.
DEMO