Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java program keeps failing test

 
John Sammy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Sammy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic