jeudi 31 décembre 2015

Mock/Stub a RuntimeException in unit test

In School class, I have a start() function which invokes another function doTask():

pubic class School {
    public void start() {
       try {
         doTask();
       } catch(RuntimeException e) {
          handleException();
       }
    }
    private void doTask() {
      //Code which might throw RuntimeException
    }
}

I want to unit test start() with RuntimeException:

@Test
public void testStartWithException() {
  // How can I mock/stub mySchool.start() to throw RuntimeException?
  mySchool.start();
}

It is not easy for my implementation code to throw the RuntimeException, how can I make the test code to mock a RuntimeException & throw it?

(Besides pure JUnit, I am considering using Mockito, but not sure how to throw RuntimeException)

Aucun commentaire:

Enregistrer un commentaire