JUnit Test Case - setUp() for two separate objects

Posted 03 April 2013 - 07:51 AM

I have a JUnit Test Case given to me by my teacher to check one of my classes. I pass all of her tests, but there are commented off sections of the test case for me to fill in my own set of tests. I created another object with a different name (team2), and added players.
Despite a new object with a different name, the setUp() seems to be combining the players of the two separate objects into the same object. This causing me to fail both my teacher's tests, though without my new object I can pass them. Any idea what's wrong? Thanks.

import static org.junit.Assert.*;
import java.util.Scanner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* This is a JUnit test program to test the Data Manager CurlerTeam.
* The following classes must be defined and implemented:
* BobSleighPosition - enumeration
* BobSleighPlayer - data element
* BobSleighTeam - data manager
* @author Professor Myers
*
*/
public class BobSleighTeamTester {
/** A BobSleighTeam object reference used for testing */
BobSleighTeam team;
BobSleighTeam teamTwo;
@Before
/** This method is run before each individual test
* Creates an object of BobSleighTeam and adds three
* BobSleighPlayers to the BobSleighTeam
*/
public void setUp() throws Exception {
team = new BobSleighTeam("Sweden","Men's",4);
team.addPlayer("John", "Smith","pilot");
team.addPlayer("Bob", "Brown", "brakeman");
team.addPlayer("Harold", "Jones", "pusher");
//STUDENT - create another team and add players to it
/* teamTwo = new BobSleighTeam("Canada", "Women's",2);
teamTwo.addPlayer("Emma", "Gold", "pilot");
teamTwo.addPlayer("Anna", "Green", "brakeman");
*/
}
@After
/** This method is run after each individual test
* It sets the team reference to null so the garbage
* collector can reclaim the memory used for the
* BobSleighTeam object
* @throws Exception
*/
public void tearDown() throws Exception {
team = null;
}
@Test
/**
* Test to
* 1. check if the number of members is originally 3
* 2. Add another member, and check if number of members is 4
*
*/
public void testGetNumPlayers() {
assertEquals(3,team.getNumMembers());
team.addPlayer("George", "Jungle", "pusher");
assertEquals(4, team.getNumMembers());
}
@Test
/**
* Test to
* 1. add a new player as a pilot
* check if recognizes there is already a pilot on the team
* 2. add a new player as a brakeman
* check if recognizes there is already a brakeman on the team
* 3. add new player as a pusher - player is added
*/
public void testAddPlayer() {
String result;
result = team.addPlayer("Benny", "Jets", "pilot");
assertEquals("There is already a pilot on this team\nPlayer not added", result);
result = team.addPlayer("Benny","Jets", "brakeman");
assertEquals("There is already a brakeman on this team\nPlayer not added", result);
result = team.addPlayer("Benny","Jets", "pusher");
assertEquals(null, result);
assertEquals(4,team.getNumMembers());
}
@Test
/**
* STUDENT
* Use the team that you created in the setup and test here
*/
public void testAddPlayerSTUDENT()
{
fail("This test is not yet implemented");
}
@Test
/**
* Test to:
* 1. Check if the country name is on the first line
* 2. Check if The team type is on the second line
* 3. Check if Harold is on the fifth line
*/
public void testPrintTeam() {
String result = team.printTeam();
Scanner team = new Scanner(result);
assertEquals("Sweden",team.nextLine()); //Sweden
assertEquals("Men's 4 member team", team.nextLine());
//extract three team players
team.nextLine(); //John Smith Position pilot
team.nextLine(); //Bob Brown Position brakeman
assertEquals("Harold",team.next()); //Harold
}
@Test
/**
* STUDENT
* Use the team that you created in the setup and test here
*/
public void testPrintTeamSTUDENT()
{
fail("This test is not yet implemented");
}
}

Replies To: JUnit Test Case - setUp() for two separate objects

Re: JUnit Test Case - setUp() for two separate objects

Posted 03 April 2013 - 09:31 AM

There's another class you haven't shown us isn't there? I've gone through this code and the trail ends when you create a new BobSleighTeam. I can't see the methods of this class, namely 'addPlayer()'. If you could post the BobSleighTeam class code, that would help me help you.