Your line manager has instructed you to write unit tests to determine the code coverage in the student management application. You are required to create a test package within the application you created in Section A, which will contain the required unit tests.
You are required to write the following unit tests:
TestSaveStudent(): To supply the student ID, name, age, email, and course to the save student method. The test will determine whether the correct student will be saved to memory.
TestSearchStudent (): To supply the student id to the search student method. The test will determine that the correct student details have been returned.
TestSearchStudent_StudentNotFound(): To supply an incorrect student ID to the search student method. The test will determine that no student was found.
TestDeleteStudent(): supply the student ID to the delete student method. The test will determine that the student has been successfully deleted.
TestDeleteStudent_StudentNotFound(): To supply an incorrect student ID to the delete student method. The test will determine that no student could be found to delete.
TestStudentAge_StudentAgeValid():To supply a valid student age to the student age method. The test will determine that the student’s age is valid.
TestStudentAge_StudentAgeInvalid():To supply an invalid student age to the student age method. The test will determine that the student’s age is less than 16.
TestStudentAge_StudentAgeInvalidCharacter():To supply an invalid character to the student age method. The test will determine that the student age supplied is not a number.
In java
1 answer
```java
import org.junit.Test;
import static org.junit.Assert.*;
public class StudentManagementApplicationTest {
@Test
public void testSaveStudent() {
// Create an instance of the application
StudentManagementApplication application = new StudentManagementApplication();
// Create a student with test data
Student student = new Student("123", "John Doe", 20, "johndoe", "Computer Science");
// Save the student
application.saveStudent(student);
// Retrieve the saved student
Student savedStudent = application.searchStudent("123");
// Assert that the saved student matches the input student
assertEquals(student, savedStudent);
}
@Test
public void testSearchStudent() {
// Create an instance of the application
StudentManagementApplication application = new StudentManagementApplication();
// Create a student with test data
Student student = new Student("123", "John Doe", 20, "johndoe", "Computer Science");
// Save the student
application.saveStudent(student);
// Search for the student
Student searchedStudent = application.searchStudent("123");
// Assert that the searched student matches the input student
assertEquals(student, searchedStudent);
}
@Test
public void testSearchStudent_StudentNotFound() {
// Create an instance of the application
StudentManagementApplication application = new StudentManagementApplication();
// Search for a student with an invalid ID
Student searchedStudent = application.searchStudent("456");
// Assert that no student is found
assertNull(searchedStudent);
}
@Test
public void testDeleteStudent() {
// Create an instance of the application
StudentManagementApplication application = new StudentManagementApplication();
// Create a student with test data
Student student = new Student("123", "John Doe", 20, "johndoe", "Computer Science");
// Save the student
application.saveStudent(student);
// Delete the student
application.deleteStudent("123");
// Search for the deleted student
Student searchedStudent = application.searchStudent("123");
// Assert that no student is found
assertNull(searchedStudent);
}
@Test
public void testDeleteStudent_StudentNotFound() {
// Create an instance of the application
StudentManagementApplication application = new StudentManagementApplication();
// Delete a student with an invalid ID
application.deleteStudent("456");
// No assertion needed as the method should not throw an exception
}
@Test
public void testStudentAge_StudentAgeValid() {
// Create an instance of the application
StudentManagementApplication application = new StudentManagementApplication();
// Create a student with test data
Student student = new Student("123", "John Doe", 20, "johndoe", "Computer Science");
// Save the student
application.saveStudent(student);
// Check the student's age
boolean isValidAge = application.isStudentAgeValid("123");
// Assert that the student's age is valid
assertTrue(isValidAge);
}
@Test
public void testStudentAge_StudentAgeInvalid() {
// Create an instance of the application
StudentManagementApplication application = new StudentManagementApplication();
// Create a student with test data
Student student = new Student("123", "John Doe", 15, "johndoe", "Computer Science");
// Save the student
application.saveStudent(student);
// Check the student's age
boolean isValidAge = application.isStudentAgeValid("123");
// Assert that the student's age is invalid
assertFalse(isValidAge);
}
@Test
public void testStudentAge_StudentAgeInvalidCharacter() {
// Create an instance of the application
StudentManagementApplication application = new StudentManagementApplication();
// Check the age of a student with an invalid ID
boolean isValidAge = application.isStudentAgeValid("456");
// Assert that the student's age is invalid
assertFalse(isValidAge);
}
}
```
Make sure to import the necessary classes (`import junit.framework.TestCase;`) and adjust the code to match the structure and functionality of your student management application.