1 Replies - 558 Views - Last Post: 03 April 2013 - 09:31 AM Rate Topic: -----

#1 jayemes22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-March 13

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 javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/**
 * @author Julia Stewart
 *Program creates and displays Bob Sleigh Team's. User can input the players.
 */
public class Driver extends JFrame{
	
	private int WIDTH = 400;
	private int HEIGHT = 450;
	private JPanel mainPanel;
	private JPanel northPanel, centerPanel, southPanel;
	private JPanel northHeader, northLogo, northCenter;
	
	//northPanel components
		//northHeader
		private JLabel northPanelTitle;
	
		//northCenter
		private JLabel country;
		private JTextField countryField;
		
		private JPanel genderPanel;
		private JRadioButton men, women;
		
		private JPanel numberOfMembers;
		private JRadioButton twoMember, fourMember;
		
		private JButton createTeam;
		
		//northWest
		private JLabel olympicLogo;
		
	//centerPanel components
		private JLabel firstName;
		private JLabel lastName;
		private JTextField fName;
		private JTextField lName;
		
		private JPanel playerPosition;
		
		ButtonGroup position;
		private JRadioButton pilot, brakeman, pusher;
		
		
	//southPanel components
		private JButton addPlayer;
		private JButton printTeam;
		private JButton newTeamButton;
		private JButton exit;
		
	public Driver()
	{
		setTitle("Create Bob Sleigh Team");
		setSize(WIDTH, HEIGHT);
		buildMainPanel();
		add(mainPanel);
		
		setVisible(true);
	}
	
	/**
	 * Builds the main panel which holds all panels. 
	 */
	public void buildMainPanel()
	{
		mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
		
		buildNorthPanel();
		buildCenterPanel();
		buildSouthPanel();
		
		mainPanel.add(northPanel, BorderLayout.NORTH);
		mainPanel.add(centerPanel, BorderLayout.CENTER);
		mainPanel.add(southPanel, BorderLayout.SOUTH);
		
	}
	
	//NORTH PANEL STARTS HERE
	/**
	 * Builds the entire northern panel
	 */
	public void buildNorthPanel()
	{
		northPanel = new JPanel();
		northPanel.setLayout(new BorderLayout());
		northPanel.setPreferredSize(new Dimension (400, 225));
		
		buildNorthHeader();
		buildNorthLogo();
		buildNorthCenter();
		
		northPanel.add(northHeader, BorderLayout.NORTH);
		northPanel.add(northLogo, BorderLayout.WEST);
		northPanel.add(northCenter, BorderLayout.CENTER);
		
	}
	
	/**
	 * builds the north region of the entire northern panel
	 */
	public void buildNorthHeader()
	{
		northHeader = new JPanel();
		northPanelTitle = new JLabel("2014 Olympic BobSleigh Teams");
		
		northHeader.add(northPanelTitle);
	}
	
	/**
	 * builds the west region of the entire northern panel. includes olympic logo.
	 */
	public void buildNorthLogo()
	{
		northLogo = new JPanel();
		ImageIcon image = new ImageIcon("C:\\Users\\julia\\Documents\\Eclipse\\Bob Sleigh Team\\src\\sochi2014.jpg");
		olympicLogo = new JLabel(image);
		
		northLogo.add(olympicLogo);
	}
	
	/**
	 * builds the central, content region of the entire northern panel
	 */
	public void buildNorthCenter()
	{
		northCenter = new JPanel();
		
		country = new JLabel("Country");
		countryField = new JTextField(10);
		
		//Type: gender
		genderPanel = new JPanel();
		genderPanel.setBorder(BorderFactory.createTitledBorder("Type"));
		men = new JRadioButton("Men's");
		women = new JRadioButton("Women's");
		
		ButtonGroup gender = new ButtonGroup();
		gender.add(men);
		gender.add(women);
		
		genderPanel.add(men);
		genderPanel.add(women);
		
		//Number of Members
		numberOfMembers = new JPanel();
		numberOfMembers.setBorder(BorderFactory.createTitledBorder("Number of Members"));
		twoMember = new JRadioButton("2 Member Team");
		fourMember = new JRadioButton("4 Member Team");
		
		ButtonGroup teamSize = new ButtonGroup();
		teamSize.add(twoMember);
		teamSize.add(fourMember);
		
		numberOfMembers.add(twoMember);
		numberOfMembers.add(fourMember);
		
		//Create Team button
		createTeam = new JButton("Create Team");
		createTeam.addActionListener(new createListener());
		
		//adding to northCenter panel
		northCenter.add(country);
		northCenter.add(countryField);
		northCenter.add(genderPanel);
		northCenter.add(numberOfMembers);
		northCenter.add(createTeam);
	}
	
	
	//CENTER PANEL STARTS HERE
	/**
	 * builds the entire center panel
	 */
	public void buildCenterPanel()
	{
		centerPanel = new JPanel();
		
		//Player Name
		firstName = new JLabel("First Name: ");
		fName = new JTextField(22);
		lastName = new JLabel("Last Name: ");
		lName = new JTextField(22);
		
		//Position panel
		playerPosition = new JPanel();
		playerPosition.setBorder(BorderFactory.createTitledBorder("Position"));
		
		pilot = new JRadioButton ("pilot");
		brakeman = new JRadioButton ("brakeman");
		pusher = new JRadioButton ("pusher");
		
		position = new ButtonGroup();
		position.add(pilot);
		position.add(brakeman);
		position.add(pusher);
		
		playerPosition.add(pilot);
		playerPosition.add(brakeman);
		playerPosition.add(pusher);
		
		//adding to centerPanel
		centerPanel.add(firstName);
		centerPanel.add(fName);
		centerPanel.add(lastName);
		centerPanel.add(lName);
		centerPanel.add(playerPosition);
		
		centerPanel.setVisible(false);
	}
	
	//SOUTH PANEL STARTS HERE
	/**
	 * builds the entire southern panel. includes the 4 buttons.
	 */
	public void buildSouthPanel()
	{
		southPanel = new JPanel();
		
		addPlayer = new JButton ("Add Player");
		printTeam = new JButton ("Print Team");
		newTeamButton = new JButton ("New Team");
		exit = new JButton ("Exit");
		
		//assign action listeners
		addPlayer.addActionListener(new addPlayerListener());
		printTeam.addActionListener(new printTeamListener());
		newTeamButton.addActionListener(new newTeamListener());
		exit.addActionListener(new exitListener());
		
		//adding to southPanel
		southPanel.add(addPlayer);
		southPanel.add(printTeam);
		southPanel.add(newTeamButton);
		southPanel.add(exit);
		
		southPanel.setVisible(false);
	}
	
	//ACTION LISTENERS

	//Create Team Button
	private class createListener implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			String actionCommand = e.getActionCommand();
			if (actionCommand.equals("Create Team"))
			{
				String genderType = "";
				if (men.isSelected())
					genderType = "Men's";
				if (women.isSelected())
					genderType = "Women's";
									
				int teamSizeNum = 0;
				if (twoMember.isSelected())
					{
						teamSizeNum = 2;
						brakeman.setVisible(false);
					}
					
				if (fourMember.isSelected())
					{
						teamSizeNum = 4;
						brakeman.setVisible(true);
					}
				
		
				new BobSleighTeam(countryField.getText(), genderType, teamSizeNum);
				BobSleighTeam.newTeam.clear();
				
				countryField.setEditable(false);
				createTeam.setEnabled(false);
				
				centerPanel.setVisible(true);
				southPanel.setVisible(true);
			}
		}	
	}
	
	//Add Player Button
	private class addPlayerListener implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
		//	String firstName = fName.getText();
		//	String lastName = lName.getText();
			String position = "";
			
			if (pilot.isSelected())
				position = "pilot";
			if (pusher.isSelected())
				position = "pusher";
			if (brakeman.isSelected())
				position = "brakeman";
			
			BobSleighTeam.addPlayer(fName.getText(), lName.getText(), position);
			
					fName.setText("");
					lName.setText("");
		}	
	}
	
	//Print Team Button
	private class printTeamListener implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			String printedTeam = BobSleighTeam.printTeam();
			JOptionPane.showMessageDialog(null, printedTeam);
		}	
	}
	
	//New Team Button
	private class newTeamListener implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			countryField.setEditable(true);
			createTeam.setEnabled(true);
			
			centerPanel.setVisible(false);
			southPanel.setVisible(false);
		}	
	}
	
	//Exit Button
	private class exitListener implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			String actionCommand = e.getActionCommand();
			if (actionCommand.equals("Exit"))
			{
				System.exit(0);
			}
		}	
	}
	
	
	// MAIN
	/**
	 * @param Starts program/Create driver object.
	 */
	public static void main(String[] args) 
	{
		new Driver();
	}

}



My mistake. The code above is the Driver of my program.

Here's the JUnit Test Case:


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

}



Is This A Good Question/Topic? 0
  • +

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

#2 Flukeshot  Icon User is online

  • A little too OCD
  • member icon

Reputation: 401
  • View blog
  • Posts: 998
  • Joined: 14-November 12

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1