This discussion is archived
7 Replies Latest reply: Feb 22, 2008 2:11 PM by 807603

java 101: constructing test classes: newbie having trouble

Currently Being Moderated
OK- I'm in a 100 level course here, so I'm sure this is like BASIC to you guys.

Here is my MazeCalculator.java:

/**
* Calculator for maze data.
*
* @author
* @version
*/
public class MazeCalculator

private int timeInSeconds;
private double distance;
private double distanceInInches;
private double distanceInCm;

// Est maze distance.
// @param inTimeInSeconds Maze time in seconds.

public MazeCalculator(int inTimeInSeconds)
{
this.timeInSeconds = inTimeInSeconds;
this.distance = 100;
}

// Est v
// @param inTimeInSeconds

public MazeCalculator(int inTimeInSeconds, float inDistance)
{
this.timeInSeconds = inTimeInSeconds;
this.distance = inDistance;
}

// Est v
// @param setDistanceIncm

public void setDistanceInCm(int inDistanceInCm)
{
this.distanceInCm = inDistanceInCm;

}

// Est v
// @param distanceInCm

public double getDistanceInCm()
{
this.distanceInCm = distance;

return this.distanceInCm;
}

// Get the Maze distance in inches.
// @param Calcs the distance in inches.

public double getDistanceInInches()
{
distanceInInches = getDistanceInCm() / 2.54;
return distanceInInches;
}

// Set the Maze time in seconds.
// @param inTimeInSeconds Maze time in seconds.

public void setTimeInSeconds(double inTimeInSeconds)
{
timeInSeconds = 100;
}

// Get the Maze time in seconds.
// @return int The maze time in seconds.

public int getTimeInSeconds()
{
return timeInSeconds;
}

// Get rat's rate in centimeters per second.
// @return float Rat's rate in kilometers per second.

public double getCPS()
{
return ( distanceInCm / timeInSeconds );
}

// Get rat's rate in inches per second.
// @return float Rat's rate in inches per second.

public double getIPS()
{
return ( distanceInInches / timeInSeconds );
}

// Get rat's rate in seconds per meter.
// @return float Rat's rate in seconds per meter.

public double getSecondsPerMeter()
{
return ( timeInSeconds / ( distanceInCm / 100));
}

// Get rat's rate in seconds per yard.
// @return float Rat's rate in seconds per yard.

public double getSecondsPerYard()
{
return ( timeInSeconds / ( distanceInInches / 36));
}

// Provide rat's Maze statistics in string format.
// @return String Rat's maze information in string format.

public String toString()
{
// toString Output Code Below
// @return String Shows everything
System.out.println("Time in Seconds: " + getTimeInSeconds());
System.out.println("Distance: " + getDistanceInCm() + " cm, " + getDistanceInInches() + " in");
System.out.println("Centimeters per second: " + getCPS());
System.out.println("Inches per second: " + getIPS());
System.out.println("Seconds per meter: " + getSecondsPerMeter());
System.out.println("Seconds per yard: " + getSecondsPerYard());
System.out.println("This is right, damn it.");
return "";
}
}

Here is my test class:

/**
* The test class MazeCalculatorTest.
*
* @author
* @version
*/
public class MazeCalculatorTest extends junit.framework.TestCase
{

/**
* Default constructor for test class MazeCalculatorTest.
*/
public MazeCalculatorTest()
{
// default constructor
}

/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
protected void setUp()
{
// anything needed prior to each test case
}

/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
protected void tearDown()
{
// anything needed after test cases complete
}

/**
* Test the first constructor.
*/
public void testConstructor1()
{
// <This is not the correct test.>
assertTrue(false);

}

/**
* Test the second constructor.
*/
public void testConstructor2()
{
// <This is not the correct test.>
assertTrue(false);
}

/**
* Test the getDistanceInCm method.
*/
public void testgetDistanceInCm()
{
// <This is not the correct test.>
//MazeCalculator sampleMaze1 = new MazeCalculator(14 , 120);
//double actualDistanceInCm = sampleMaze1.getDistanceInCm();
//assertTrue(actualDistanceInCm == 14);

}

{
// <This is not the correct test.>
assertTrue(false);
}

/**
* Test the setDistance method.
*/
public void testsetDistanceInCm()
{
// <This is not the correct test.>
}

/**
* Test the getDistanceInInches method.
*/
public void testgetDistanceInInches()
{
// <This is not the correct test.>
assertTrue(false);
}

/**
* Test the getTimeInSeconds method.
*/
public void testGetTimeInSeconds()
{
// <This is not the correct test.>
assertTrue(false);
}

/**
* Test the setTimeInSeconds method.
*/
public void testSetTimeInSeconds()
{
// <This is not the correct test.>
//MazeCalculator sampleMaze1 = new MazeCalculator(14 , 120);
//int actualTimeInSeconds = sampleMaze1.setTimeInSeconds();
//assertTrue(actualDistance = 100);
//assertTrue(false);
}

/**
* Test the getCPS method.
*/
public void testgetCPS()
{
// <This is not the correct test.>
assertTrue(false);
}

/**
* Test the getIPS method.
*/
public void testgetIPS()
{
// <This is not the correct test.>
assertTrue(false);
}

/**
* Test the getSecondsPerMeter method.
*/
public void testGetSecondsPerMeter()
{
// <This is not the correct test.>
assertTrue(false);
}

/**
* Test the getSecondsPerYard method.
*/
public void testGetSecondsPerYard()
{
// <This is not the correct test.>
assertTrue(false);
}

/**
* Test the toString method.
*/
public void testToString()
{
// <This is not the correct test.>
assertTrue(false);
}

}

