Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Main.java
- public class Main {
- public static void main(String[] args) {
- assertAddMethod();
- MaxFinderTest.testMaxFinder();
- }
- static void assertAddMethod() {
- int result = add(1, 2);
- assert result == 3 : "Given 1 and 2, When add method is called, Then it should return 3";
- System.out.println("Add method test passed");
- }
- static int add(int first, int second) {
- return first + second;
- }
- }
- //MaxFinder.java
- public class MaxFinder {
- public static Integer max(Integer[] digits) {
- if (digits == null || digits.length == 0) {
- return null;
- }
- Integer max = digits[0];
- for (int i = 1; i < digits.length; i++) {
- if (digits[i] != null && (max == null || digits[i] > max)) {
- max = digits[i];
- }
- }
- return max;
- }
- }
- //MaxFinderTest.java
- public class MaxFinderTest {
- public static void testMaxFinder() {
- testNullInput();
- testEmptyArray();
- testSingleElementArray();
- testMultipleElementsArray();
- System.out.println("All tests passed successfully.");
- }
- private static void testNullInput() {
- Integer[] digits = null;
- assert MaxFinder.max(digits) == null : "Given null input, When max method is called, Then it should return null";
- System.out.println("Test for null input passed");
- }
- private static void testEmptyArray() {
- Integer[] digits = {};
- assert MaxFinder.max(digits) == null : "Given empty array, When max method is called, Then it should return null";
- System.out.println("Test for empty array passed");
- }
- private static void testSingleElementArray() {
- Integer[] digits = {5};
- assert MaxFinder.max(digits) == 5 : "Given single element array, When max method is called, Then it should return the element itself";
- System.out.println("Test for single element array passed");
- }
- private static void testMultipleElementsArray() {
- Integer[] digits = {5, 3, 9, 1, 7};
- assert MaxFinder.max(digits) == 9 : "Given multiple elements array, When max method is called, Then it should return the maximum element";
- System.out.println("Test for multiple elements array passed");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement