CSci 2101 Lab 6. Introduction to jUnit

Due Tuesday March 8th at 11:59pm

20 points

Work in groups. You must use Eclipse.

jUnit resources: jUnit API (find the Assert class)

Starting code

The Card class:


public class Card {

	final private String suit;
	final private String value;
	private int numericValue = 0;
	final private String[] arr = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

	public Card(String suit, String value){
		this.suit = suit;
		this.value = value;
		for (int i = 0; i < 13; i++) {
			if (arr[i].equals(value)) {
				numericValue = i + 2;			
			}
		}
	}

	public String getSuit(){
		return suit;
	}

	public String getValue(){
		return value;
	}

	public int getNumericValue(){
		return numericValue;
	}

	public String toString(){
		return value + " of " + suit;
	}

	public int compareTo(Card that){
		return this.numericValue - that.numericValue;
	}
}
  

The (empty) exception class

     
public class InvalidParameterException extends Exception {

}
  

The JUnit tests


import static org.junit.Assert.*;
import org.junit.Test;


public class CardTests {
	@Test
	public void testGetSuitValue() {
		Card aceSpades = new Card("Spades", "A");
		assertEquals("Spades",aceSpades.getSuit());
		assertEquals("A",aceSpades.getValue());
		assertEquals(14,aceSpades.getNumericValue());
	}
	
	@Test
	public void testNumericValue() {
		Card aceSpades = new Card("Spades", "A");
		Card tenSpades = new Card("Spades", "10");
		Card queenHearts = new Card("Hearts", "Q");
		assertEquals(14,aceSpades.getNumericValue());
		assertEquals(10,tenSpades.getNumericValue());
		assertEquals(12,queenHearts.getNumericValue());
	}
	
	@Test
	public void testToString() {
		Card aceSpades = new Card("Spades", "A");
		Card twoSpades = new Card("Spades", "2");
		Card queenHearts = new Card("Hearts", "Q");
		assertEquals("A of Spades",aceSpades.toString());
		assertEquals("2 of Spades",twoSpades.toString());
		assertEquals("Q of Hearts",queenHearts.toString());
	}
	
	@Test
	public void testCompareToNotEqual() {
		Card aceSpades = new Card("Spades", "A");
		Card twoSpades = new Card("Spades", "2");
		Card queenHearts = new Card("Hearts", "Q");
		assertTrue(aceSpades.compareTo(twoSpades) > 0);
		assertTrue(twoSpades.compareTo(aceSpades) < 0);
		assertTrue(twoSpades.compareTo(queenHearts) < 0);
	}
	
	@Test
	public void testCompareToEqual() {
		Card aceSpades = new Card("Spades", "A");
		Card aceHearts = new Card("Hearts", "A");
		assertEquals(0, aceSpades.compareTo(aceHearts));
		assertEquals(0, aceHearts.compareTo(aceHearts));
		assertEquals(0, aceHearts.compareTo(aceSpades));
	}
	
	/*
	@Test(expected=InvalidParameterException.class, message="")
	public void testExceptionValue() {
		Card oneSpades = new Card("Spades", "1");
	}
	
	@Test(expected=InvalidParameterException.class, message="")
	public void testExceptionSuit() {
		Card fiveApples = new Card("Apples", "5");
	}	*/

}

    

Task 1: missing exceptions for the Card class

Uncomment the exception tests in jUnit, fill in the message you would like to see, and make the tests pass.

Task 2: priority queue tests

Copy the starting priority queue code and change the test class into a jUnit test class. Note that the enhanced "for" loop is difficult to change. Add the queue elements into an array list, and then use the corresponding jUnit assertion method.

How to submit:

Send me all of the code you worked on at the end of the lab (CC your group). The lab grade is based on in-class activities and the code sent to me at the end of the lab (or, if final, incorporated into the solution).

Add at least one of your own tests.


CSci 2101 course web site.