Given class Student, create a JUnit class, labeled TestStudent, to test the Stud
ID: 3842202 • Letter: G
Question
Given class Student, create a JUnit class, labeled TestStudent, to test the Student class. Class Student has three constructor, and therefore I want you to have 3 tests, one for each constructor.
public Student() , test that firstName & lastName are null
public Student(String fName, String lName ), test that firstName equals fName and lastName equals lName
public Student(String fName, String lName, double gpax ), test that firstName equals fName and lastName equals lName and gpa == gpax.
public class Student
{
private String firstName;
private String lastName;
private double gpa;
public Student()
{
firstName = null;
lastName = null;
}
public Student(String fName, String lName )
{
firstName = fName;
lastName = lName;
}
public Student(String fName, String lName, double gpa )
{
firstName = fName;
lastName = lName;
this.gpa = gpa;
}
public void setFirstName(String newName) {firstName = newName; }
public void setLastName(String newName){ lastName = newName;}
public void setGpa(double newGpa){ gpa = newGpa;}
public String getFirstName() { return firstName; }
public String getLastName() { return lastName;}
public double getGpa() { return gpa;}
}
Explanation / Answer
import static org.junit.Assert.*;
import org.junit.Test;
public class TestStudent {
public class Student
{
private String firstName;
private String lastName;
private double gpa;
public Student()
{
firstName = null;
lastName = null;
}
public Student(String fName, String lName )
{
firstName = fName;
lastName = lName;
}
public Student(String fName, String lName, double gpa )
{
firstName = fName;
lastName = lName;
this.gpa = gpa;
}
}
@Test
public void testStudent() {
Student student = new Student();
assertEquals("First Name is NULL", null, student.firstName);
assertEquals("Last Name is NULL", null, student.lastName);
}
@Test
public void testStudentStringString() {
Student student = new Student("hello","world");
assertEquals("firstName=fName", "hello", student.firstName);
assertEquals("lastname=LName", "world", student.lastName);
}
@SuppressWarnings("deprecation")
@Test
public void testStudentStringStringDouble() {
Student student = new Student("hello","world",10.2);
assertEquals("firstName=fName", "hello", student.firstName);
assertEquals("lastname=LName", "world", student.lastName);
assertEquals(10.2, student.gpa, 0.00001);
}
public static void main(String[] args) {
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.