JUnit Framework

Posted By : Hany Yadav | 31-May-2020

Java

Loading...

Junit framework

JUnit is a unit testing framework for JAVA programming language. JUnit used to write and execute scripts in selenium. It completely depends on annotations and with the help of JUnit classes and methods are to be controlled. It is the platform that generates the HTML reports. JUnit framework can be easily integrated with following:-

  • Eclipse
  • Ant
  • Maven

Annotations:-

  • @BeforeClass
  • @Before
  • @Test
  • @After
  • @AfterClass

  • @BeforeClass:-

It is an annotation that is applicable on static function and runs once before all the test cases mentioned in the script start their execution.

  • @Before:-

It is a method that is a precondition for all test cases mentioned in scripts and is invoked/run for each test case.

  • @Test:-

It is used to control functions and is applicable to non-static and non-parameterized functions. @Test annotation is a randomized annotation so, it is not possible to set priority in @test annotation just because of randomized. Any method can be executed initially. It converts the functions into test cases, executes them, and generates the report with the function name.

  • @AfterClass:-

It is annotation that is applicable to static function and runs once all the test cases mentioned in the script complete their execution.

  • @After:-

It is a method which is a postcondition for all test cases mentioned in scripts and for each test cases it will be invoked/run.

Example:-

import org.junit.*;

public class Junit {

// Run once

@BeforeClass

public static void BeforeClass() {

System.out.println("@BeforeClass");

}

// Run once

@AfterClass

public static void AfterClass() {

System.out.println("@AfterClass");

}

// Run before TestMethod

@Before

public void runBeforeTestMethod() {

System.out.println("@Before");

}

// Run after TestMethod

@After

public void After() {

System.out.println("@After");

}

@Test

public void test_method_1() {

System.out.println("@Test1");

}

@Test

public void test_method_2() {

System.out.println("@Test2");

}

}

Outout:-

@BeforeClass

@Before

@Test1

@After

@Before

@Test2

@After

@AfterClass

We are an ERP development company that enables enterprises to streamline operations and drive returns, using next-gen technologies, with our development services. Our services include developing applications for all your ERP needs from CRM, WFM, and HRM to eCommerce, accounting, and wealth management software. We use an extensive tech stack including Javascript and JUnit to develop end to end customized software for your enterprise. Get in touch with our experts to know more about how you can implement these technologies into your business.