JUnit 初始化错误 - 静态方法
JUnit initializationError - static method
我的教授让我们对所有静态方法使用实用程序 class。当我尝试在 JUnit 测试中测试方法时,出现初始化错误。我在下面包含了代码和错误代码的照片以及我认为是构建路径的内容。我包含照片的原因是为了显示构建路径,以防它是导致问题的原因。
从图片中的代码可以看出,我实际上还没有进行任何测试。
谁能帮我查明错误并让我知道如何在 JUnit 中测试实用程序 class 的静态方法?
谢谢。
public class MorseCodeTest {
@Test
public static void testGetEncodingMap() {
//
Map<Character, String> map = new HashMap<Character, String>();
map = MorseCode.getEncodingMap();
for (Map.Entry<Character, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " | " + entry.getValue());
}
}
/**
*
* @return the mapping of encodings from each character to its morse code representation.
*/
public static Map<Character, String> getEncodingMap(){
Map<Character,String> tmpMap = new HashMap<Character,String>();
Set<Map.Entry<Character,String>> mapValues = encodeMappings.entrySet(); // this is the Entry interface inside of the Map interface
//the entrySet() method returns the set of entries aka the set of all key-value pairs
//deep copy encodeTree
for(Map.Entry<Character,String> entry : mapValues){
tmpMap.put(entry.getKey(), entry.getValue());
} //end of enhanced for-loop
return tmpMap;
} //end of getEncodingMap method
为了测试静态 getEncodingMap,您的测试方法不必是静态的。测试方法是它自己的方法,与 getEncodingMap 是静态的事实没有任何关系。
@Test
public void testGetEncodingMap() {
*Your code here*
}
我的教授让我们对所有静态方法使用实用程序 class。当我尝试在 JUnit 测试中测试方法时,出现初始化错误。我在下面包含了代码和错误代码的照片以及我认为是构建路径的内容。我包含照片的原因是为了显示构建路径,以防它是导致问题的原因。 从图片中的代码可以看出,我实际上还没有进行任何测试。
谁能帮我查明错误并让我知道如何在 JUnit 中测试实用程序 class 的静态方法?
谢谢。
public class MorseCodeTest {
@Test
public static void testGetEncodingMap() {
//
Map<Character, String> map = new HashMap<Character, String>();
map = MorseCode.getEncodingMap();
for (Map.Entry<Character, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " | " + entry.getValue());
}
}
/**
*
* @return the mapping of encodings from each character to its morse code representation.
*/
public static Map<Character, String> getEncodingMap(){
Map<Character,String> tmpMap = new HashMap<Character,String>();
Set<Map.Entry<Character,String>> mapValues = encodeMappings.entrySet(); // this is the Entry interface inside of the Map interface
//the entrySet() method returns the set of entries aka the set of all key-value pairs
//deep copy encodeTree
for(Map.Entry<Character,String> entry : mapValues){
tmpMap.put(entry.getKey(), entry.getValue());
} //end of enhanced for-loop
return tmpMap;
} //end of getEncodingMap method
为了测试静态 getEncodingMap,您的测试方法不必是静态的。测试方法是它自己的方法,与 getEncodingMap 是静态的事实没有任何关系。
@Test
public void testGetEncodingMap() {
*Your code here*
}