java - mockito test void Method, override member variable -


i'm new unit-testing , mockito need test class:

public class xmlhandler {     private static final string config_file = "path/to/xml";     private xmlconfiguration config = null;      public xmlhandler() {         try {             config = new xmlconfiguration(config_file);             config.setvalidating(true);         } catch (configurationexception e) {             logger.log(level.severe, e );         }     }      public list<configentries> getentries() {              // stuff         return list;     }      @override     public void removeentry(int index) {         // remove entry     } } 

i think have override config variable mockup don't have setter, how this? , removeentry? how test void method?

i hope can me this

as allowed modify class, suggest following structure:

public class xmlhandler {   private final xmlconfiguration config;    public xmlhandler(xmlconfiguration config) {     this.config = config;   }    public list<configentries> getentries() {          // stuff     return list;   }    @override   public void removeentry(int index) {     // remove entry   } } 

make sure xmlconfiguration interface, not concrete implementation. can mock config parameter passed constructor. (note: can mock non-final concrete class too, using interface preferred.)

you can test public methods of xmlhandler , confirm correct behaviour inspecting calls made config , asserting method responses correct.

void methods can tested without problem. need way of determining state of object , world has been adjusted corrected. so, perhaps need call getter method @ end of test ensure values have changed. or verify expected calls made mock object.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -