2015-03-02 170 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。