Results 21 to 22 of 22
Thread: Need help, beginner here
- 10-02-2011, 10:12 AM #21
Member
- Join Date
- Oct 2011
- Posts
- 11
- Rep Power
- 0
Re: Need help, beginner here
Thanks guys, with your help I managed to get most of the code up to 90 percent done in the tests.
Now the only problem is the ToString part in the player class.
Java Code:Testcase: testEquals(model.PlayerTest): FAILED Tom and Harry are different junit.framework.AssertionFailedError: Tom and Harry are different at model.PlayerTest.testEquals(PlayerTest.java:202)
Java Code:package model; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*; public class PlayerTest { public PlayerTest() { } /** * Test the default Player constructor */ @Test public void testDefaultConstructor() { System.out.println("Constructor"); Player instance = new Player(); assertEquals("Default setting - name", "?", instance.getName()); assertEquals("Default setting - cash", 5000, instance.getCash()); assertEquals("Default setting - holding1", 0, instance.getHolding1()); assertEquals("Default setting - holding2", 0, instance.getHolding2()); assertEquals("Default setting - holding3", 0, instance.getHolding3()); } /** * Test the convenience Player constructor */ @Test public void testConvenienceConstructor() { System.out.println("Constructor (convenience)"); Player instance = new Player("Henri", 2000); assertEquals("Default setting - name", "Henri", instance.getName()); assertEquals("Default setting - cash", 2000, instance.getCash()); assertEquals("Default setting - holding1", 0, instance.getHolding1()); assertEquals("Default setting - holding2", 0, instance.getHolding2()); assertEquals("Default setting - holding3", 0, instance.getHolding3()); } /** * Test of setName method, of class Player. */ @Test public void testSetName() { System.out.println("setName"); // test what should be good values String[] good = {"George", "Henri", "StRaNgE_bUt_lEgAl"}; for (String expResult : good) { Player instance = new Player(); instance.setName(expResult); assertEquals("Good name rejected", expResult, instance.getName()); } // test what will be bad values, but they're ok at this point String[] bad = {"", null, "James L", "Hmm - I wonder if we should have a restriction on the length of a name, or should we allow ridiculously long stock names?"}; for (String expResult : bad) { Player instance = new Player(); instance.setName(expResult); assertEquals("Bad name prematurely rejected", expResult, instance.getName()); } } /** * Test of setCash method, of class Player. */ @Test public void testSetCash() { System.out.println("setCash"); // test what should be good values int[] good = {50, 500, 1000, 5000, 100000}; for (int expResult : good) { Player instance = new Player(); instance.setCash(expResult); assertEquals("Good value rejected", expResult, instance.getCash()); } // test what will be bad values, but they're ok at this point int[] bad = {-5, 0, 201, 1, 2, 77}; for (int expResult : bad) { Player instance = new Player(); instance.setCash(expResult); assertEquals("Bad value prematurely rejected", expResult, instance.getCash()); } } /** * Test of setHolding1 method, of class Player. */ @Test public void testSetHolding1() { System.out.println("setHolding1"); // test what should be good values int[] good = {0, 50, 500, 1000, 5000, 100000}; for (int expResult : good) { Player instance = new Player(); instance.setHolding1(expResult); assertEquals("Good value rejected", expResult, instance.getHolding1()); } // test what will be bad values, but they're ok at this point int[] bad = {-5, 201, 1, 2, 77}; for (int expResult : bad) { Player instance = new Player(); instance.setHolding1(expResult); assertEquals("Bad value prematurely rejected", expResult, instance.getHolding1()); } } /** * Test of setHolding2 method, of class Player. */ @Test public void testSetHolding2() { System.out.println("setHolding2"); // test what should be good values int[] good = {0, 50, 500, 1000, 5000, 100000}; for (int expResult : good) { Player instance = new Player(); instance.setHolding2(expResult); assertEquals("Good value rejected", expResult, instance.getHolding2()); } // test what will be bad values, but they're ok at this point int[] bad = {-5, 201, 1, 2, 77}; for (int expResult : bad) { Player instance = new Player(); instance.setHolding2(expResult); assertEquals("Bad value prematurely rejected", expResult, instance.getHolding2()); } } /** * Test of setHolding3 method, of class Player. */ @Test public void testSetHolding3() { System.out.println("setHolding3"); // test what should be good values int[] good = {0, 50, 500, 1000, 5000, 100000}; for (int expResult : good) { Player instance = new Player(); instance.setHolding3(expResult); assertEquals("Good value rejected", expResult, instance.getHolding3()); } // test what will be bad values, but they're ok at this point int[] bad = {-5, 201, 1, 2, 77}; for (int expResult : bad) { Player instance = new Player(); instance.setHolding3(expResult); assertEquals("Bad value prematurely rejected", expResult, instance.getHolding3()); } } /** * Test of toString method, of class Player. */ @Test public void testToString() { System.out.println("toString"); Player instance = new Player(); String expResult = "?... $5000 cash and 0/0/0 holdings"; String result = instance.toString(); assertEquals("Text representation from default constructor", expResult, result); instance = new Player("John", 111); expResult = "John... $111 cash and 0/0/0 holdings"; result = instance.toString(); assertEquals("Text representation from convenience constructor", expResult, result); instance.setHolding1(500); instance.setHolding2(1000); instance.setHolding3(500); expResult = "John... $111 cash and 500/1000/500 holdings"; result = instance.toString(); assertEquals("Text representation from convenience constructor", expResult, result); } /** * Test of equals method, of class Player. */ @Test public void testEquals() { System.out.println("equals"); Player tom = new Player("Tom", 1000); Player harry = new Player("Harry", 5000); Player henri = new Player("Harry", 100); // identical objects assertTrue("Tom is Tom", tom.equals(tom)); // same objects assertTrue("Harry and Henri are the same", harry.equals(henri)); // not same objects assertFalse("Tom and Harry are different", tom.equals(harry)); } }
-
Re: Need help, beginner here
Um, where is Player's latest toString code? All I see is how you propose to test it here.
Similar Threads
-
beginner
By Eiolvit in forum New To JavaReplies: 4Last Post: 07-10-2011, 05:32 PM -
Beginner
By gozuhair in forum New To JavaReplies: 12Last Post: 07-10-2011, 03:14 PM -
Need Help - Beginner
By ooooohmaul in forum New To JavaReplies: 4Last Post: 08-08-2010, 04:22 AM -
beginner here...help please
By shroomiin in forum New To JavaReplies: 6Last Post: 09-15-2009, 11:06 PM -
almost done...beginner needs help plz..
By shongo in forum New To JavaReplies: 15Last Post: 11-10-2008, 08:14 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks