Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22
  1. #21
    fuyongwu is offline Member
    Join Date
    Oct 2011
    Posts
    11
    Rep Power
    0

    Default 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));
    
        }
    }

  2. #22
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Page 2 of 2 FirstFirst 12

Similar Threads

  1. beginner
    By Eiolvit in forum New To Java
    Replies: 4
    Last Post: 07-10-2011, 06:32 PM
  2. Beginner
    By gozuhair in forum New To Java
    Replies: 12
    Last Post: 07-10-2011, 04:14 PM
  3. Need Help - Beginner
    By ooooohmaul in forum New To Java
    Replies: 4
    Last Post: 08-08-2010, 05:22 AM
  4. beginner here...help please
    By shroomiin in forum New To Java
    Replies: 6
    Last Post: 09-16-2009, 12:06 AM
  5. almost done...beginner needs help plz..
    By shongo in forum New To Java
    Replies: 15
    Last Post: 11-10-2008, 09:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •