Fix flaky tests that depends on order of rules

SystemPropertyRule in some cases should be applied last:
when multiple rules exist and and modified property is checked
in cleanups.

ConsoleOteSetupActionTest and ConsoleRegistrarCreatorActionTest
are two such classes, and can be flaky in JUnit 4. This PR
migrates them to JUnit5 and applies ordering to extensions in
them.

Added a mockito dependency, and upgraded mockito-core to 3.3.3.

Meaningful changes: SystemPropertyRule.java and
ConsoleOteSetupActionTest.java, and
ConsoleRegistrarCreatorActionTest.java
This commit is contained in:
Weimin Yu 2020-06-24 23:14:47 -04:00 committed by GitHub
parent a65a3fd8b7
commit 2e5466f32f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 107 additions and 85 deletions

View file

@ -24,10 +24,19 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nullable;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.rules.ExternalResource;
/** JUnit Rule for overriding the values Java system properties during tests. */
public final class SystemPropertyRule extends ExternalResource implements SystemPropertySetter {
/**
* JUnit Rule for overriding the values Java system properties during tests.
*
* <p>In most scenarios this class should be the last rule/extension to apply. In JUnit 5, apply
* {@code @Order(value = Integer.MAX_VALUE)} to the extension.
*/
public final class SystemPropertyRule extends ExternalResource
implements SystemPropertySetter, BeforeEachCallback, AfterEachCallback {
/** Class representing a system property key value pair. */
private static class Property {
@ -90,4 +99,14 @@ public final class SystemPropertyRule extends ExternalResource implements System
original.set();
}
}
@Override
public void beforeEach(ExtensionContext context) {
before();
}
@Override
public void afterEach(ExtensionContext context) {
after();
}
}

View file

@ -48,26 +48,25 @@ import google.registry.util.SendEmailService;
import java.util.Optional;
import javax.mail.internet.InternetAddress;
import javax.servlet.http.HttpServletRequest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.junit.jupiter.MockitoExtension;
@RunWith(JUnit4.class)
/** Unit tests for {@link ConsoleOteSetupAction}. */
@ExtendWith(MockitoExtension.class)
public final class ConsoleOteSetupActionTest {
@Rule
@RegisterExtension
public final AppEngineRule appEngineRule =
AppEngineRule.builder().withDatastoreAndCloudSql().build();
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
@Rule
@RegisterExtension
@Order(value = Integer.MAX_VALUE)
public final SystemPropertyRule systemPropertyRule = new SystemPropertyRule();
private final FakeResponse response = new FakeResponse();
@ -77,7 +76,7 @@ public final class ConsoleOteSetupActionTest {
@Mock HttpServletRequest request;
@Mock SendEmailService emailService;
@Before
@BeforeEach
public void setUp() throws Exception {
persistPremiumList("default_sandbox_list", "sandbox,USD 1000");

View file

@ -50,26 +50,24 @@ import java.util.Optional;
import javax.mail.internet.InternetAddress;
import javax.servlet.http.HttpServletRequest;
import org.joda.money.CurrencyUnit;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.junit.jupiter.MockitoExtension;
@RunWith(JUnit4.class)
@ExtendWith(MockitoExtension.class)
public final class ConsoleRegistrarCreatorActionTest {
@Rule
@RegisterExtension
public final AppEngineRule appEngineRule =
AppEngineRule.builder().withDatastoreAndCloudSql().build();
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
@Rule
@RegisterExtension
@Order(value = Integer.MAX_VALUE)
public final SystemPropertyRule systemPropertyRule = new SystemPropertyRule();
private final FakeResponse response = new FakeResponse();
@ -79,7 +77,7 @@ public final class ConsoleRegistrarCreatorActionTest {
@Mock HttpServletRequest request;
@Mock SendEmailService emailService;
@Before
@BeforeEach
public void setUp() throws Exception {
persistPremiumList("default_sandbox_list", "sandbox,USD 1000");