为什么公共静态AtomicBoolean变量在Maven测试期间有时是假的?

时间:2017-09-07 15:35:23

标签: java multithreading unit-testing testing atomic

为什么公共静态AtomicBoolean变量在Maven测试期间有时会出错?

也许,粗鲁的并发错误?

但在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.");
        }
    }//..
 }
}

1 个答案:

答案 0 :(得分:1)

没有注意到

                <parallel>methods</parallel>

在我的maven-surefire-plugin中。