Open GoogleCodeExporter opened 9 years ago
What version of Junit are you using?
Original comment by johan.ha...@gmail.com
on 9 Mar 2012 at 7:36
I also experienced this when using PowerMockRule and ExpectedException rule.
It seems the problem is the order of the Rule's statements being executed and
the classloading PowerMock does. The exception is thrown OK through to
ExpectedExceptionStatement, but the ExpectedExceptionStatement itself is a
different object than the one that is visible to the test (I suspect due to
classloading).
That means that the "expect" methods in the Test are working with another
Object than the one that does the "handleException", and so there are no
matchers and the whole thing fails.
A workaround is as follows:
1.) create a class PowerMockTestRule which wraps PowerMockRule to make it
implement TestRule:
import java.lang.reflect.Method;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.powermock.modules.junit4.rule.PowerMockRule;
public class PowerMockTestRule implements TestRule {
private final PowerMockRule rule;
public PowerMockTestRule(PowerMockRule rule) {
this.rule = rule;
}
@Override
public Statement apply(Statement base, Description description) {
return rule.apply(base, createFrameworkMethod(description), getTestObject(description));
}
private FrameworkMethod createFrameworkMethod(Description description) {
try {
String methodName = description.getMethodName();
Class c = getTestClass(description);
Method m = c.getDeclaredMethod(methodName);
return new FrameworkMethod(m);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
private Class<?> getTestClass(Description description) {
return description.getTestClass();
}
private Object getTestObject(Description description) {
try {
return getTestClass(description).newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
}
2.) Use RuleChain to define the order of the Rules:
public ExpectedException thrown = ExpectedException.none();
@Rule public TestRule chain = RuleChain
.outerRule(new PowerMockTestRule(new PowerMockRule()))
.around(thrown);
I don't know if this is feasible for a general solution, but in my case, it
works.
PS: Junit used: 4.11, Powermock used: 1.5.1
Original comment by ronald.b...@gmail.com
on 26 Sep 2013 at 4:04
Update: instead of the Powermock specific PowerMockTestRule above i am now
using a more generic Version: TestRuleAdapter:
import java.lang.reflect.Method;
import org.junit.rules.MethodRule;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
public class TestRuleAdapter implements TestRule {
private final MethodRule rule;
public TestRuleAdapter(MethodRule rule) {
this.rule = rule;
}
@Override
public Statement apply(Statement base, Description description) {
return rule.apply(base, createFrameworkMethod(description), getTestObject(description));
}
private FrameworkMethod createFrameworkMethod(Description description) {
try {
String methodName = description.getMethodName();
Class<?> c = getTestClass(description);
Method m = c.getDeclaredMethod(methodName);
return new FrameworkMethod(m);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
private Class<?> getTestClass(Description description) {
return description.getTestClass();
}
private Object getTestObject(Description description) {
try {
return getTestClass(description).newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
}
Original comment by ronald.b...@gmail.com
on 26 Sep 2013 at 8:02
Thanks for sharing this. It wouldn't be possible for you to try to resolve this
issue in the PowerMockRule? I would be really glad if we could get this fixed
to the next release.
Original comment by johan.ha...@gmail.com
on 27 Sep 2013 at 5:16
Is this expected to be fixed anytime soon? I am on JUnit 4.12, PowerMock 1.6.1,
and Mockito 1.10.8 and this still doesn't work. Anytime I use PowerMock on a
test class I have to revert to try/catch to validate exceptions.
Original comment by kyk...@gmail.com
on 6 Feb 2015 at 7:23
This is a serious problem. To fix it, all that needs to be done is for
PowerMockRule to implement TestRule so that it can be chainable with RuleChain.
It is currently not possible to submit issues on Github for this that I could
see.
Original comment by ryan.dan...@gmail.com
on 24 Mar 2015 at 10:03
No issues are currently reported here (but we will move to Github). Please
submit a pull request at Github and I'll include it.
Original comment by johan.ha...@gmail.com
on 26 Mar 2015 at 7:55
Original issue reported on code.google.com by
dietrich...@gmail.com
on 9 Mar 2012 at 6:58