Created
March 19, 2023 15:54
-
-
Save nadvolod/15e9958e1dee90283221fcaa6fcfce9f to your computer and use it in GitHub Desktop.
JUnit 4 Exercise Solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Solution to 1 | |
import org.junit.Test; | |
import static org.junit.Assert.assertEquals; | |
public class AdditionTest { | |
@Test | |
public void testSum() { | |
int sum = 3 + 4; | |
int expectedSum = 7; | |
assertEquals(expectedSum, sum); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Before; | |
import org.junit.After; | |
import org.junit.Test; | |
import static org.junit.Assert.assertEquals; | |
public class CalculatorTest { | |
private Calculator calculator; | |
@Before | |
public void setUp() { | |
calculator = new Calculator(); | |
} | |
@Test | |
public void testAddition() { | |
int result = calculator.add(3, 4); | |
assertEquals(7, result); | |
} | |
@Test | |
public void testSubtraction() { | |
int result = calculator.subtract(5, 2); | |
assertEquals(3, result); | |
} | |
@After | |
public void tearDown() { | |
calculator = null; | |
} | |
// Calculator class for illustration purposes | |
class Calculator { | |
int add(int a, int b) { | |
return a + b; | |
} | |
int subtract(int a, int b) { | |
return a - b; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import static org.junit.Assert.assertTrue; | |
import static org.junit.Assert.assertFalse; | |
public class StringTest { | |
@Test | |
public void testContainsSubstring() { | |
String mainString = "Hello, World!"; | |
String substring1 = "World"; | |
String substring2 = "Java"; | |
assertTrue(mainString.contains(substring1)); | |
assertFalse(mainString.contains(substring2)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import static org.junit.Assert.assertArrayEquals; | |
public class ArrayEqualityTest { | |
@Test | |
public void testArrayEquality() { | |
int[] array1 = {1, 2, 3, 4, 5}; | |
int[] array2 = {1, 2, 3, 4, 5}; | |
assertArrayEquals(array1, array2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import static org.junit.Assert.assertNull; | |
import static org.junit.Assert.assertNotNull; | |
public class ObjectNullityTest { | |
@Test | |
public void testObjectIsNull() { | |
String nullString = null; | |
assertNull(nullString); | |
} | |
@Test | |
public void testObjectIsNotNull() { | |
String nonNullString = "JUnit 4"; | |
assertNotNull(nonNullString); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import static org.junit.Assert.assertEquals; | |
public class FloatingPointTest { | |
@Test | |
public void testFloatingPointEquality() { | |
double number1 = 1.3333333; | |
double number2 = 4.0 / 3.0; | |
double delta = 0.000001; | |
assertEquals(number1, number2, delta); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import static org.junit.Assert.assertEquals; | |
public class ExceptionHandlingTest { | |
@Test(expected = ArithmeticException.class) | |
public void testDivisionByZero() { | |
int result = 5 / 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import java.util.List; | |
import java.util.Arrays; | |
import static org.hamcrest.CoreMatchers.hasItem; | |
import static org.junit.Assert.assertThat; | |
public class CollectionTest { | |
@Test | |
public void testListContainsElement() { | |
List<String> colors = Arrays.asList("red", "green", "blue"); | |
assertThat(colors, hasItem("green")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment