I'm using the maven-surefire-plugin
with junit
4.1.4. I have a unit test which relies on a 3rd party class that internally uses static { ... }
code block to initiate some variables. For one test, I need to change one of these variables, but only for certain tests. I'd like this block to be re-executed between tests, since it picks up a value the first time it runs.
When testing, it seems like surefire instantiates the test class once, so the static { ... }
code block is never processed again.
This means my unit tests that change values required for testing are ignored, the static class has already been instantiated.
π Note: The static class uses System.loadLibrary(...)
, from what I've found, it can't be rewritten to be instantiated, static
is the (rare, but) proper usage.
I found a similar solution for Spring Framework which uses @DirtiesContext(...)
annotation, allowing the programmer to mark classes or methods as "Dirty" so that a new class (or in many cases, the JVM) is initialized between tests.
How do you do the same thing as @DirtiesContext(...)
, but with maven-surefire-plugin
?
public class MyTests {
@Test
public void test1() {
assertThat(MyClass.THE_VALUE, is("something-default"));
}
@Test
public void test2() {
System.setProperty("foo.bar", "something-else");
assertThat(MyClass.THE_VALUE, is("something-else"));
// ^-- this assert fails
// value still "something-default"
}
}
public class MyClass {
static {
String value;
if(System.getProperty("foo.bar") != null) {
value = System.getProperty("foo.bar"); // set to "something-else"
} else {
value = "something-default";
}
}
public static String THE_VALUE = value;
}
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>4.1.2</version>
</plugin>
surefire-maven-plugin
versusJUnit
, I'm mostly just using@Test
annotation for these, so I'm still unsure how to fix the static class problem. I've also added a pseudocode snippet. β EastwardreuseForks
set tofalse
seems to be broken: issues.apache.org/jira/browse/SUREFIRE-1534. Edit: Bumping to Surefire 3.0.0-M4 fixes this. β EastwardreuseForks
false doesn't fix this, since it's the same test class, it still fails. Is there another way? β Eastward<reuseForks>
's "If set to "false", a new VM is forked for each test class to be executed." applies? β Pasleystatic
initializer, but I've seen enough projects to leverage them that I don't see this problem going away. β EastwardSystem.loadLibrary(...)
so thestatic
invocation is static to the JVM regardless of the class design. I'll move the test to another class for now, unless there are some other ideas. β Eastward@Test
reusability for each class, which is especially grotesque (or more accurately, inflated) in scenarios where tests are otherwise identical. I chose the word "overkill" as a short way to describe the impact of this design. I think it fairly accurately represents how onlooking developers would feel if put the same situation. β EastwardSystem.loadLibrary(...)
to explain whystatic
is (rarely but in this case correctly) being used. β Eastward