Saturday 19 June 2021

Junit5: @Test: signal that the annotated method is a test method.

@Test annotation is used to signal that the annotated method is a test method.

 

Following are the constraints on @Test methods.

a.   Methods must not be private or static

b.   Methods must not return a value.

 

Example

@Test
void testSuccessScenario() {
	assertEquals(5, 2 + 3, "Sum of 2 and 3 is not equal to 5");
}

 

Find the below working application.

 

TestAnnotationDemo.java

package com.sample.app;

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

import org.junit.jupiter.api.Test;

public class TestAnnotationDemo {

	@Test
	void testSuccessScenario() {
		assertEquals(5, 2 + 3, "Sum of 2 and 3 is not equal to 5");
	}

	@Test
	void testFailureScenario() {
		assertTrue(false);
	}
}

 

When you run above class as Junit Test, you will see the test case ‘testSuccessScenario’ is passed and ‘testFailureScenario’ is failed.

 

 



Can I create meta annotations using @Test annotation?

Yes, you can create a custom composed annotation using @Test annotation that inherits the semantics of @Test annotation. You can see more about this in meta annotation section.

 

Can a test method declare parameters?

Yes a test method can optionally declare parameters, those are resolved by org.junit.jupiter.api.extension.ParameterResolver. You can see more about this in ParameterResolver section.

 

Can I customize the test execution order?

Yes, you can customize the test execution order using @TestMethodOrder annotation.

 

 

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