我正在使用Intellij Idea 14CE。我想測試我的課DistanceOfRoute
JUnit Testing - Intellij Idea
import java.util.*;
/*
Definition of DistanceOfRoute.java class
Finds distances of directly defined routes
1. The distance of the route A-B-C.
2. The distance of the route A-D.
3. The distance of the route A-D-C.
4. The distance of the route A-E-B-C-D.
5. The distance of the route A-E-D.
*/
public class DistanceOfRoute {
//declaration of the HashTable
public Hashtable <Town, Path> trip;
//constructor
public DistanceOfRoute(){
this.trip = new Hashtable<Town, Path>();
}
public int distanceOfDirectRoutes(ArrayList <Town> nodes){
//initialize distance and depth variables to 0, before calculation
int distance = 0;
int depth = 0;
//there must be at least 2 town to get the route distance.
if(nodes.size() < 2){
System.out.println("THERE SHOULD BE AT LEAST 2 TOWNS.");
}
for(int index = 0; index <= nodes.size(); index++){
//check whether index is in the HashTable.If not, print "NO SUCH ROUTE."
if(!this.trip.containsKey(nodes.get(index))){
System.out.println("NO SUCH ROUTE");
}
/*if the index is in the HashTable, control whether any corresponding
route from key to the next town is available.If so, add weight of the
path to the distance and increase depth by 1.
*/
else
{
Path newPath = this.trip.get(nodes.get(index));
while(newPath != null){
if(newPath.destinationTown.equals(nodes.get(index + 1))){
distance = distance + newPath.weightOfPath;
depth = depth + 1;
break;
}
//Continue with the next path.
newPath = newPath.nextPath;
}
}
//increase index after each turn of the loop
index++;
}
return distance;
}
}
所以我寫了我的測試類作爲
import org.junit.Test;
import java.util.*;
public class DistanceOfRouteTest {
Town A,B,C,D,E;
Path path1;
@org.junit.Before
public void setUp() throws Exception {
A = new Town("A");
B = new Town("B");
C = new Town("C");
D = new Town("D");
E = new Town("E");
path1 = new Path();
path1.trip.put(A, new Path(A, B, 5).nextPath(new Path(A, D, 5).nextPath(new Path(A, E, 7))));
path1.trip.put(B, new Path(B, C, 4));
path1.trip.put(C, new Path(C, D, 8).nextPath(new Path(C, E, 2)));
path1.trip.put(D, new Path(D, C, 8).nextPath(new Path(D, E, 6)));
path1.trip.put(E, new Path(E, B, 3));
}
@Test
public void testDistanceOfDirectRoutes_ABC() throws Exception {
ArrayList<Town> distance = new ArrayList<Town>();
distance.add(A);
distance.add(B);
distance.add(C);
assertEquals("ABC route have a length of 9. ", 9, path1.distanceOfDirectRoutes(distance));
}
@Test
public void testDistanceOfDirectRoutes_AD() throws Exception {
ArrayList<Town> distance = new ArrayList<Town>();
distance.add(A);
distance.add(D);
assertEquals("AD route have a length of 5 ", 5, path1.distanceOfDirectRoutes(distance));
}
@Test
public void testDistanceOfDirectRoutes_ADC() throws Exception {
ArrayList<Town> distance = new ArrayList<Town>();
distance.add(A);
distance.add(C);
assertEquals("ADC route have a length of 13 ", 13, path1.distanceOfDirectRoutes(distance));
}
@Test
public void testDistanceOfDirectRoutes_AEBCD() throws Exception {
ArrayList<Town> distance = new ArrayList<Town>();
distance.add(A);
distance.add(E);
distance.add(B);
distance.add(C);
distance.add(D);
assertEquals(" AEBCD route have a length of 22", 22, path1.distanceOfDirectRoutes(distance));
}
@Test
public void testDistanceOfDirectRoutes_AED() throws Exception {
ArrayList<Town> distance = new ArrayList<Town>();
distance.add(A);
distance.add(E);
distance.add(D);
assertEquals("NO SUCH ROUTE EXISTS ", -1, path1.distanceOfDirectRoutes(distance));
}
}
的Town.java類
/*
Definition of Town.java class
Node of the graph is considered as a Town
*/
public class Town {
//variables
public String name;
public boolean isVisited;
//default constructor
public Town(){
name = "no name yet.";
isVisited = false;
}
//constructor
public Town(String name){
this.name = name;
this.isVisited = false;
}
//Accessor and mutuator methods
public String getName(){
return name;
}
public boolean isVisited(){
return isVisited;
}
public void setName(String name){
this.name = name;
}
public void setVisited(boolean isVisited){
this.isVisited = isVisited;
}
}
Path.java類
/*
Definition of Path.java class
Edge of the graph is considered as a Path between two towns.
*/
public class Path {
//Variables
public Town startingTown;
public Town destinationTown;
public int weightOfPath;
public Path nextPath;
public Path(){
startingTown = null;
destinationTown = null;
weightOfPath = 0;
nextPath = null;
}
public Path(Town startingTown, Town destinationTown, int weightOfPath){
this.startingTown = startingTown;
this.destinationTown = destinationTown;
this.weightOfPath = weightOfPath;
this.nextPath = null;
}
public Path nextPath(Path nextPath) {
this.nextPath = nextPath;
return this;
}
public Town getStartingTown(){
return startingTown;
}
public Town getDestinationTown(){
return destinationTown;
}
public int getWeightOfPath(){
return weightOfPath;
}
public Path getNextPath(){
return nextPath;
}
public void setStartingTown(Town startingTown) {
this.startingTown = startingTown;
}
public void setDestinationTown(Town setDestinationTown){
this.destinationTown = destinationTown;
}
public void setWeightOfPath(int weightOfPath){
this.weightOfPath = weightOfPath;
}
public void setNextPath(Path nextPath){
this.nextPath = nextPath;
}
}
該測試給出了說'不能解析符號junit','無法解析符號行程','無法解析符號路徑1'的不同錯誤。 我該如何解決這個問題?
你使用maven來構建它嗎?你的班級路線中有Junit嗎? – acanby 2015-03-02 06:43:05
是的,我使用maven來構建這個.Junit包含在classpath中。 – oddly 2015-03-02 06:52:13