From b81c522ee1624f3d5f170fc3468f4600956950bd Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 10 Jan 2015 17:51:51 +0100 Subject: [PATCH] Handle exceptions properly in SpringJUnit4ClassRunner JUnit 4.9 introduced a regression in BlockJUnit4ClassRunner.runChild() such that exceptions thrown from methodBlock() cause the current test execution to abort immediately. As a result, the failing test method is unrooted, and subsequent test methods are never invoked. Furthermore, RunListeners registered with JUnit are not properly notified. In conjunction with SPR-11908, SpringJUnit4ClassRunner was updated to use the aforementioned changes to BlockJUnit4ClassRunner.runChild(). Consequently, SpringJUnit4ClassRunner now suffers from the same regression. This commit addresses this issue by ensuring that any exceptions thrown during the invocation of methodBlock() are properly wrapped in a JUnit Fail Statement. Issue: SPR-12613 --- .../test/context/junit4/SpringJUnit4ClassRunner.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java b/spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java index 7e70830a98..ed7ce56443 100644 --- a/spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java +++ b/spring-test/src/main/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunner.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -214,7 +214,14 @@ public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner { notifier.fireTestIgnored(description); } else { - runLeaf(methodBlock(frameworkMethod), description, notifier); + Statement statement; + try { + statement = methodBlock(frameworkMethod); + } + catch (Throwable ex) { + statement = new Fail(ex); + } + runLeaf(statement, description, notifier); } }