I have been asked to test some classes of my program, however I am unsure how to do so and have been unable to find any tutorials online.
Here is one of the classes I am looking to test:
public class Candidate {
private String name;
private int voteCount = 0;
public Candidate(String name) {
this.name = name;
}
public void setVoteCount(int voteCount) {
this.voteCount = voteCount;
}
public String getName() {
return name;
}
public int getVoteCount() {
return voteCount;
}
}
And here is what it looks like with the automated JUnit testing added:
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 CandidateTest {
public CandidateTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of setVoteCount method, of class Candidate.
*/
@Test
public void testSetVoteCount() {
System.out.println("setVoteCount");
int voteCount = 0;
Candidate instance = null;
instance.setVoteCount(voteCount);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
/**
* Test of getName method, of class Candidate.
*/
@Test
public void testGetName() {
System.out.println("getName");
Candidate instance = null;
String expResult = "";
String result = instance.getName();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
/**
* Test of getVoteCount method, of class Candidate.
*/
@Test
public void testGetVoteCount() {
System.out.println("getVoteCount");
Candidate instance = null;
int expResult = 0;
int result = instance.getVoteCount();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
}
I am unsure how to go about testing the functions. Below is my attempt of a test on testSetVoteCount()
public void testSetVoteCount() {
System.out.println("setVoteCount");
int voteCount = 5;
Candidate instance = new Candidate("Test");
instance.setVoteCount(voteCount);
assertEquals(instance.getVoteCount(), 5);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
I am attempting to set the voteCount value to 5 in the instance object and then use assertEquals to check the correct value is returned, however when I run this test it fails with an initialization error.
The stack trace of the error is below:
org/hamcrest/SelfDescribing
java.lang.NoClassDefFoundError
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
Aucun commentaire:
Enregistrer un commentaire