Tuesday, 15 June 2021

Junit 5: Hello World Application using maven

Step 1: Open Eclipse, and create new maven project junit5-examples.

 

File -> New -> Other.

Under maven -> Maven Project.

 


 

Click on Next button.

 


 

Select the checkbox ‘Create s simple project (skip archetype selection)’, click on Next button.




Give Group Id as ‘com.sample.app’ and Artifact Id as junit5-examples and Version as 1 and click on Finish button.

 

Total project structure looks like below.




Step 2: Update pom.xml with maven dependencies.

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>junit5-examples</artifactId>
	<version>1</version>

	<properties>
		<junit-jupiter.version>5.6.1</junit-jupiter.version>
		<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>

		<java.version>1.8</java.version>
		<maven.compiler.target>${java.version}</maven.compiler.target>
		<maven.compiler.source>${java.version}</maven.compiler.source>

		<project.encondig>UTF-8</project.encondig>
		<project.build.sourceEncoding>${project.encondig}</project.build.sourceEncoding>
		<project.reporting.outputEncoding>${project.encondig}</project.reporting.outputEncoding>
	</properties>


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

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>${maven-surefire-plugin.version}</version>
			</plugin>
		</plugins>
	</build>
</project>

 

Step 3: Create a package com.sample.app under src/test/java folder.

 

Step 4: Define AppTest class in com.sample.app package.

 

AppTest.java

 

package com.sample.app;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class AppTest {

	@Test
	public void helloTest() {
		assertTrue(true);
	}

}

 

Step 5: Run the testcases.

 

Right click on AppTest.java class -> Run As -> Junit Test.

 

 


You will see the test results under Junit window.


 


 

 

You can download complete working application from this link.

https://github.com/harikrishna553/junit5/tree/master/junit5-examples

Previous                                                    Next                                                    Home

No comments:

Post a Comment