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

@ -15,6 +15,8 @@
package google.registry.tools.params;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import com.google.common.collect.ImmutableMap;
import google.registry.tools.params.KeyValueMapParameter.CurrencyUnitToStringMap;
@ -22,19 +24,13 @@ import google.registry.tools.params.KeyValueMapParameter.StringToIntegerMap;
import google.registry.tools.params.KeyValueMapParameter.StringToStringMap;
import org.joda.money.CurrencyUnit;
import org.joda.money.IllegalCurrencyException;
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 KeyValueMapParameter}. */
@RunWith(JUnit4.class)
public class KeyValueMapParameterTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
private final StringToStringMap stringToStringInstance = new StringToStringMap();
private final StringToIntegerMap stringToIntegerInstance = new StringToIntegerMap();
private final CurrencyUnitToStringMap currencyUnitToStringMap = new CurrencyUnitToStringMap();
@ -92,51 +88,51 @@ public class KeyValueMapParameterTest {
@Test
public void testFailure_convertStringToInteger_badType() throws Exception {
thrown.expect(NumberFormatException.class);
stringToIntegerInstance.convert("key=1,key2=foo");
assertThrows(
NumberFormatException.class, () -> stringToIntegerInstance.convert("key=1,key2=foo"));
}
@Test
public void testFailure_convertCurrencyUnitToString_badType() throws Exception {
thrown.expect(IllegalCurrencyException.class);
thrown.expectMessage("XYZ");
currencyUnitToStringMap.convert("USD=123abc,XYZ=xyz789");
IllegalCurrencyException thrown =
expectThrows(
IllegalCurrencyException.class,
() -> currencyUnitToStringMap.convert("USD=123abc,XYZ=xyz789"));
assertThat(thrown).hasMessageThat().contains("XYZ");
}
@Test
public void testFailure_convertStringToString_badSeparator() throws Exception {
thrown.expect(IllegalArgumentException.class);
stringToStringInstance.convert("key=foo&key2=bar");
assertThrows(
IllegalArgumentException.class, () -> stringToStringInstance.convert("key=foo&key2=bar"));
}
@Test
public void testFailure_convertStringToInteger_badSeparator() throws Exception {
thrown.expect(IllegalArgumentException.class);
stringToIntegerInstance.convert("key=1&key2=2");
assertThrows(
IllegalArgumentException.class, () -> stringToIntegerInstance.convert("key=1&key2=2"));
}
@Test
public void testFailure_convertCurrencyUnitToString_badSeparator() throws Exception {
thrown.expect(IllegalArgumentException.class);
currencyUnitToStringMap.convert("USD=123abc&JPY=xyz789");
assertThrows(
IllegalArgumentException.class,
() -> currencyUnitToStringMap.convert("USD=123abc&JPY=xyz789"));
}
@Test
public void testFailure_convertStringToString_badFormat() throws Exception {
thrown.expect(IllegalArgumentException.class);
stringToStringInstance.convert("foo");
assertThrows(IllegalArgumentException.class, () -> stringToStringInstance.convert("foo"));
}
@Test
public void testFailure_convertStringToInteger_badFormat() throws Exception {
thrown.expect(IllegalArgumentException.class);
stringToIntegerInstance.convert("foo");
assertThrows(IllegalArgumentException.class, () -> stringToIntegerInstance.convert("foo"));
}
@Test
public void testFailure_convertCurrencyUnitToString_badFormat() throws Exception {
thrown.expect(IllegalArgumentException.class);
currencyUnitToStringMap.convert("foo");
assertThrows(IllegalArgumentException.class, () -> currencyUnitToStringMap.convert("foo"));
}
}