Question

How do i debug this/ import what needs to be imported? import org.junit.Assert; import static org.junit.Assert.*;...

How do i debug this/ import what needs to be imported?

import org.junit.Assert;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.*;


public class GPACalculatorTest {

GPACalculator calc;

/** Fixture initialization (common initialization
* for all tests). **/
@Before public void setUp() {
calc = new GPACalculator();
}


/* Tests to make sure that the constructor is initializing the gpaTotal variable correctly.
* If you are seeing -1 as your actual value, you either aren't initializing it at all,
* or you changed the name of the variable */
@Test public void constructorTest1(){
double gpaTotal = -1;
try{
/* Note that you do not need to understand how this test works. This is some
* fancy stuff to get around the fact that these variables are private. You
* should not try to do this outside of testing, as it defeats the purpose of
* things being private */
Field gpaTotalField = GPACalculator.class.getDeclaredField("gpaTotal");
gpaTotalField.setAccessible(true);
gpaTotal = (double)gpaTotalField.get(calc);
} catch (Exception e){}
// Message for the test Expected, Actual , Acceptable margin of error
Assert.assertEquals("gpaTotal is initialized correctly by constructor", 0.0 , gpaTotal, 0.0);
}

/* Tests to make sure that the constructor is initializing the numCredits variable correctly.
* If you are seeing -1 as your actual value, you either aren't initializing it at all,
* or you changed the name of the variable */
@Test public void constructorTest2(){
int numCredits = -1;
try{
/* Note that you do not need to understand how this test works. This is some
* fancy stuff to get around the fact that these variables are private. You
* should not try to do this outside of testing, as it defeats the purpose of
* things being private */
Field numCreditsField = GPACalculator.class.getDeclaredField("numCredits");
numCreditsField.setAccessible(true);
numCredits = (int)numCreditsField.get(calc);
} catch (Exception e){}
// Note that int assertions don't need an acceptable margin of error
// Message for the test Expected, Actual
Assert.assertEquals("gpaTotal is initialized correctly by constructor", 0 , numCredits);
}

/* Checks to make sure that getGPA() returns the correct value after the GPACalculator has been created */
@Test public void getGPATest1(){
Assert.assertEquals("getGPA returns the correct value of the GPA after initialization", 0.0, calc.getGPA(), 0.0);
}

/* Checks to make sure that getCredits() returns the correct value after the GPACalculator has been created */
@Test public void getCreditsTest1(){
Assert.assertEquals("getCredits returns the correct value of the GPA after initialization", 0, calc.getCredits());
}

/* Checks to make sure that add() returns the correct GPA after it has been updated*/
@Test public void addTest1(){
double result = calc.addNewGradeToGPA(3.0, 4);
Assert.assertEquals("add returns the correct GPA after it has been updated the first time", 3.0, result, 0.0);
}
  
/* Checks to make sure that add() returns the correct GPA after it has been updated repeatedly*/
@Test public void addTest2(){
calc.addNewGradeToGPA(3.0, 4);
calc.addNewGradeToGPA(4.0, 4);
double result = calc.addNewGradeToGPA(1.0, 4);
double expected = (3.0*4+4.0*4+1.0*4)/(4+4+4);
Assert.assertEquals("add returns the correct GPA after being updated multiple times", expected, result, 0.0001);
}

/* Checks to make sure that add() returns the correct GPA after it has been updated more complexly*/
@Test public void addTest3(){
calc.addNewGradeToGPA(3.0, 4);
calc.addNewGradeToGPA(4.0, 2);
double result = calc.addNewGradeToGPA(1.0, 0);
double expected = (3.0*4+4.0*2+1.0*0)/(4+2+0);
Assert.assertEquals("add returns the correct GPA after it has been updated in more complex ways", expected, result, 0.0001);
}

/* Checks to make sure getGPA continues to return correct GPA values after updates */
@Test public void getGPATest2(){
calc.addNewGradeToGPA(3.0, 4);
calc.addNewGradeToGPA(4.0, 2);
calc.addNewGradeToGPA(1.0, 0);
double result = calc.getGPA();
double expected = (3.0*4+4.0*2+1.0*0)/(4+2+0);
Assert.assertEquals("getGPA returns the correct GPA after it has been updated", expected, result, 0.0001);
}

/* Checks to make sure getCredits continues to return correct values after updates */
@Test public void getCreditsTest2(){
calc.addNewGradeToGPA(3.0, 4);
calc.addNewGradeToGPA(4.0, 2);
calc.addNewGradeToGPA(1.0, 0);
int result = calc.getCredits();
int expected = 4+2+0;
Assert.assertEquals("getCredits returns the correct number of total credits taken", expected, result);
}

/* Checks to make sure reset() resets GPA correctly */
@Test public void resetTest1(){
calc.addNewGradeToGPA(3.0, 4);
calc.addNewGradeToGPA(4.0, 2);
calc.addNewGradeToGPA(1.0, 15);
calc.reset();
double result = calc.getGPA();
double expected = 0;
Assert.assertEquals("reset correctly resets the total gpa", expected, result, 0);
}

/* Checks to make sure reset() resets credits correctly */
@Test public void resetTest2(){
calc.addNewGradeToGPA(3.0, 4);
calc.addNewGradeToGPA(4.0, 2);
calc.addNewGradeToGPA(1.0, 15);
calc.reset();
int result = calc.getCredits();
int expected = 0;
Assert.assertEquals("reset correctly resets the total gpa", expected, result);
}
}
Errors:

