Ask coding questions

← Back to all posts
Using Classes in Unit Tests
AustinMcGowan (1)

I am trying to set up some Unit tests. I have a class called Cat and what I want me unit test to create some Cat objects. however I can't see to get this to work. The Cat class is in a file called Cat but the Unit test is giving an error saying it can't find symbol Cat.

Commentshotnewtop
AustinMcGowan (1)

I figured out the answer to my problem.

My Cat file was called "Cat" instead of "Cat.java". Changing it to Cat.java has fixed all my problems.

AustinMcGowan (1)

Here is the code for my Model:

public class Cat
{
private String mySubFamily;
private String myGenus;
private String myCommonName;

/**
 * Constructs a newly created Cat object.
 * @param subFamily The subfamily of this cat object.
 * @param genus The genus of this cat object.
 * @param commonName The common name of this cat object.
 **/	
public Cat(String subFamily, String genus, String commonName) {
	mySubFamily = subFamily;
	myGenus = genus;
	myCommonName = commonName;
}

/**
 * Returns the sub family of this cat object.
 * @return the sub family of this cat object.
 */
public String getSubFamily() {
	return mySubFamily;
}

/**
 * Returns the genus of this cat object.
 * @return the genus of this cat object.
 */
public String getGenus() {
	return myGenus;
}

/**
 * Returns the common name of this cat object.
 * @return the common name of this cat object.
 */
public String getCommonName() {
	return myCommonName;
}

/**
 * Compares this Cat to the specified object. The result is true if and only if
 * the argument is not null and is a Cat object that has the same common name
 * as this Cat.
 * @param anObject The object to compare this Cat against 
 * @return true if the given object represents a Cat equivalent to this Cat,
 * false otherwise
 */
public boolean equals(Object anObject) {
	if (anObject instanceof Cat)
	{
		Cat c = (Cat) anObject;
		return getCommonName().equals(c.getCommonName());
	}
	return false;
}

/**
 * Returns a string representation of this Cat object.
 * @return a string representation of this Cat object. 
 */
public String toString() {
	return "[Family: Felidae/SubFamily: " + getSubFamily() + "/Genus: " + getGenus() +
		   "/Common Name: " + getCommonName() + "]";
}

}

And the code for my unit tests:

@Test
public void lionsAndTigers() {
// Failure message:
// Lions should not equal tigers, check your .equals method.
assertFalse(lion.equals(tiger));
}
@Test
public void felixAndFluffy() {
// Failure message:
// Felix SHOULD equal fluffy, check your .equals method.
assertTrue(felix.equals(fluffy));
}
@Test
public void fluffyAsInteger() {
// Failure message:
// Fluffy should not be an integer.
assertFalse(fluffy.equals(new Integer(3)));
}

AustinMcGowan (1)

I am creating the Cat objects and calling their constructors in the advanced section:

// Initialization code(click to edit)
Cat lion ;
Cat tiger ;
Cat felix ;
Cat fluffy ;
@Before
public void setUp() {
// Setup code(click to edit)
lion = new Cat("Pantherinae", "Panthera", "Lion");
tiger = new Cat("Pantherinae", "Panthera", "Tiger");
felix = new Cat("Felinae", "Felis", "Domestic Cat");
fluffy = new Cat("Felinae", "Felis", "Domestic Cat");
}
@After
public void tearDown() {
// TearDown code(click to edit)
}

timmy_i_chen (1003)

What language are you creating unit tests in?

AustinMcGowan (1)

@timmy_i_chen Java, posted the code above.