mardi 23 juillet 2019

Stub only one private static method in a class

I have a class that I am unit testing. It looks like following:

 public class classToUT {
         public static Event getEvent(String id) {
                return getEvent(id, null);
         }
         private static Event getEvent(String id, String name) {
              //do something
              logEvent(id, name);
              return event;
         }
         private static void logEvent(String id, string name) {
             // do some logging
         }
    }

There are external util methods being called in logEvent that I want to avoid. Basically, I want only logEvent to be stubbed out but all other methods to be called in my unit test. How do I stub out this one method only?

public void UTClass {
    @Test
    public void testGetEvent() {
         assertNotNull(event, classToUt.getEvent(1)); //should not call logEvent 
        //but call real method for getEvent(1) and getEvent(1, null)  
    }
}

Aucun commentaire:

Enregistrer un commentaire