User:Virendralimbad/sandbox

JUnit Introduction
What is JUnit?

JUnit is a unit testing framework for the Java Programming Language. It is important in the test driven development, and is one of a family of unit testing frameworks collectively known as xUnit. JUnit promotes the idea of "first testing then coding", which emphasis on setting up the test data for a piece of code which can be tested first and then can be implemented. This approach is like "test a little, code a little, test a little, code a little..." which increases programmer productivity and stability of program code that reduces programmer stress and the time spent on debugging. Features

- JUnit is an open source framework which is used for writing & running tests. Provides Annotation to identify the test methods. - Provides Assertions for testing expected results. - Provides Test runners for running tests. - JUnit tests allow you to write code faster which increasing quality - JUnit is elegantly simple. It is less complex & takes less time. - JUnit tests can be run automatically and they check their own results and provide immediate feedback. There's no need to manually comb through a report of test results. - 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 test is going fine and it turns red when a test fails.

Jar file can be downloaded from here.

https://github.com/downloads/junit-team/junit/junit-4.11.jar

- Open Eclipse ->right click on project and click on property > Build Path > Configure build path and add the junit-4.11.jar in the libraries using Add external Jar button.

- Select the Junit jar file that is saved.

import org.junit.Test; import static org.junit.Assert.assertEquals;

public class TestJunit { String message = "Hello World"; @Test public void testPrintMessage { assertEquals(message,"Hello World"); } } Right click on program run as JUnit

What is Junit Test Framework?

JUnit is a Regression Testing Framework used by developers to implement unit testing in Java and accelerate programming speed and increase the quality of code. JUnit Framework can be easily integrated with either of the followings: Features JUnit test framework provides following important features -Fixtures -Test Suites -Test Runners -Junit classes

Fixtures Fixtures is a fixed state of a set of objects used as a baseline for running tests. The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable. It includes - setUp method which runs before every test invocation. - tearDown method which runs after every test method.

public class JavaTest extends TestCase { protected int value1, value2; // assigning the values protected void setUp{ value1=3; value2=3; }

// test method to add two values public void testAdd{ double result= value1 + value2; assertTrue(result == 6); } }

Test Suite

Test suite means bundle a few unit test cases and run it together. In JUnit, both @RunWith and @Suite annotation are used to run the suite test. //JUnit Suite Test @RunWith(Suite.class) @Suite.SuiteClasses({   TestJunit1.class ,TestJunit2.class }) public class JunitTestSuite { }

Test runner Test runner is used for executing the test cases. Here is an example which assumes TestJunit1/JavaTest test class already exists.

public class TestRunner { public static void main(String[] args) { Result result = JUnitCore.runClasses(TestJunit.class); for (Failure failure : result.getFailures) { System.out.println(failure.toString); }     System.out.println(result.wasSuccessful); } }

JUnit classes JUnit classes are important classes which is used in writing and testing JUnits. Some of the important classes are - Asset which contain a set of assert methods. - TestCase which contains a test case defines the fixture to run multiple tests. - TestResult which contain methods to collect the results of executing a test case.

Annotation used in JUnit Annotations are like meta-tags that you can add to you code and apply them to methods or in class. These annotation in JUnit gives us information about test methods, which methods are going to run before & after test methods, which methods run before & after all the methods, which methods or class will be ignore during execution.

- List of 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.

- @AfterClass This will perform the method after all tests have finished. This can be used to perform clean-up activities.

- @Ignore The Ignore annotation is used to ignore the test and that test will not be executed.

- First of all beforeClass method execute only once - Lastly, the afterClass method executes only once. - before method executes for each test case but before executing the test case. - after method executes for each test case but after the execution of test case - In between before and after each test case executes.

- @Ignore Test

For Ignore test method

@Ignore @Test public void testPrintMessage { System.out.println("Inside testPrintMessage"); message = "Robert"; assertEquals(message,messageUtil.printMessage); } or for ignore any test class

@Ignore public class TestJunit { @Test public void testPrintMessage { }  @Test public void testSalutationMessage { } }

Time Test - Junit provides a handy option of Timeout. If a test case takes more time than specified number of milliseconds then Junit will automatically mark it as failed. The timeout parameter is used along with @Test annotation. Now let's see @Test(timeout) in action.

@Test(timeout=1000) public void testPrintMessage { System.out.println(“Message test”); while(true); }

Exception Test Junit provides a option of tracing the Exception handling of code. You can test the code whether code throws desired exception or not. The expected parameter is used along with @Test annotation. Now let's see @Test(expected) in action. - It is used with @test annotation like this

@Test(expected = ArithmeticException.class) public void testPrintMessage { System.out.println("Inside testPrintMessage"); int a =0; int b = 1/a; }