Show List
JUnit Coding Questions
- Write a JUnit test case for a method that returns the sum of two numbers.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class CalculatorTest {
@Test
public void testAddition() {
Calculator calculator = new Calculator();
int result = calculator.add(3, 4);
assertEquals(7, result);
}
}
- Write a JUnit test case for a method that returns the factorial of a number.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class CalculatorTest {
@Test
public void testFactorial() {
Calculator calculator = new Calculator();
int result = calculator.factorial(5);
assertEquals(120, result);
}
}
- Write a JUnit test case for a method that checks whether a number is prime or not.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public class PrimeTest {
@Test
public void testPrimeNumber() {
Prime prime = new Prime();
assertTrue(prime.isPrime(7));
}
@Test
public void testNonPrimeNumber() {
Prime prime = new Prime();
assertFalse(prime.isPrime(4));
}
}
- Write a JUnit test case for a method that checks whether a string is a palindrome or not.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public class PalindromeTest {
@Test
public void testPalindrome() {
Palindrome palindrome = new Palindrome();
assertTrue(palindrome.isPalindrome("racecar"));
}
@Test
public void testNonPalindrome() {
Palindrome palindrome = new Palindrome();
assertFalse(palindrome.isPalindrome("hello"));
}
}
- Write a JUnit test case for a method that reverses a string.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class StringReverseTest {
@Test
public void testStringReverse() {
StringReverse stringReverse = new StringReverse();
String result = stringReverse.reverse("hello");
assertEquals("olleh", result);
}
}
- Write a JUnit test case for a method that calculates the area of a rectangle.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class RectangleTest {
@Test
public void testArea() {
Rectangle rectangle = new Rectangle(4, 5);
double result = rectangle.area();
assertEquals(20, result, 0);
}
}
- Write a JUnit test case for a method that calculates the circumference of a circle.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class CircleTest {
@Test
public void testCircumference() {
Circle circle = new Circle(5);
double result = circle.circumference();
assertEquals(31.42, result, 0.01);
}
}
- Write a JUnit test case for a method that calculates the volume of a sphere.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class SphereTest {
@Test
public void testVolume() {
Sphere sphere = new Sphere(5);
double result = sphere.volume();
assertEquals(523.6, result, 0.1);
}
}
- Write a JUnit test case for a method that sorts an array of integers.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
public class SortTest {
@Test
public void testSort() {
int[] array = {3, 2, 1, 4, 5};
Sort sort = new Sort();
int[] result = sort.sortArray(array);
int[] expected = {1, 2, 3, 4, 5};
assertArrayEquals(expected, result);
}
}
- Write a JUnit test case for a method that finds the maximum element in an array.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MaxElementTest {
@Test
public void testMaxElement() {
int[] array = {3, 2, 1, 4, 5};
MaxElement maxElement = new MaxElement();
int result = maxElement.findMax(array);
assertEquals(5, result);
}
}
- Write a JUnit test case for a method that calculates the sum of a list of numbers.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ListSumTest {
@Test
public void testSum() {
List<Integer> list = new ArrayList<>();
list.add(3);
list.add(4);
list.add(5);
ListSum listSum = new ListSum();
int result = listSum.sum(list);
assertEquals(12, result);
}
}
- Write a JUnit test case for a method that checks whether a list is sorted in ascending order.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public class ListSortTest {
@Test
public void testSorted() {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
ListSort listSort = new ListSort();
assertTrue(listSort.isSorted(list));
}
@Test
public void testUnsorted() {
List<Integer> list = new ArrayList<>();
list.add(3);
list.add(2);
list.add(1);
ListSort listSort = new ListSort();
assertFalse(listSort.isSorted(list));
}
}
- Write a JUnit test case for a method that reverses a list.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ListReverseTest {
@Test
public void testReverse() {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
ListReverse listReverse = new ListReverse();
List<Integer> result = listReverse.reverse(list);
List<Integer> expected = new ArrayList<>();
expected.add(3);
expected.add(2);
expected.add(1);
assertEquals(expected, result);
}
}
- Write a JUnit test case for a method that returns the length of a string.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class StringLengthTest {
@Test
public void testLength() {
StringLength stringLength = new StringLength();
int result = stringLength.getLength("hello");
assertEquals(5, result);
}
}
- Write a JUnit test case for a method that concatenates two strings.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class StringConcatTest {
@Test
public void testConcat() {
StringConcat stringConcat = new StringConcat();
String result = stringConcat.concatStrings("hello", "world");
assertEquals("helloworld", result);
}
}
- Write a JUnit test case for a method that reverses a string.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class StringReverseTest {
@Test
public void testReverse() {
StringReverse stringReverse = new StringReverse();
String result = stringReverse.reverseString("hello");
assertEquals("olleh", result);
}
}
- Write a JUnit test case for a method that checks whether a string is a palindrome.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public class PalindromeTest {
@Test
public void testPalindrome() {
Palindrome palindrome = new Palindrome();
assertTrue(palindrome.isPalindrome("racecar"));
}
@Test
public void testNotPalindrome() {
Palindrome palindrome = new Palindrome();
assertFalse(palindrome.isPalindrome("hello"));
}
}
- Write a JUnit test case for a method that checks whether a number is even.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public class EvenNumberTest {
@Test
public void testEven() {
EvenNumber evenNumber = new EvenNumber();
assertTrue(evenNumber.isEven(2));
}
@Test
public void testOdd() {
EvenNumber evenNumber = new EvenNumber();
assertFalse(evenNumber.isEven(3));
}
}
- Write a JUnit test case for a method that checks whether a number is prime.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public class PrimeNumberTest {
@Test
public void testPrime() {
PrimeNumber primeNumber = new PrimeNumber();
assertTrue(primeNumber.isPrime(7));
}
@Test
public void testNotPrime() {
PrimeNumber primeNumber = new PrimeNumber();
assertFalse(primeNumber.isPrime(4));
}
}
- Write a JUnit test case for a method that calculates the factorial of a number.
javaCopy code
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class FactorialTest {
@Test
public void testFactorial() {
Factorial factorial = new Factorial();
int result = factorial.calculateFactorial(5);
assertEquals(120, result);
}
}
Leave a Comment