samedi 12 septembre 2015

Or I should test all map values on unit testing?

I have class for convert string data to map, and have Junit test for this class. And want to know how I should organize my tests.

Or is good practice to check several items, or should test all map items?

public class Converter {

private static final String ATHLETIC_NAME = "athletic_name";
private static final String RESULT_100M = "result_100m";
private static final String RESULT_LONGJUMP = "result_long_jump";
private static final String RESULT_SHOTPUT = "result_shot_put";
private static final String RESULT_HIGHJUMP = "result_high_jump";
private static final String RESULT_400M = "result_400m";
private static final String RESULT_HURDLES_110M = "result_hurdles_110M";
private static final String RESULT_DISCUS_THROW = "result_discus_throw";
private static final String RESULT_POLE_VOULT = "result_pole_voult";
private static final String RESULT_JAVELIN_THROW = "result_javelin_throw";
private static final String RESULT_1500M = "result_1500m";

private static final String[] FIELDS_LIST = new String[]{
        ATHLETIC_NAME,
        RESULT_100M,
        RESULT_LONGJUMP,
        RESULT_SHOTPUT,
        RESULT_HIGHJUMP,
        RESULT_400M,
        RESULT_HURDLES_110M,
        RESULT_DISCUS_THROW,
        RESULT_POLE_VOULT,
        RESULT_JAVELIN_THROW,
        RESULT_1500M
};

public static Map<String, String> getResultsMap(String values){

    Map<String, String> valuesMap = new HashMap<String, String>();

    try{
        String value = "";

        String[] splitted = values.split(";");

        if (splitted.length != FIELDS_LIST.length){
            throw new Exception("Unmatched arrays size!!");
        }
        int i=0;
        for (String keyItem : FIELDS_LIST){
            valuesMap.put(keyItem, splitted[i++]);
        }
    }
    catch (Exception e){
        e.printStackTrace();
    }

    return valuesMap;
}

}

Class unit test:

public class ConverterTest {
private String testLine = "Siim Susi;12.61;5.00;9.22;1.50;60.39;16.43;21.60;2.60;35.81;5.25.72";
private Map<String, String> athleticResultsData = Converter.getResultsMap(testLine);

@Test
public void testCVSline2map_athletic_name(){
    Assert.assertEquals("Siim Susi",athleticResultsData.get("athletic_name"));
}

@Test
public void testCVSline2map_result_1500m(){
    Assert.assertEquals("5.25.72",athleticResultsData.get("result_1500m"));
}

}

Aucun commentaire:

Enregistrer un commentaire