0% found this document useful (0 votes)
46 views13 pages

Junit: BY Gayathri S Ap/Cse 20.08.2020

JUnit is a unit testing framework for Java applications that provides annotations and assertions to write and run automated tests, it allows tests to be organized into test cases and test suites and features include support for test-driven development, integration with IDEs, and generation of test reports. JUnit provides a simple way to write and run repeatable tests in Java by including test annotations and assertions for validating outcomes.

Uploaded by

Gayuscribd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views13 pages

Junit: BY Gayathri S Ap/Cse 20.08.2020

JUnit is a unit testing framework for Java applications that provides annotations and assertions to write and run automated tests, it allows tests to be organized into test cases and test suites and features include support for test-driven development, integration with IDEs, and generation of test reports. JUnit provides a simple way to write and run repeatable tests in Java by including test annotations and assertions for validating outcomes.

Uploaded by

Gayuscribd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

JUNIT

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 is linked as a JAR at compile-time; the framework resides under packagesjunit.framework


for
JUnit 3.8 and earlier and under org.junit for JUnit 4 and later.

• 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 has Test runners for running tests.

• JUnit tests can be organized into test suites which contains test cases and other test suites.

• JUnit Framework can be easily integrated with either of the followings:


• Eclipse
• Ant
• Maven
TESTING VIA JUNIT

• Can test JUnit by using Test Case & Test Suit .

• 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

• Support for test assertions

• Test suite development

• Immediate test reporting


FEATURES
•JUnit is an open source framework, which is used for writing and running tests.
•Provides annotations to identify test methods.
•Provides assertions for testing expected results.
•Provides test runners for running tests.
•JUnit tests allow you to write codes faster, which increases quality.
•JUnit is elegantly simple. It is less complex and takes less time.
•JUnit tests can be organized into test suites containing test cases and even other
test suites.
•JUnit shows test progress in a bar that is green if the test is running smoothly,
and it turns red when a test fails.
JUNIT ANNOTATIONS
• @Test : The Test annotation tells JUnit that the public void method to which it is attached can be
run as a test case.

• @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

 public void before()


 O/P :
 { System.out.println("Before Test");  Before Test
 c=new Calculator(); }
 Add
 @After

 public void after() { System.out.println("After Test"); } function


 @Test
 After Test
 public void testAdd() {

 System.out.println("Add function");  Before


 assertEquals("Result",5,c.add(2,3));

 }
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.

public class Assert extends


java.lang.Object

• This class provides a set of assertion methods useful for writing


tests.

• Only failed assertions are recorded.


ASSERTION METHODS
• void assertEquals(boolean expected, boolean actual) Check that two primitives/Objects are equal

• void assertTrue(boolean expected, boolean actual) Check that a condition is true

• void assertFalse(boolean condition) Check that a condition is false

• void assertNotNull(Object object) Check that an object isn't null.

• void assertNull(Object object) Check that an object is null


ASSERTION METHODS (CONTD…)

• void assertSame(boolean condition) The assertSame() methods tests if two object references
point to the same object

• void assertNotSame(boolean condition) The assertNotSame() methods tests if two object


references not point to the same object

• void assertArrayEquals(expectedArray, resultArray); The assertArrayEquals() method will


test
whether two arrays are equal to each other.

• fail(String message) Fails a test with the given message.

• 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

You might also like