CS 320 Module Four Tutorial Using JUnit

pdf

School

Southern New Hampshire University *

*We aren’t endorsed by this school

Course

320

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

6

Uploaded by CountIron10886

Report
Tutorial Using JUnit In this tutorial, we will be developing a student object with the following requirements: The student object will have a name field that will be a String. The student object will have an address that will be a String. The student object will have a student ID that is a String and cannot be null, and the String shall be less than five characters. Let’s Look at the Student Class The student class takes in three arguments in the constructor. It takes the name, the address, and the student ID. Please also note that there is a conditional check in the constructor to make sure that the student ID is not null or is no longer than five characters; otherwise, we throw an exception that the input is invalid. This check is done to ensure that the following requirement is being met: “The student object will have a student ID that is a String and cannot be null, and shall be less than five characters.” Next, Let’s Look at Writing the Test in JUnit Before we begin, we need to create a new package in which our tests can exist. Therefore, first, under the “src” folder in our Java project, we create a new package named “test.” Second, we right-click into the new package and select “New” and then select “JUnit test case.”
Now we are ready to start to develop our tests. In this tutorial, we will be creating two test points. The first one will cover the success case. In the success case, we want to ensure that we can create the student object successfully and have confidence that the software meets the following requirements: The student object will have a name field that will be a String. The student object will have an address that will be a String. The student object will have a student ID that is a String and cannot be null, and the String shall be less than five characters. Typically, for JUnit test cases, we want to start each test point with the word “test”: In the first test point, we have chosen the name “testStudent.” In this test point, we are creating a student object with a name, address, and student ID. The next step is to use assertions to test that the data we provided in the constructor is being returned from the get methods. In the second test point named, “testStudentIDTooLong,” we are testing that the student constructor will throw an exception if the ID is too long. We test this by using the following pattern: Assertions.assertThrows(IllegalArgumentException.class, () -> { new Student("Jeff", "1111 E Road", "123456"); }); This assertion checks to see if an illegal exception is being thrown. There are two parts to this assertion: 1. First, we need to identify the exception that is expected to be thrown Assertions.assertThrows(IllegalArgumentException.class, () ->
T 2. Second, we need to provide the behavior that is expected to cause the exception. In this example, we expect that an exception will be thrown when we create a new student object with an ID that is longer than five characters: new Student("Jeff", "1111 E Road", "123456"); Note: We can reuse this approach when creating our mobile application to ensure that requirements are being met: Assertions.assertThrows(“The expected Exception”, () -> { “The class constructor creating the exception”; });
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
How Do I Run My Tests? The next step is to run the tests to make sure our tests are passing. To run the JUnit tests, right-click and select “Run As.” Then, select “JUnit Test” from the menu. Below, we can see the results of the JUnit tests being executed. The green bar means that all test points executed successfully. “Runs:2/2” describes the number of tests pointed, executed, and successfully passed.
We can also run the tests with “Coverage As.” This option will show us the test coverage percentage. We should strive for the highest percentage we can get to ensure that the tests provide sufficient coverage for the object they are testing. In the next example, we should make note of two things. First, the coverage option also runs the JUnit test as in the previous example. Second, we have a new tab below named “Coverage.” If we expand it, we can see that the StudentTest class has tested the Student object at 100% coverage. This means that our tests have sufficiently tested the Student object. Test coverage is based on the following: all the
public application programing interfaces in the software we are testing and on conditional flows. This means, Do our tests all pass through conditional statements?
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help