In: Computer Science
- Java Programming
- Develop JUnit test for a designated program
- Perform JUnit testing for on method that return both successful and unsuccessful results
- Create a JUnit test class for testing the getAverageScore and the getTotalScore methods. On class per test.
public class Student
{
private String name;
private double totalScore;
private int quizCount;
public Student()
{
name = "";
totalScore = 0;
quizCount = 0;
}
n the name
*/
public Student(String n)
{
name = n;
totalScore = 0;
quizCount = 0;
}
public void setName(String aName)
{
name = aName;
}
public String getName()
{
return name;
}
public void addQuiz(int score)
{
if(score >= 0 && score <= 100)
{
totalScore = totalScore + score;
quizCount = quizCount + 1;
}
else
{
System.out.println("Score must be between 0 and 100,
inclusive");
}
}
public double getTotalScore()
{
return totalScore;
}
public double getAverageScore()
{
return totalScore / quizCount;
}
}
----------------------
import java.util.Scanner;
public class StudentTester
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String repeat = "n";
int currentScore = 0;
System.out.print("Enter Student's name: ");
String sName = keyboard.nextLine();
Student student1 = new Student(sName);
do
{
System.out.print("Enter quiz score: ");
currentScore = keyboard.nextInt();
student1.addQuiz(currentScore);
System.out.print("Do you wish to enter another score, (Enter y for
Yes, n for no: " );
keyboard.nextLine();
repeat = keyboard.nextLine();
}while(repeat.equalsIgnoreCase("y"));
String studName = student1.getName();
double totalScore = student1.getTotalScore();
double avgScore = student1.getAverageScore();
System.out.printf("%s total quiz score is: %,2f and average quiz
score is: %6.2f \n", studName, totalScore, avgScore);
Student student2 = new Student();
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class StudentTester {
private Student student;
@Before
public void setUp() {
this.student = new Student();
student.setName("User1");
student.addQuiz(12);
student.addQuiz(45);
student.addQuiz(67);
student.addQuiz(87);
}
@Test
public void positiveTestTotal() {
assertEquals(211, student.getTotalScore(), 0.0);
}
@Test
public void negativeTestTotal() {
assertNotSame("Assert", 2411, student.getTotalScore());
}
@Test
public void positiveTestAverage() {
assertEquals(52.75, student.getAverageScore(), 0.0);
}
@Test
public void negativTestAverage() {
assertNotSame("Should not Match", 211, student.getTotalScore());
}
@Test
public void testGetName() {
Student student = new Student();
student.setName("User2");
assertSame("Should Match", "User2", student.getName());
}
@Test
public void testNegativeGetName() {
Student student = new Student();
student.setName("User2");
assertNotSame("Should Not Match", "User1", student.getName());
}
@Test
public void successfulTestaddQuix() {
Student student = new Student();
student.setName("User2");
student.addQuiz(12);
assertEquals("Should Match", 12, student.getAverageScore(), 0.0);
assertEquals("Should Match", 12, student.getTotalScore(), 0.0);
}
}