Search code examples
javajunit4

JUnit initializationError - static method


My professor made us use a utility class with all static methods. When I try to test the methods in a JUnit test, I get an initialization error. I have included code AND photos of the code below of the error and what I believe to be the build path. The reason I included photos was to show the build path in case it was what was causing the problem. As you can see from the code in the pictures, I have not actually done any tests yet.

Can someone help me pinpoint the error and let me know how to test static methods of a utility class in JUnit?

Thank you.

public class MorseCodeTest {

@Test
public static void testGetEncodingMap() {

    //https://stackoverflow.com/questions/1293337/how-can-i-test-final-and-static-methods-of-a-utility-project

    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

Solution

  • Your test method does not have to be static in order to test the static getEncodingMap. The test method is it's own method and does not have anything to do with the fact that getEncodingMap is static.

    @Test public void testGetEncodingMap() { *Your code here* }