Basic annotations in junit

In this topic, you will learn about basic annotations in JUnit. Here is the list of frequently used annotations in JUnit. Remember that all below annotations are put in org.junit package from JUnit 4.0 onwards
  • @Test – marks the method as test method
  • @BeforeClass – Method marked with this annotation is called once before all tests are run in particular class.
  • @AfterClass – Method marked with this annotation is called once afterall tests are run in particular class.
  • @Before – Method marked with this annotation is called once before each test is executed.
  • @After- Method marked with this annotation is called once after each test is executed.
 
package junit_tests;

import org.junit.*;

/**
 * Created by Sagar on 28-03-2016.
 */
public class TestClass {

    @BeforeClass
    public static void initialize(){
        //This method will be called once before all tests are run in this class
        //do stuff here which you want to do only once for all test methods
        //in this class like setting up environment, allocating resources
        System.out.println("Before Class method");
    }

    @Before
    public void initMethod(){
        //This method will be called once before each test is run
        //In this method, do stuff which you want to do before each test
        System.out.println("Before each test");
    }

    @Test
    public void test1(){
        System.out.println("Running test1");
        Assert.assertTrue("Checking simple condition",1==1);

    }

    @Test
    public void test2(){
        System.out.println("Running test2");
        Assert.assertTrue("Checking other condition",1==2);
    }

    @After
    public void cleanUpMethod(){
        //This method will be called once after each test is run
        //In this method, do stuff which you want to do after each test
        System.out.println("After each test");
    }

    @AfterClass
    public static void cleanUp(){
        //This method will be called once after all tests are run in this class
        //do stuff here which you want to do only once for all test methods
        //in this class like cleaning environment, releasing resources
        System.out.println("After Class method");
    }
}

Here is the output of the above code.
 
junit_tests.MyTestRunner
Before Class method
Before each test
Running test1
After each test
Before each test
Running test2
After each test
After Class method
Total tests Ran 2
Total tests passed 1
Total tests failed 1
********Failed Tests*******
java.lang.AssertionError: Checking other condition
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.assertTrue(Assert.java:41)
at junit_tests.TestClass.test2(TestClass.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runners.Suite.runChild(Suite.java:127)
at org.junit.runners.Suite.runChild(Suite.java:26)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at org.junit.runner.JUnitCore.run(JUnitCore.java:138)
at org.junit.runner.JUnitCore.run(JUnitCore.java:128)
at org.junit.runner.JUnitCore.runClasses(JUnitCore.java:73)
at junit_tests.MyTestRunner.main(MyTestRunner.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

********Test were successful*******
false

Process finished with exit code 0      

Web development and Automation testing

solutions delivered!!