Java 为什么公共静态AtomicBoolean变量有时在Maven中测试时为false?

Java 为什么公共静态AtomicBoolean变量有时在Maven中测试时为false?,java,multithreading,unit-testing,testing,atomic,Java,Multithreading,Unit Testing,Testing,Atomic,为什么公共静态AtomicBoolean变量有时在Maven中测试时为false 也许,粗鲁的并发错误 但在Intellij中效果很好 public class MaintenanceFilterTest { @Rule public ExpectedException expectedException = ExpectedException.none(); private MaintenanceFilter target; @Before public void setUp() throw

为什么公共静态AtomicBoolean变量有时在Maven中测试时为false

也许,粗鲁的并发错误

但在Intellij中效果很好

public class MaintenanceFilterTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
private MaintenanceFilter target;

@Before
public void setUp() throws Exception {
    target = new MaintenanceFilter();
    MaintenanceFilter.isMaintenanceStarted.set(false);
}

@Test
public void doFilterException() throws Exception {
    MaintenanceFilter.isMaintenanceStarted.set(true);
    expectedException.expect(MaintenanceException.class);
    expectedException.expectMessage
        ("Redeployment of application or restart of server is in progress.");

    target.doFilter(null, null, null);
}

public static class MaintenanceFilter implements Filter {
    public static final AtomicBoolean isMaintenanceStarted = new AtomicBoolean();

    @Override
    public void doFilter(
        ServletRequest input, 
        ServletResponse output, 
        FilterChain chain
    ) throws IOException,
            ServletException {
        if (isMaintenanceStarted.get()) {
            throw new MaintenanceException
                ("Redeployment of application or restart of server is in progress.");
        }
    }//..
 }
}
没有注意到

                <parallel>methods</parallel>
方法

在我的maven surefire插件中。

布尔值的默认值始终为
false
。。。您在Intellij中得到的值是多少?我正在了解。为什么要将
isMaintenance Start
设置为静态,并在单独测试之前和期间更改其值?Maven可以在单独的线程中运行您的测试,并且可以执行另一个
setUp()
,当另一个线程正在执行您的
target.doFilter(null,null,null)时,该线程将
isMaintenance Started
设置为false方法。@tsolakp如何解决这个问题?@Arthur Kharkivskiy只需将其作为实例变量,也不需要使用
AtomicBoolean