Junit Test case sample

Step 1: Java code

[java]
package com.math;

public class AgnClass {
public int Sum(int x, int y) {
return x + y;
}
}
[/java]

Step 2: Junit Test case for the above Java code

[java]
package com.math;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class AgnClassTest {

@Test
public void testSum() {
AgnClass nav = new AgnClass();
assertEquals("Result", 15, nav.sum(10, 5));
}
}
[/java]

Step 3: Right click on the AgnClassTest program and run as junit, then by means of green color indication we can understand the success of the test case and program.

Leave a Reply