Exercise 11

info

This is an in-class exercise. An exercise page like this one will contain a brief description but is intended to be supplemented by discussion during our meeting time. Complete the exercise to the best of your ability in the time given. Feel free to talk with other students as you work, and do not be afraid to ask questions. Aim to complete as much as possible during our meeting and continue to work at home to finish, but you need not hand it in.

Learning Objectives#

Objectives

This exercise should help you practice with:

  • Unit testing randomness
  • Unit testing methods/classes that do i/o
  • REST API testing
  • rest-assured tool

Answer the following questions#

  1. What are some of the techniques to unit test methods/classes that work with (psuedo)random generators?

  2. How do you unit test a method that does not return anything but writes to standard output? What if it outputs to a file?

Task 1#

Consider the following "collectThenPrint" helper method that reads size of an int array from standard inout, then collects that many integers, and finnaly prints the content of the array to standard output:

public void collectThenPrint() {
Scanner scanner = new Scanner(System.in);
// Ask the user for the size of the array
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
// Create an array of the specified size
int[] numbers = new int[size];
// Collect the integers from the user
System.out.println("Enter " + size + " integers:");
for (int i = 0; i < size; i++) {
System.out.print("Enter number " + (i + 1) + ": ");
numbers[i] = scanner.nextInt();
}
// Print the elements of the array
System.out.println("The elements in the array are:");
for (int i = 0; i < size; i++) {
System.out.println("Element " + (i + 1) + ": " + numbers[i]);
}
// Close the scanner
scanner.close();
}

write a unit test to verify that collectThenPrint indeed collects "4, {1, 2, 3, 4}" from standard Input and and then prints the content of the array "{1, 2, 3, 4}" correctly on standard ouput.

Task 2#

Get a free API key to work with JHU SIS API from here if you do not have one already. Utilizing rest-assured,

  1. Assert that the Carey Business School exists within JHU.
  2. Assert that the course Data Structures is offered within Whiting School of Engineering in the current academic term
  3. Assert that sending a post request to a SIS endpoint returns the status error code 405.

Extra#

Rewrite the above test cases using Postman or a similar standalone rest api testing framework.