Below are examples of JUnit Jupiter @ annotations.
Example 1 (@Test) :-
>> in file path: JUnittest-demo
src/main/java
//MyClass.java
package com.unittest;
public class myClass {
public int sum(int a, int b) {
return a+b;
}
}
>> in file path: JUnittest-demo
src/test/java
//myTestForClass.java
package com.unittest;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class myunittest {
@Test
void test() {
MyClass objmyclass= new MyClass();
int actualresult;
int expected ;
for(int i=1;i<=10;i++)
{
actualresult= objmyclass.sum(i, i+1);
if(i%2==0)
expected= i+i+1;
else
expected=10;
assertEquals(expected, actualresult);
}
//fail("Not yet implemented");
}
}
Example 2 (@Test with all Passed)
package com.unittest;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class myTestForClass {
@Test
void test_sum() {
myClass objmyclass = new myClass();
int actualresult;
int expected ;
for(int i=1; i<=10; i++)
{
actualresult = objmyclass.sum(i, i+1);
expected = i+i+1;
assertEquals(expected, actualresult);
}
}
@Test
void test_subt() {
myClass objmyclass = new myClass();
int actualresult;
int expected ;
for(int i=1; i<=10; i++)
{
actualresult = objmyclass.subt(i, i+1);
expected = i-(i+1);
assertEquals(expected, actualresult);
}
}
@Test
void test_divide() {
myClass objmyclass = new myClass();
int actualresult;
int expected ;
for(int i=1; i<=10; i++)
{
actualresult = objmyclass.divide(i, i+1);
expected = i/(i+1);
assertEquals(expected, actualresult);
}
}
}
Example 3 (@BeforeAll):-
// setting up value of variable i at class level which can used by other test methods
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
public class myunittest {
static int i;
@Test
void test() {
System.out.println("test method is called " + i);
MyClass objmyclass= new MyClass();
int actualresult;
int expected ;
for(int i=1;i<=10;i++)
{
actualresult= objmyclass.sum(i, i+1);
expected= i+i+1;
assertEquals(expected, actualresult);
}
//fail("Not yet implemented");
}
@Test
void newTest() {
System.out.println("failed method is called" + i);
String mystr="Testing";
if(mystr.contains("t"))
{
fail("This test case is failed");
}
}
@BeforeAll
static void setup() {
i=5;
System.out.println("this is my test class ");
}
}
Example 4 (@BeforeEach):-
package com.unittest;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class myTestForClass {
static int i;
@Test
void a() {
i=30;
System.out.println("'a' test method is called " + i);
myClass objmyclass = new myClass();
int actualresult;
int expected;
for(int i=1;i<=10;i++)
{
actualresult= objmyclass.sum(i, i+1);
expected= i+i+1;
assertEquals(expected, actualresult);
}
}
@Test
void b() {
System.out.println("'b' failed method is called" + i);
String mystr="Testing";
if(mystr.contains("t"))
{
fail("This test case is failed");
}
}
@Test
void c() {
System.out.println("'c' This is mytest example");
}
//@BeforeAll is an annotation which get executed in the first instance.
@ BeforeAll
static void setup() {
i=5;
System.out.println("this is will appear Before ALL test classes ");
}
//@BeforeEach is an annotation which get executed on each TestCase(@Test)
@ BeforeEach
void setupEachTest()
{
System.out.println("this will appear Before EACH method is called");
i=20;
}
}
Example 5 (@DisplayName and @Disabled):-
@DisplayName ("Step 1")
@Test
void a() {
i=30;
System.out.println("'a' test method is called " + i);
myClass objmyclass = new myClass();
int actualresult;
int expected;
for(int i=1;i<=10;i++)
{
actualresult= objmyclass.sum(i, i+1);
expected= i+i+1;
assertEquals(expected, actualresult);
}
}
@Disabled ("This is disabled because keep on failing")
@Test
void b() {
System.out.println("'b' failed method is called" + i);
String mystr="Testing";
if(mystr.contains("t"))
{
fail("This test case is failed");
}
}
@DisplayName ("Step 3")
@Test
void c() {
System.out.println("'c' This is mytest example");
}
Example 6 (groupAssertions):-
(will fail)
@Test
void groupAssertions() {
int[] numbers = {0, 1, 2, 3, 4};
assertAll("numbers",
() -> assertEquals(numbers[0], 1),
() -> assertEquals(numbers[3], 3),
() -> assertEquals(numbers[4], 1)
);
}(will pass)@Test void groupAssertions() { int[] numbers = {0, 1, 2, 3, 4}; assertAll("numbers", () -> assertEquals(numbers[0], 0), () -> assertEquals(numbers[3], 3), () -> assertEquals(numbers[4], 4) ); }
Example 7 (assertThrows):- @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); });
}
No comments:
Post a Comment