Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

EX NO : 2.

2(1)
JUNIT
DATE :

AIM :
To write a java program to perform unit testing on the given program.

ALGORITHM:
BEGIN

STEP 1: Add the Junit and hamcrest-core JAR files to the project

STEP 2: Perform the unit testing using the assert methods available in the Junit package.

STEP 3: Obtain the Test results.

END

SOURCE CODE :

public class FibonacciNumber{


public int nthFibonacci(int num) {
if (num == 1) {
return 0;
}
if (num == 2) {
return 1;
}
return nthFibonacci(num - 1) + nthFibonacci(num -2);
}
}
Test File :
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class FibonacciTest {

Roll Number: 20F146 18FE04-Advanced Java Programming


FibonacciNumber obj = new FibonacciNumber();
@Test
public void test(){
assertEquals(obj.nthFibonacci(6),5);
}
@Test
public void test1(){
assertEquals(obj.nthFibonacci(8),13);
}
}
OUTPUT :

RESULT :

Roll Number: 20F146 18FE04-Advanced Java Programming


Thus the program was executed successfully and the output is obtained.

EX NO : 2.2(2)
JUNIT
DATE :

AIM :
To write a java program to perform unit testing on the given program.
ALGORITHM:
BEGIN
STEP 1: Add the Junit and hamcrest-core JAR files to the project
STEP 2: Perform the unit testing using the assert methods available in the Junit package.
STEP 3: Obtain the Test results.
END

SOURCE CODE :
public class Palin {
public String Palindrome(String s){
String rev = "";
for(int i=s.length()-1;i>=0;i--){
rev+=Character.toString(s.charAt(i));
}
return rev;
}
}
Test File :
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class PaliTest {
Palin obj = new Palin();
@Test
public void test(){

Roll Number: 20F146 18FE04-Advanced Java Programming


assertEquals(obj.Palindrome("madam"),"madam");
}
@Test
public void test1(){
assertEquals(obj.Palindrome("malayalam"),"malayalam");
}
}
OUTPUT :

RESULT :

Roll Number: 20F146 18FE04-Advanced Java Programming


Thus the program was executed successfully and the output is obtained

EX NO : 2.2(3)
JUNIT
DATE :

AIM :
To write a java program to perform unit testing on the given program.
ALGORITHM:
BEGIN

STEP 1: Add the Junit and hamcrest-core JAR files to the project
STEP 2: Perform the unit testing using the assert methods available in the Junit package.
STEP 3: Obtain the Test results.
END

SOURCE CODE :
import java.util.Arrays;
public class removeDuplicate {
public String[] removeDuplicates(String[] input){
input = Arrays.stream(input).distinct().toArray(String[]::new);
return input;
}
}
Test File :
import static org.junit.Assert.assertArrayEquals;
import org.junit.Test;
public class RemoveTest {
removeDuplicate obj = new removeDuplicate();
@Test
public void test(){
String[] input = {"Hi","Hello","Hey","Hi","Yayy"};

Roll Number: 20F146 18FE04-Advanced Java Programming


String[] output = {"Hi","Hello","Hey","Yayy"};
assertArrayEquals(obj.removeDuplicates(input),output);
}
}

OUTPUT :

Roll Number: 20F146 18FE04-Advanced Java Programming


RESULT :
Thus the program was executed successfully and the output is obtained.

EX NO : 2.2(4)
JUNIT
DATE :

AIM :
To write a java program to perform unit testing on the given program.
ALGORITHM:
BEGIN

STEP 1: Add the Junit and hamcrest-core JAR files to the project
STEP 2: Perform the unit testing using the assert methods available in the Junit package.
STEP 3: Obtain the Test results.
END

SOURCE CODE :
public class Binary {
public byte add(byte b1, byte b2) {
return (byte) (b1 + b2);
}
public short add(short b1, short b2) {
return (short) (b1 + b2);
}
public int[] printBinary(int decimal) {
int binary[] = new int[4];
int index = 0;
while(decimal > 0){
binary[index++] = decimal%2;
decimal = decimal/2;

Roll Number: 20F146 18FE04-Advanced Java Programming


}
int[] res = new int[4];
int j=0;
for(int i = index-1;i >= 0;i--){
res[j] = binary[i];
j++;
}
return (res);
}
}
Test File :
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import org.junit.Test;
public class BInaryTest {
Binary obj = new Binary();
@Test
public void testAddByte(){
byte n1 = 10;
byte n2 = 12;
assertEquals(obj.add(n1,n2),22);
}
@Test
public void testAddShort(){
short n1 = 18;
short n2 = 14;
assertEquals(obj.add(n1,n2),32);
}
@Test
public void testprintBinary(){
int[] output = {1,1,0,0};
assertArrayEquals(obj.printBinary(12),output);

Roll Number: 20F146 18FE04-Advanced Java Programming


}
}

OUTPUT :

Roll Number: 20F146 18FE04-Advanced Java Programming


RESULT :
Thus the program was executed successfully and the output is obtained.

Roll Number: 20F146 18FE04-Advanced Java Programming

You might also like