This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Java program keeps failing test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java program keeps failing test" Watch "Java program keeps failing test" New topic
Author

Java program keeps failing test

John Sammy
Greenhorn

Joined: Sep 28, 2012
Posts: 2
Hi Everyone,

I'm unfortunately new to this site but it really seems to have some great information. I'm currently working on a homework assignment and cannot find what code is wrong in TestConstructor2, testToString, setTotalTimeInSecs, and setNumOfTrials. Every time I run a test, these four fail. I'm still new at learning Java, so could anyone please give me a hand on this and show me what is wrong and why? Thanks!!
public class RatTest extends junit.framework.TestCase
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9962
    
163

John, welcome to the forums!

What exactly do those failure messages say and what does the Rat class look like?

By the way, the forum software here allows you to wrap your code/logs/xml in a "Code" block so that it can be properly formatted. Please use the Code button in the message editor window to wrap the appropriate text in those code blocks, the next time you post some code. There's also the Preview button which you can use to see how the post will look like before posting.

[My Blog] [JavaRanch Journal]
John Sammy
Greenhorn

Joined: Sep 28, 2012
Posts: 2
The test errors are:
RatTest.testSetTotalTimeInSecs says expected:<4685> but was:<12> AssertionFailedError: expected:<4685> but was:<12>
RatTest.testSetNumOfTrials says expected:<OH1234567> but was:<12> AssertionFailedError: expected:<OH1234567> but was:<12>
RatTest.testConstructor2 says OH1234567 AssertionFailedError: OH1234567
RatTest.testToString says null AssertionFailedError: null
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

John Sammy wrote:The test errors are:
RatTest.testSetTotalTimeInSecs says expected:<4685> but was:<12> AssertionFailedError: expected:<4685> but was:<12>
RatTest.testSetNumOfTrials says expected:<OH1234567> but was:<12> AssertionFailedError: expected:<OH1234567> but was:<12>
RatTest.testConstructor2 says OH1234567 AssertionFailedError: OH1234567
RatTest.testToString says null AssertionFailedError: null


Take it one step at a time, and look at each one methodically.

Let's start with the first one:


So you called getTotalTimeInSecs and you epxected it to be 4685 (and by the way, if you're storing and returning it as a number, which you should be doing, then use assertEquals(4685, ...), rather than ("4685", ...)), but it was 12. And we can see that 12 is what was passed as one of the constructor parameters.

So this should suggest to you that there's some combination of one or more of 1) the 12 c'tor parameter getting stored in the wrong place, 2) the setTotalTimeInSecs() method setting the wrong field, 3) the getTotalTimeInSecs() method returning the wrong field.

This is exactly why we do these tests. So now you know where to look in your Rat class for something that's not doing what it should.

Also, it would be a good idea to pass a different value to the set() method than what you're passing to the constructor. That way, if we see the right value, we KNOW it's because the setter was working right, since that's the only place that value comes up. As it is, your setter method could be totally failing, and you could still get the right answer, making it look like it's working, because you passed that same value to the c'tor.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java program keeps failing test
 
Similar Threads
Adding JScrollPane to JPanel?
Picture not appearing
Accessing an object without a reference
Just getting started
New to java testing