----jGRASP exec: javac -g GPACalculatorTest.java
GPACalculatorTest.java:1: error: package org.junit does not exist
import org.junit.Assert;
^
GPACalculatorTest.java:2: error: package org.junit does not exist
import static org.junit.Assert.*;
^
GPACalculatorTest.java:3: error: package org.junit does not exist
import org.junit.Before;
^
GPACalculatorTest.java:4: error: package org.junit does not exist
import org.junit.Test;
^
GPACalculatorTest.java:14: error: cannot find symbol
@Before public void setUp() {
^
symbol: class Before
location: class GPACalculatorTest
GPACalculatorTest.java:22: error: cannot find symbol
@Test public void constructorTest1(){
^
symbol: class Test
location: class GPACalculatorTest
GPACalculatorTest.java:40: error: cannot find symbol
@Test public void constructorTest2(){
^
symbol: class Test
location: class GPACalculatorTest
GPACalculatorTest.java:57: error: cannot find symbol
@Test public void getGPATest1(){
^
symbol: class Test
location: class GPACalculatorTest
GPACalculatorTest.java:62: error: cannot find symbol
@Test public void getCreditsTest1(){
^
symbol: class Test
location: class GPACalculatorTest
GPACalculatorTest.java:67: error: cannot find symbol
@Test public void addTest1(){
^
symbol: class Test
location: class GPACalculatorTest
GPACalculatorTest.java:73: error: cannot find symbol
@Test public void addTest2(){
^
symbol: class Test
location: class GPACalculatorTest
GPACalculatorTest.java:82: error: cannot find symbol
@Test public void addTest3(){
^
symbol: class Test
location: class GPACalculatorTest
GPACalculatorTest.java:91: error: cannot find symbol
@Test public void getGPATest2(){
^
symbol: class Test
location: class GPACalculatorTest
GPACalculatorTest.java:101: error: cannot find symbol
@Test public void getCreditsTest2(){
^
symbol: class Test
location: class GPACalculatorTest
GPACalculatorTest.java:111: error: cannot find symbol
@Test public void resetTest1(){
^
symbol: class Test
location: class GPACalculatorTest
GPACalculatorTest.java:122: error: cannot find symbol
@Test public void resetTest2(){
^
symbol: class Test
location: class GPACalculatorTest
GPACalculatorTest.java:34: error: cannot find symbol
Assert.assertEquals("gpaTotal is initialized correctly by constructor", 0.0 , gpaTotal, 0.0);
^
symbol: variable Assert
location: class GPACalculatorTest
GPACalculatorTest.java:53: error: cannot find symbol
Assert.assertEquals("gpaTotal is initialized correctly by constructor", 0 , numCredits);
^
symbol: variable Assert
location: class GPACalculatorTest
GPACalculatorTest.java:58: error: cannot find symbol
Assert.assertEquals("getGPA returns the correct value of the GPA after initialization", 0.0, calc.getGPA(), 0.0);
^
symbol: variable Assert
location: class GPACalculatorTest
GPACalculatorTest.java:63: error: cannot find symbol
Assert.assertEquals("getCredits returns the correct value of the GPA after initialization", 0, calc.getCredits());
^
symbol: variable Assert
location: class GPACalculatorTest
GPACalculatorTest.java:69: error: cannot find symbol
Assert.assertEquals("add returns the correct GPA after it has been updated the first time", 3.0, result, 0.0);
^
symbol: variable Assert
location: class GPACalculatorTest
GPACalculatorTest.java:78: error: cannot find symbol
Assert.assertEquals("add returns the correct GPA after being updated multiple times", expected, result, 0.0001);
^
symbol: variable Assert
location: class GPACalculatorTest
GPACalculatorTest.java:87: error: cannot find symbol
Assert.assertEquals("add returns the correct GPA after it has been updated in more complex ways", expected, result, 0.0001);
^
symbol: variable Assert
location: class GPACalculatorTest
GPACalculatorTest.java:97: error: cannot find symbol
Assert.assertEquals("getGPA returns the correct GPA after it has been updated", expected, result, 0.0001);
^
symbol: variable Assert
location: class GPACalculatorTest
GPACalculatorTest.java:107: error: cannot find symbol
Assert.assertEquals("getCredits returns the correct number of total credits taken", expected, result);
^
symbol: variable Assert
location: class GPACalculatorTest
GPACalculatorTest.java:118: error: cannot find symbol
Assert.assertEquals("reset correctly resets the total gpa", expected, result, 0);
^
symbol: variable Assert
location: class GPACalculatorTest
GPACalculatorTest.java:129: error: cannot find symbol
Assert.assertEquals("reset correctly resets the total gpa", expected, result);
^
symbol: variable Assert
location: class GPACalculatorTest
27 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Your code is correct, as you have correct static imports in test class. However this is a configuration issue.. please search on internet on how to configure junit in jgrasp..

I can not debug the complete issue here, but as a starting point, the junit dependency should be present in your class path. If you check the project properties, in the build path, the junit jar should be present and then only the junits can be run.. if this doesn't resolve the issue, please search on configuring junits in jgrasp, or alternatively, try creating a junit test class by going file ->new and then pasting your code into that.. Thanks!

Add a comment
Know the answer?
Add Answer to:
How do i debug this/ import what needs to be imported? import org.junit.Assert; import static org.junit.Assert.*;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • Given this JUnit test, write a SentenceCounter class that makes the tests pass: import static org.junit.Assert.*;...

    Given this JUnit test, write a SentenceCounter class that makes the tests pass: import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; /** * Tests that will make us practice writing loops */ public class TestSentenceCounter { private static final String SENTENCE1 = "This is my sentence."; private static final String SENTENCE2 = "These words make another sentence that is longer"; private SentenceCounter sc1; private SentenceCounter sc2;    /** * Create two instances we can play with */ @Before public void setup()...

  • import java.util.Scanner; public class creditScore { public static void main(String[]args) { // declare and initialize variables...

    import java.util.Scanner; public class creditScore { public static void main(String[]args) { // declare and initialize variables int creditScore; double loanAmount,interestRate,interestAmount; final double I_a = 5.56,I_b = 6.38,I_c = 7.12,I_d = 9.34,I_e = 12.45,I_f = 0; String instructions = "This program calculates annual interest\n"+"based on a credit score.\n\n"; String output; Scanner input = new Scanner(System.in);// for receiving input from keyboard // get input from user System.out.println(instructions ); System.out.println("Enter the loan amount: $"); loanAmount = input.nextDouble(); System.out.println("Enter the credit score: "); creditScore...

  • PLEASE HELP I'M GETTING A "cannot find symbol - variable CENTER" ERROR: import javax.swing.*; import java.awt.*;...

    PLEASE HELP I'M GETTING A "cannot find symbol - variable CENTER" ERROR: import javax.swing.*; import java.awt.*; /** * Write a description of class FlowLayout here. * * @author (your name) * @version (a version number or a date) */ public class FlowLayout { // instance variables - replace the example below with your own JButton button1,button2,button3; JFrame jFrame;    /** * Constructor for objects of class FlowLayout */ FlowLayout() { // initialise instance variables jFrame= new JFrame("FlowLayout Example"); jFrame.setLayout(new FlowLayout(FlowLayout.CENTER));...

  • How do I test this with JUnit? I watched a video on YouTube about how to...

    How do I test this with JUnit? I watched a video on YouTube about how to create and run a Simple JUnit test in Eclipse IDE but I still don't understand. Here's what I need to test: public class ReverseDomainName {    public static void main(String[] args)    {        String domain = "cs.princeton.edu";        System.out.println("domain is " + domain);        System.out.println("reverse of domain is " + rev_domain(domain));    }       public static String rev_domain(String domain)...

  • I need help with my code. It keeps getting this error: The assignment is: Create a...

    I need help with my code. It keeps getting this error: The assignment is: Create a class to represent a Food object. Use the description provided below in UML. Food name : String calories : int Food(String, int) // The only constructor. Food name and calories must be // specified setName(String) : void // Sets the name of the Food getName() : String // Returns the name of the Food setCalories(int) : void // Sets the calories of the Food...

  • Analyze the code to determine what the code does, how the data is structured, and why...

    Analyze the code to determine what the code does, how the data is structured, and why it is the appropriate data structure (i.e. linked list, stack or queue). Note that these are examples of classes and methods and do not include the "main" or "test" code needed to execute. When you are done with your analysis, add a header describing the purpose of the program (include a description of each class and method), the results of your analysis, and add...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

  • import java.util.ArrayList; import java.util.Scanner; public class AssertDemo {    /* Work on this in a piecewise...

    import java.util.ArrayList; import java.util.Scanner; public class AssertDemo {    /* Work on this in a piecewise fashion by uncommenting and focusing on one section at a time    * in isolation rather than running everything at once.    */    public static void main(String[] args) {        assert(true);        assert(false);               warmUpAsserts();               assertWithPrimitives();               assertWithObjects();               homeworkRelatedAsserts();    }    /*    * Just a...

  • import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {       ...

    import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);               Mileage mileage = new Mileage();               System.out.println("Enter your miles: ");        mileage.setMiles(input.nextDouble());               System.out.println("Enter your gallons: ");        mileage.setGallons(input.nextDouble());               System.out.printf("MPG : %.2f",mileage.getMPG());           } } public class Mileage {    private double miles;    private double gallons;    public double getMiles()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT