Automatically refactor more exception testing to use new JUnit rules

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178911894
This commit is contained in:
mcilwain 2017-08-14 09:20:03 -04:00 committed by Ben McIlwain
parent 36ad38e5df
commit 7dc224627f
125 changed files with 1970 additions and 1982 deletions

View file

@ -32,7 +32,6 @@ import google.registry.testing.InjectRule;
import java.util.Optional;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -46,10 +45,6 @@ public class CreateGroupsActionTest {
public final AppEngineRule appEngine = AppEngineRule.builder()
.withDatastore()
.build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
@Rule
public final InjectRule inject = new InjectRule();
@ -67,17 +62,21 @@ public class CreateGroupsActionTest {
@Test
public void test_invalidRequest_missingClientId() throws Exception {
thrown.expect(BadRequestException.class);
thrown.expectMessage("Error creating Google Groups, missing parameter: clientId");
runAction(null);
BadRequestException thrown = expectThrows(BadRequestException.class, () -> runAction(null));
assertThat(thrown)
.hasMessageThat()
.contains("Error creating Google Groups, missing parameter: clientId");
}
@Test
public void test_invalidRequest_invalidClientId() throws Exception {
thrown.expect(BadRequestException.class);
thrown.expectMessage(
"Error creating Google Groups; could not find registrar with id completelyMadeUpClientId");
runAction("completelyMadeUpClientId");
BadRequestException thrown =
expectThrows(BadRequestException.class, () -> runAction("completelyMadeUpClientId"));
assertThat(thrown)
.hasMessageThat()
.contains(
"Error creating Google Groups; "
+ "could not find registrar with id completelyMadeUpClientId");
}
@Test

View file

@ -30,7 +30,6 @@ import org.joda.money.Money;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -41,8 +40,6 @@ import org.junit.runners.JUnit4;
public class CreatePremiumListActionTest {
@Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
@Rule public final ExpectedException thrown = ExpectedException.none();
CreatePremiumListAction action;
FakeJsonResponse response;

View file

@ -18,6 +18,7 @@ import static com.google.appengine.api.datastore.DatastoreServiceFactory.getData
import static com.google.common.truth.Truth.assertThat;
import static com.googlecode.objectify.Key.create;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.JUnitBackports.expectThrows;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.KeyFactory;
@ -28,7 +29,6 @@ import google.registry.testing.FakeResponse;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -38,10 +38,6 @@ public class DeleteEntityActionTest {
@Rule
public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
FakeResponse response = new FakeResponse();
DeleteEntityAction action = new DeleteEntityAction();
@ -89,9 +85,8 @@ public class DeleteEntityActionTest {
Entity entity = new Entity("not", "here");
String rawKey = KeyFactory.keyToString(entity.getKey());
action.rawKeys = rawKey;
thrown.expect(BadRequestException.class);
thrown.expectMessage("Could not find entity with key " + rawKey);
action.run();
BadRequestException thrown = expectThrows(BadRequestException.class, () -> action.run());
assertThat(thrown).hasMessageThat().contains("Could not find entity with key " + rawKey);
}
@Test
@ -102,8 +97,7 @@ public class DeleteEntityActionTest {
Entity entity = new Entity("non", "existent");
String rawKey = KeyFactory.keyToString(entity.getKey());
action.rawKeys = String.format("%s,%s", ofyKey, rawKey);
thrown.expect(BadRequestException.class);
thrown.expectMessage("Could not find entity with key " + rawKey);
action.run();
BadRequestException thrown = expectThrows(BadRequestException.class, () -> action.run());
assertThat(thrown).hasMessageThat().contains("Could not find entity with key " + rawKey);
}
}

View file

@ -23,7 +23,6 @@ import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
/**
* Base class for tests of list actions.
@ -35,9 +34,6 @@ public class ListActionTestCase {
.withDatastore()
.build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
private FakeJsonResponse response;
private void runAction(

View file

@ -29,7 +29,6 @@ import org.joda.money.Money;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -44,9 +43,6 @@ public class UpdatePremiumListActionTest {
.withDatastore()
.build();
@Rule
public final ExpectedException thrown = ExpectedException.none();
UpdatePremiumListAction action;
FakeJsonResponse response;