Hi all,
I'm trying to set a private static final
field in a class with private
constructor for a JUnit test. When I boil the code down to its basics, I get the following:
public class Foo {
private static final boolean FLAG = false;
private Foo() { /* don't call me */ }
public static boolean get() { return FLAG; }
}
My tests looks like this:
@RunWith(PowerMockRunner.class)
@PrepareEverythingForTest // Whitebox fails without this line
public class FooTest {
@Test
public void testGet() {
Whitebox.setInternalState(Foo.class, "FLAG", true);
assertTrue(Foo.get());
}
}
And here is an excerpt from my POM file:
<junit.version>4.11</junit.version>
<powermock.version>1.5.4</powermock.version>
<mockito.version>1.9.5</mockito.version>
When I put a breakpoint on return FLAG;
, I can clearly see that FLAG
was set to true
in IntelliJ's debugger. Yet the test fails with an AssertionError
.
Any ideas what to do to make this work?
Kind regards,
Christian
P.S.: This is a cross-post from StackOverflow: http://stackoverflow.com/q/33281347/2018047
--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.
@RunWith(PowerMockRunner.class)
@PrepareEverythingForTest // including foo.Foo, the class under test
@SuppressStaticInitializationFor("Foo") // foo.Foo is in the default package
public class FooTest {
@Test
public void testGet() {
Whitebox.setInternalState(Foo.class, "FLAG", true);
assertTrue(Foo.get());
}
}