jeudi 18 janvier 2018

How to test the code with rethrowing exceptions?

I have a piece of code in my android app that catches one exception and rethrows it as another, custom exception. And it seems impossible to be tested.

Take a look at the following code:

 try { ... } catch (MalformedURLException e) {
     throw new CustomMalformedDataException(downloadUrlString, e);
 }

When I try to test the exception case, I write:

@Test(expected = CustomMalformedDataException.class)
public void exceptionsTest() throws Exception {
    <code triggering the exception>
}

But i get Test running failed: Instrumentation run failed due to 'java.net.MalformedURLException'

When I write:

@Test(expected = MalformedURLException.class)
public void exceptionsTest() throws Exception {
    <code triggering the exception>
}

I get java.lang.Exception: Unexpected exception, expected<java.net.MalformedURLException> but was<CostomMalformedDataException>

So how should I test this case?

Aucun commentaire:

Enregistrer un commentaire