dimanche 30 décembre 2018

How to test function which calls another void function from it with static value as argument using mockito

I have a class A as

Class A{
 private static final String ANON_DIR             = "/webapps/worldlingo/data/anonymizer/";
 private static final String NO_ANON             = "noanonymize";

  public String first(String text, String srclang, Map dictTokens) {
      Set<String> noAnonymize = new HashSet<String>();
      second(noAnonymize,ANON_DIR + NO_ANON, "tmpLang","name");

      String value;
      if(noAnonymize.contains("test")){
      value = word;
      }
      else {
         value = "test";
        }

    return value;
}

where ANON_DIR and NO_ANON is static final value. This class has function first and function second .The first function has a calling method in it which calls second function. The second function is void function which takes static fields as parameter.

Second function is just the file read function with the path provided as

private void second (Set<String> hashSet, String path, String lang , String type) {
    FileReader fr = null;
    BufferedReader br = null;

    try {
      fr = new FileReader(path);
      br = new BufferedReader(fr);
      String Line;
      while ((Line = br.readLine()) != null) {
        hashSet.add(Line);
      }
    } catch (IOException e) {
      log.error("Anonymizer: Unable to load file.", e);

    } finally {
      try {
        if (fr != null) {
          fr.close();
        }
        if (br != null) {
          br.close();
        }
      } catch (IOException e) {
        log.error("Anonymizer : An error occured while closing a resource.", e);
      }
    }
  }

  } 

Now I am trying to test the function first using mockito. I am trying to change the static parameter and send those changed static parameter as

public void testfirst() throws Exception {
    A anon = new A();


    Field ANON_DIR = A.class.getDeclaredField("ANON_DIR");
    ANON_DIR.setAccessible(true);

    //java relection to change private static final field
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(ANON_DIR, ANON_DIR.getModifiers() & ~Modifier.FINAL);
    ANON_DIR.set(null,"test");

    Field NO_ANON = A.class.getDeclaredField("NO_ANON");
    NO_ANON.setAccessible(true);

    Field modifiersField1 = Field.class.getDeclaredField("modifiers");
    modifiersField1.setAccessible(true);
    modifiersField1.setInt(NO_ANON, NO_ANON.getModifiers() & ~Modifier.FINAL);
    NO_ANON.set(null,"/noanonymize");


    Method anonymizeNames = anon.getClass().getDeclaredMethod("first", String.class, String.class , Map.class);
    String srcLang = "MSFT_EN";
    Map mapTokens = new HashMap();
    String result = (String) anonymizeNames.invoke(anon,"I am David",srcLang,mapTokens);


  }

PROBLEM: Here I could change the private final static field ANON_DIR and NO_ANON using java reflection but the changed field are not send to the second function. The second function called from first function takes the original value instead of changed value. i.e when the second is called ANON_DIR has "/webapps/worldlingo/data/anonymizer/" value instead of "test".

Since I changed the value of ANON_DIR to "test" I want the same "test" value to be passed to the second function. How can I acheive this for testing for void second method.





Aucun commentaire:

Enregistrer un commentaire