Followers

Java Junit

JUnit 5   Unit testing involves the testing of each unit or an individual component of the software application. It is the first level of fu...

JUnit 5

 Unit testing involves the testing of each unit or an individual component of the software application. It is the first level of functional testing. The aim behind unit testing is to validate unit components with their performance.

JUnit5 main aim is to adapt the Java 8 style of coding. Minimum Jdk8 is required to execute JUnit 5 tests.

Architecture

JUnit 5 contains a different set of modules from its subprojects. 

  1. JUnit Platform
  2. JUnit Jupiter
  3. JUnit Vintage

JUnit Platform

The platform is responsible for launching testing frameworks on the JVM. It defines a stable and powerful interface between JUnit and its clients, such as build tools.

The platform easily integrates clients with JUnit to discover and execute tests.

It also defines the TestEngine API for developing a testing framework that runs on the JUnit platform. By implementing a custom TestEngine, we can plug 3rd party testing libraries directly into JUnit.

JUnit Jupiter

This module includes new programming and extension models for writing tests in JUnit 5. New annotations in comparison to JUnit 4 are:

@TestFactory – denotes a method that's a test factory for dynamic tests

@DisplayName – defines a custom display name for a test class or a test method

@Nested – denotes that the annotated class is a nested, non-static test class

@Tag – declares tags for filtering tests

@ExtendWith – registers custom extensions

@BeforeEach – denotes that the annotated method will be executed before each test method (previously @Before)

@AfterEach – denotes that the annotated method will be executed after each test method (previously @After)

@BeforeAll – denotes that the annotated method will be executed before all test methods in the current class (previously @BeforeClass)

@AfterAll – denotes that the annotated method will be executed after all test methods in the current class (previously @AfterClass)

@Disable – disables a test class or method (previously @Ignore)

JUnit Vintage

JUnit Vintage supports running tests based on JUnit 3 and JUnit 4 on the JUnit 5 platform.

Basic Annotation

Example 1

        @Test
	void testSingleSuccessTest() {
	System.out.println("Success Test");
	}
	@BeforeAll
	static void setup() {
	   System.out.println("@BeforeAll - executes once before all test methods in this class");
	}

	@BeforeEach
	void init() {
		System.out.println("@BeforeEach - executes before each test method in this class");
	}	

Example 2

        @DisplayName("Single test successful")
	@Test
	void testSingleSuccessTest() {
	System.out.println("Success Test");
	}
	@Disabled("Not implemented yet")
	@Test
	void avoidTesting() {
		
	}

Example 3

        @Test
	void SingleAssertion()
	{
		assertEquals(2,3);
	}
	@Test
	 void groupAssertions() {
	     int[] numbers = {0, 1, 2, 3, 4};
	     assertAll("numbers",
	         () -> assertEquals(numbers[0], 1),
	         () -> assertEquals(numbers[3], 3),
	         () -> assertEquals(numbers[4], 1)
	     );
	 }

Example 4

        @Test
	void shouldThrowException() {
	    Throwable exception = assertThrows(UnsupportedOperationException.class, () -> {
	      throw new UnsupportedOperationException("Not supported");
	    });
	    assertEquals(exception.getMessage(), "Not supported");
	}

	@Test
	void assertThrowsException() {
	    String str = null;
	    assertThrows(IllegalArgumentException.class, () -> {
	      Integer.valueOf(str);
	    });
	}

Maven Dependencies

<dependencies>
 
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
 
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
item
DevOpsWorld: Java Junit
Java Junit
DevOpsWorld
https://www.devopsworld.co.in/2022/01/java-junit.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2022/01/java-junit.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content