-2

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
Boognish
  • 103
  • 1
  • 2
  • 13
  • Hey there. Please incluse your code within your questions instead of images please, this will be easier for us to help you – DamCx Apr 30 '18 at 13:15
  • Take moment to go through this https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question and edit the question. – VirtuosoMessi Apr 30 '18 at 13:17
  • I added the code. The reason I used pics was to show the build path in case that is what was causing the error. – Boognish Apr 30 '18 at 13:19

1 Answers1

1

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* }

Vadim Cote
  • 188
  • 2
  • 10