2015-03-02 124 views
0

我正在使用Intellij Idea 14CE。我想测试我的课DistanceOfRouteJUnit 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'的不同错误。 我该如何解决这个问题?

+0

你使用maven来构建它吗?你的班级路线中有Junit吗? – acanby 2015-03-02 06:43:05

+0

是的,我使用maven来构建这个.Junit包含在classpath中。 – oddly 2015-03-02 06:52:13

回答

0

@oddly我在代码中只看到一个重大错误,即Path类没有任何名为'trip'的方法/ var。 'trip'是你在类'DistanceOfRoute'&中定义的HashTable,你试图通过Path实例访问。

没有发现与JUnit相关的错误。我假设你已经导入'org.junit.Assert。*'断言

0

尝试使您的全局变量:

Town A, B, C, D, E; 
Path path1; 
@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)); 
} 
//tests here 

也相当然后做你可以使用完整的注释声明,@Test@Before

+0

非常感谢..我做了你所说的修改。但它仍然说在导入语句中'无法解析符号junit'。每个旅行和DistanceOfRoute条款都存在问题。 – oddly 2015-03-02 06:32:20

+0

您可能没有类路径上的JUnit依赖关系?不知道还有什么可能是 – Brandon 2015-03-02 06:44:34

+0

@blm我会走得更远,并制定A,B,C和D专用最终字段,并将它们初始化为它们定义的位置。将字段命名为a,b,c和d也是更好的方式。 – NamshubWriter 2015-03-02 07:13:39

1

如果您正在使用Maven,确保您有依赖junit指定,并在正确的范围内:

<dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit</artifactId> 
    <version>3.8.1</version> 
    <scope>test</scope> 
</dependency> 

如果您不使用Maven,请确保您没有当你运行测试时,他会在你的类路径上的某个地方使用jar。