3

My JUnit test does not catch the exception because of my return statement in the catch block. When I delete returning statement, the test passes. I want my unit test to work with returning statement if the exception occurs.

I've also tried JUnit 5 stuff but it does not fix the problem.

My method:

public ArrayList<Action> parsePlaByPlayTable() { ArrayList<Action> actions = new ArrayList<>(); Document document = null; try { document = Jsoup.connect(url).get(); } catch (Exception e) { log.error(e.getMessage()); return new ArrayList<>(); } // if the exception occurs and there is no return in the catch block, // nullPointerException is thrown here Element table = document.getElementById("pbp"); // more code. . . } 

My test:

 @Test(expected = Exception.class) public void testParsePlaByPlayTableInvalidUrl() { PlayByPlayActionHtmlParser parser = new PlayByPlayActionHtmlParser("https://www.basketbal-reference.com/oxscores/pbp/201905160GS.html"); ArrayList<Action> actions = parser.parsePlaByPlayTable(); } 
1
  • 3
    You are catching the Exception and returning a new ArrayList<>() in the catch block. That means, you cannot assert any Exception. Commented May 28, 2019 at 7:51

2 Answers 2

4

Because you're swallowing the exception in your catch block and returning an empty list. The only way to check if the exception occured is to assert that the returned list is empty.

@Test public void testParsePlaByPlayTableInvalidUrl() { PlayByPlayActionHtmlParser parser = new PlayByPlayActionHtmlParser("https://www.basketbal-reference.com/oxscores/pbp/201905160GS.html"); ArrayList<Action> actions = parser.parsePlaByPlayTable(); Assert.assertTrue(actions.isEmpty()); } 

You also need to remove (expected = Exception.class) from your @Test annotation. Because an exception will never be thrown.

Sign up to request clarification or add additional context in comments.

3 Comments

The only way to check if the exception occured is to assert that the returned list is empty : I disagree, you can use the annotation the OP posted, the solution you posted is one of the possibles, not the only one :) ... see my answer , +1 tho
@Leviand I disagree with your disagreement :P I does not seem like a good idea to change the production code (to throw any exception and don't log anything), so that your tests don't fail
Ofc if that's a production code you're right, but if the question is about "why my annotation is not working", then that's not the only one way. Ty for clarification btw!
0

You are catching the exception with try-catch block, so that throw will never reach the test method: all you need to do is just remove that try catch:

public ArrayList<Action> parsePlaByPlayTable() { //... document = Jsoup.connect(url).get(); //... } 

then your test will run fine, since @Test(expected = Exception.class) will catch your exception, succeeding your test

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.