I've commented out things that aren't working, hoping to stumble on one that works basically. I'm not too smart with this stuff; if someone could show me a few examples in the context of the program above, I could probably get the rest. Thanks for any help!
• 1. Re: java 101: constructing test classes: newbie having trouble
Currently Being Moderated
Don't you have a specific question about some code you have written?
• 2. Re: java 101: constructing test classes: newbie having trouble
Currently Being Moderated
Yes. My specific question is on the testing portion. The program compiles and runs fine. I am confused on the syntax of the test methods. I know I'm not writing them correctly because we are using blueJ, and I run the tests, and I'm getting failures. I've only tried doing two. I'll reprint them here:

public void testgetDistanceInCm()
{
// <This is not the correct test.>
//MazeCalculator sampleMaze1 = new MazeCalculator(14 , 120);
//double actualDistanceInCm = sampleMaze1.getDistanceInCm();
//assertTrue(actualDistanceInCm == 14);

}

I know we have to use the assertTrue method. I don't know if I'm using the incorrect parameters after = new MazeCalculator(14, 120) or if I'm doing something else wrong. I'd be happy to email you the whole project. I'm not looking for someone to do the work for me, I just need one or two examples to point me in the right direction.

Thanks-
• 3. Re: java 101: constructing test classes: newbie having trouble
Currently Being Moderated
Um, that code is commented out, so it won't run.

Or did you mean something else when you said "getting failures"? That phrase doesn't tell us much.
• 4. Re: java 101: constructing test classes: newbie having trouble
Currently Being Moderated
//MazeCalculator sampleMaze1 = new MazeCalculator(14 , 120);
This code, if uncommented, would create a MazeCalculator and set time to 14, and distance to 120
//double actualDistanceInCm = sampleMaze1.getDistanceInCm();
This would invoke the getDistanceInCm method, which simply sets distanceInCm to distance (120) and returns that value
//assertTrue(actualDistanceInCm == 14);
This would fail, because actualDistanceInCm is 120 (see above)
• 5. Re: java 101: constructing test classes: newbie having trouble
Currently Being Moderated
{
// <This is not the correct test.>
MazeCalculator sampleMaze1 = new MazeCalculator(14 , 120);
double actualDistanceInCm = sampleMaze1.getDistanceInCm();
assertTrue(actualDistanceInCm == 120);

}

I tried this one, and it fails as well. I'm using the blueJay IDE.

http://freud.franklin.edu/melvil01/lab2/

If you go to this directory, download the zip file. Unzip into a new directory on your desktop. BlueJ is a free, 4MB IDE. Maybe that will present a clearer picture on what I'm trying to convey. I really appreciate the help, I'm very very new at this. If I could get one or two examples, I think I could pick up on how to develop testing methods. I was trying to go off of the previous assignment as a template.

Thanks
• 6. Re: java 101: constructing test classes: newbie having trouble
Currently Being Moderated
I tried this one, and it fails as well. I'm using the blueJay IDE.
You're still using vague statements like "it fails" instead of showing the actual result (compile time error, run-time exception, expected versus actual results, things like that).

>
http://freud.franklin.edu/melvil01/lab2/

If you go to this directory, download the zip file. Unzip into a new directory on your desktop. BlueJ is a free, 4MB IDE. Maybe that will present a clearer picture on what I'm trying to convey. I really appreciate the help, I'm very very new at this. If I could get one or two examples, I think I could pick up on how to develop testing methods. I was trying to go off of the previous assignment as a template.
That's too much work for me. The onus is on you to provide a clear description of what's going on, not to just dump it off for someone else to go have to download your stuff, compile it, run it, etc -- just to see what the real problem is that you're witholding and abbreviating for some reason as "it fails".
• 7. Re: java 101: constructing test classes: newbie having trouble
Currently Being Moderated
http://freud.franklin.edu/melvil01/lab2/

I made 2 screen shots of what I'm talking about. You right click on the object and can run individual tests within it. The test is what is failing. The program compiles fine and runs correctly.