Automatically refactor some exception testing to use new JUnit rules

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=176550995
This commit is contained in:
mcilwain 2017-11-21 13:15:12 -08:00 committed by jianglai
parent f041b1bac0
commit c7484b25e0
13 changed files with 199 additions and 194 deletions

View file

@ -15,23 +15,19 @@
package google.registry.ui.server;
import static com.google.common.truth.Truth8.assertThat;
import static google.registry.testing.JUnitBackports.expectThrows;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import google.registry.testing.CertificateSamples;
import google.registry.ui.forms.FormFieldException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link RegistrarFormFields}. */
@RunWith(JUnit4.class)
public class RegistrarFormFieldsTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Test
public void testValidCertificate_doesntThrowError() {
assertThat(RegistrarFormFields.CLIENT_CERTIFICATE_FIELD.convert(CertificateSamples.SAMPLE_CERT))
@ -40,10 +36,15 @@ public class RegistrarFormFieldsTest {
@Test
public void testBadCertificate_throwsFfe() {
thrown.expect(equalTo(
new FormFieldException("Invalid X.509 PEM certificate")
.propagate("clientCertificate")));
RegistrarFormFields.CLIENT_CERTIFICATE_FIELD.convert("palfun");
FormFieldException thrown =
expectThrows(
FormFieldException.class,
() -> RegistrarFormFields.CLIENT_CERTIFICATE_FIELD.convert("palfun"));
assertThat(
thrown,
equalTo(
new FormFieldException("Invalid X.509 PEM certificate")
.propagate("clientCertificate")));
}
@Test
@ -56,9 +57,14 @@ public class RegistrarFormFieldsTest {
@Test
public void testBadCertificateHash_throwsFfe() {
thrown.expect(equalTo(
new FormFieldException("Field must contain a base64 value.")
.propagate("clientCertificateHash")));
RegistrarFormFields.CLIENT_CERTIFICATE_HASH_FIELD.convert("~~~");
FormFieldException thrown =
expectThrows(
FormFieldException.class,
() -> RegistrarFormFields.CLIENT_CERTIFICATE_HASH_FIELD.convert("~~~"));
assertThat(
thrown,
equalTo(
new FormFieldException("Field must contain a base64 value.")
.propagate("clientCertificateHash")));
}
}

View file

@ -16,11 +16,11 @@ package google.registry.ui.server.registrar;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.DatastoreHelper.loadRegistrar;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.testing.TaskQueueHelper.assertNoTasksEnqueued;
import static google.registry.testing.TaskQueueHelper.assertTasksEnqueued;
import static google.registry.util.ResourceUtils.readResourceUtf8;
import static java.util.Arrays.asList;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.never;
@ -73,12 +73,8 @@ public class RegistrarSettingsActionTest extends RegistrarSettingsActionTestCase
when(sessionUtils.getRegistrarForAuthResult(
any(HttpServletRequest.class), any(AuthResult.class)))
.thenThrow(new ForbiddenException("Not authorized to access Registrar Console"));
try {
action.handleJsonRequest(ImmutableMap.of());
fail("expected ForbiddenException");
} catch (ForbiddenException ex) {
assertNoTasksEnqueued("sheet");
}
assertThrows(ForbiddenException.class, () -> action.handleJsonRequest(ImmutableMap.of()));
assertNoTasksEnqueued("sheet");
}
/**