Those who know Java: Could use some guidance on Unit Testing.

greedostick

Obsessed Neo-Fan
15 Year Member
Joined
Aug 11, 2003
Posts
4,476
My code for the classes are below. Basically I am doing a unit test to a toString.

I don't think we ever covered this in our book, I can not find it. I am so close to finishing this lab, I just need to get this last test completed.

First, all my code is below the comments section. Everything else was provided in the lab source.

Second, I really don't think "num" goes in all my tests. That's just the only thing that would compile.

/**
* Return a string representation of the WholeLifePolicy.
*
* @return String output string.
*
* <pre>
* Produce output in the following format:
*
* Policy Information:
* Policy #: WLP1000000
* Policy Years: 20
* Face Value: 50000.0
* Beneficiary: John Doe
*
* </pre>
*/
public String toString()
{
String output = "Policy Information:\n";
output = output + "Policy #: " + this.getPolicyNum() + "\n";

// your code here, finish the output string
output = output + "Face Value" + this.getFaceValue() + "\n";
output = output + "Policy Years:" + this.getPolicyYrs() + "\n";
output = output + "Beneficiary" + this.getBeneficiary() + "\n";
return output;

}

//////////////////////////////////////
Here's the test case.
/////////////////////////////////////

/**
* Test the toString method.
*/
@Test
public void testToString()
{
String num = "WLP1234567";
double value = 50000.0;
int years = 20;
String name = "Katie Perry";
WholeLifePolicy policy = new WholeLifePolicy(num, value, years, name);

String text = policy.toString();
assertTrue(text.contains("Policy Information:"));

assertTrue(text.contains("Policy #:"));
assertTrue(text.contains(num));

// your code here, finish the testing of the toString() method
// checking for both the headings and face value, policy years
assertTrue(text.contains("Face Value:"));
assertTrue(text.contains(num));

assertTrue(text.contains("Policy Years:"));
assertTrue(text.contains(num));

assertTrue(text.contains("Beneficiary:"));
assertTrue(text.contains(num));



}
}
 

fluxcore

Another Striker
Joined
Nov 4, 2013
Posts
324
You haven't actually said what the problem you're having is.

Reading the errors from compilation should give a pretty good idea when you've done something wrong, or if you're using Eclipse it can tell you as you write the code, even.

Also, checking just for 'contains' in a string isn't particularly rigorous, since the values could be returned against the wrong labels and the test would still pass.
 
Top