gberes
gberes

Reputation: 1169

Junit expected exception fails

I'm trying to test a load method, which throws a StreamCorruptedException. But when i'm testing it with junit 4, the test fails.

My load method:

 public BookDataProvider(String filename) throws StreamCorruptedException { ... }

My test class:

public class TestClass {
   @Test (expected=StreamCorruptedException.class)
   public void wrongFileTest() throws StreamCorruptedException  {
       BookDataProvider bdp = new BookDataProvider("wrong filename");
   }
}

The method throws the exception, but the test fails. What did i do wrong?

Upvotes: 0

Views: 1101

Answers (1)

Prasanna
Prasanna

Reputation: 3781

From your code, I see that your test is passing a wrong file name to BookDataProvider constructor and this does not cause StreamCorruptedException, it will only cause IOException. StreamCorruptedException occurs due to failure of deserialisation of data mostly due to difference in stream used for writing and reading. For example, it happens when trying to read data using ObjectInputStream if it was not written using ObjectOutputStream.

Upvotes: 1

Related Questions