powermock: ExpectedException.expectMessage AssertionError lost with PowerMockRunner

From dietrich…@gmail.com on March 09, 2012 07:58:14

What steps will reproduce the problem? Run this test class:

@RunWith(PowerMockRunner.class)
public class ExpectedExceptionTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testExpectedException()  {
    thrown.expect(IllegalStateException.class);
    thrown.expectMessage("expected message");

    throw new IllegalStateException("surprise message");
}
}

What is the expected output? What do you see instead? Expected output:


java.lang.AssertionError: 
Expected: (exception with message a string containing "expected message" and an instance of java.lang.IllegalStateException)
     got: <java.lang.IllegalStateException: surprise message>

Instead I see:
java.lang.IllegalStateException: surprise message

Without the PowerMockRunner the expected AssertionError is shown in the output. What version of the product are you using? On what operating system? 1.4.11

_Original issue: http://code.google.com/p/powermock/issues/detail?id=376_

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Comments: 19 (5 by maintainers)

Commits related to this issue

Most upvoted comments

From ronald.b…@gmail.com on September 26, 2013 13:02:45

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);
    }
}


}