From 34ee8bece63c0fe3ebbcf7422e351b744624b969 Mon Sep 17 00:00:00 2001 From: Si Jie Date: Fri, 1 Feb 2019 15:07:25 +0800 Subject: [PATCH] Assert: Use JUnit 5 assertions Currently, Assert implements our custom assertThrows using try/catch. This implementation was done when we used JUnit 4 which has no proper support for assertThrows. As we migrate to JUnit 5[1], we can make use of JUnit 5's Assertions.assertThrows. However, our current assertThrows supports checking the correctness of error's message, whcih JUnit 5 does not support. Let's * migrate Assert#assertThrows to use JUnit 5's assertThrows. * Remove VoidCallable interface within Assert * create an overloaded assertThrows(Class, String, Executable) to support checking the correctness of error messages. * standardise to use only seedu.address.testutil.Assert#assertThrows instead of org.junit.jupiter.api.Assertions.assertThrows By creating an overloaded assertThrows method, we can extract the common routine of checking error message into a single utility function within Assert.java. This encourages reusability and follows the DRY principle. In addition, by using org.junit.jupiter.api.function.Executable that already accepts lambda expressions, we no longer need the VoidCallable interface previously. Hence, we can remove the unused VoidCallable interface. [1] https://github.com/se-edu/addressbook-level4/issues/951 --- .../address/commons/core/ConfigTest.java | 4 -- .../address/commons/core/VersionTest.java | 14 +++-- .../address/commons/util/AppUtilTest.java | 21 ++------ .../commons/util/CollectionUtilTest.java | 15 ++---- .../address/commons/util/ConfigUtilTest.java | 33 ++++-------- .../address/commons/util/FileUtilTest.java | 9 ++-- .../address/commons/util/StringUtilTest.java | 41 +++++--------- .../seedu/address/logic/LogicManagerTest.java | 53 +++++++----------- .../logic/commands/AddCommandTest.java | 22 +++----- .../logic/commands/CommandTestUtil.java | 18 +++---- .../address/logic/parser/ParserUtilTest.java | 43 ++++++--------- .../seedu/address/model/AddressBookTest.java | 18 ++----- .../seedu/address/model/ModelManagerTest.java | 23 +++----- .../model/VersionedAddressBookTest.java | 8 +-- .../address/model/person/AddressTest.java | 13 +++-- .../seedu/address/model/person/EmailTest.java | 13 +++-- .../seedu/address/model/person/NameTest.java | 13 +++-- .../address/model/person/PersonTest.java | 8 +-- .../seedu/address/model/person/PhoneTest.java | 13 +++-- .../model/person/UniquePersonListTest.java | 45 ++++++---------- .../java/seedu/address/model/tag/TagTest.java | 9 ++-- .../storage/JsonAdaptedPersonTest.java | 3 +- .../storage/JsonAddressBookStorageTest.java | 30 ++++------- .../JsonSerializableAddressBookTest.java | 16 ++---- .../storage/JsonUserPrefsStorageTest.java | 24 +++------ .../java/seedu/address/testutil/Assert.java | 54 ++++++------------- 26 files changed, 195 insertions(+), 368 deletions(-) diff --git a/src/test/java/seedu/address/commons/core/ConfigTest.java b/src/test/java/seedu/address/commons/core/ConfigTest.java index a9e7ab09648c..8c657ade434f 100644 --- a/src/test/java/seedu/address/commons/core/ConfigTest.java +++ b/src/test/java/seedu/address/commons/core/ConfigTest.java @@ -4,13 +4,9 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; public class ConfigTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); @Test public void toString_defaultObject_stringReturned() { diff --git a/src/test/java/seedu/address/commons/core/VersionTest.java b/src/test/java/seedu/address/commons/core/VersionTest.java index 7601efa25893..9ab5b6a3a03c 100644 --- a/src/test/java/seedu/address/commons/core/VersionTest.java +++ b/src/test/java/seedu/address/commons/core/VersionTest.java @@ -1,15 +1,12 @@ package seedu.address.commons.core; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.Assert.assertThrows; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; public class VersionTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); @Test public void versionParsing_acceptableVersionString_parsedVersionCorrectly() { @@ -20,8 +17,9 @@ public void versionParsing_acceptableVersionString_parsedVersionCorrectly() { @Test public void versionParsing_wrongVersionString_throwIllegalArgumentException() { - thrown.expect(IllegalArgumentException.class); - Version.fromString("This is not a version string"); + assertThrows(IllegalArgumentException.class, () -> + Version.fromString("This is not a version string") + ); } @Test diff --git a/src/test/java/seedu/address/commons/util/AppUtilTest.java b/src/test/java/seedu/address/commons/util/AppUtilTest.java index b81493f4784b..d8497ea5edb6 100644 --- a/src/test/java/seedu/address/commons/util/AppUtilTest.java +++ b/src/test/java/seedu/address/commons/util/AppUtilTest.java @@ -1,28 +1,20 @@ package seedu.address.commons.util; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static seedu.address.testutil.Assert.assertThrows; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; public class AppUtilTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - - - @Test public void getImage_exitingImage() { assertNotNull(AppUtil.getImage("/images/address_book_32.png")); } - @Test public void getImage_nullGiven_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - AppUtil.getImage(null); + assertThrows(NullPointerException.class, () -> AppUtil.getImage(null)); } @Test @@ -33,15 +25,12 @@ public void checkArgument_true_nothingHappens() { @Test public void checkArgument_falseWithoutErrorMessage_throwsIllegalArgumentException() { - thrown.expect(IllegalArgumentException.class); - AppUtil.checkArgument(false); + assertThrows(IllegalArgumentException.class, () -> AppUtil.checkArgument(false)); } @Test public void checkArgument_falseWithErrorMessage_throwsIllegalArgumentException() { String errorMessage = "error message"; - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage(errorMessage); - AppUtil.checkArgument(false, errorMessage); + assertThrows(IllegalArgumentException.class, errorMessage, () -> AppUtil.checkArgument(false, errorMessage)); } } diff --git a/src/test/java/seedu/address/commons/util/CollectionUtilTest.java b/src/test/java/seedu/address/commons/util/CollectionUtilTest.java index 7739103f4f80..50cf815b83d9 100644 --- a/src/test/java/seedu/address/commons/util/CollectionUtilTest.java +++ b/src/test/java/seedu/address/commons/util/CollectionUtilTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; +import static seedu.address.testutil.Assert.assertThrows; import java.util.Arrays; import java.util.Collection; @@ -86,12 +87,7 @@ public void isAnyNonNull() { * if {@code objects} or any element of {@code objects} is null. */ private void assertNullPointerExceptionThrown(Object... objects) { - try { - requireAllNonNull(objects); - throw new AssertionError("The expected NullPointerException was not thrown."); - } catch (NullPointerException npe) { - // expected behavior - } + assertThrows(NullPointerException.class, () -> requireAllNonNull(objects)); } /** @@ -99,12 +95,7 @@ private void assertNullPointerExceptionThrown(Object... objects) { * if {@code collection} or any element of {@code collection} is null. */ private void assertNullPointerExceptionThrown(Collection collection) { - try { - requireAllNonNull(collection); - throw new AssertionError("The expected NullPointerException was not thrown."); - } catch (NullPointerException npe) { - // expected behavior - } + assertThrows(NullPointerException.class, () -> requireAllNonNull(collection)); } private void assertNullPointerExceptionNotThrown(Object... objects) { diff --git a/src/test/java/seedu/address/commons/util/ConfigUtilTest.java b/src/test/java/seedu/address/commons/util/ConfigUtilTest.java index fdd4ff86d09f..8bb0462b5396 100644 --- a/src/test/java/seedu/address/commons/util/ConfigUtilTest.java +++ b/src/test/java/seedu/address/commons/util/ConfigUtilTest.java @@ -1,7 +1,8 @@ package seedu.address.commons.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static seedu.address.testutil.Assert.assertThrows; import java.io.IOException; import java.nio.file.Path; @@ -11,7 +12,6 @@ import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import seedu.address.commons.core.Config; @@ -21,16 +21,13 @@ public class ConfigUtilTest { private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "ConfigUtilTest"); - @Rule - public ExpectedException thrown = ExpectedException.none(); @Rule public TemporaryFolder testFolder = new TemporaryFolder(); @Test - public void read_null_throwsNullPointerException() throws DataConversionException { - thrown.expect(NullPointerException.class); - read(null); + public void read_null_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> read(null)); } @Test @@ -39,14 +36,8 @@ public void read_missingFile_emptyResult() throws DataConversionException { } @Test - public void read_notJsonFormat_exceptionThrown() throws DataConversionException { - - thrown.expect(DataConversionException.class); - read("NotJsonFormatConfig.json"); - - /* IMPORTANT: Any code below an exception-throwing line (like the one above) will be ignored. - * That means you should not have more than one exception test in one method - */ + public void read_notJsonFormat_exceptionThrown() { + assertThrows(DataConversionException.class, () -> read("NotJsonFormatConfig.json")); } @Test @@ -85,15 +76,13 @@ private Optional read(String configFileInTestDataFolder) throws DataConv } @Test - public void save_nullConfig_throwsNullPointerException() throws IOException { - thrown.expect(NullPointerException.class); - save(null, "SomeFile.json"); + public void save_nullConfig_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> save(null, "SomeFile.json")); } @Test - public void save_nullFile_throwsNullPointerException() throws IOException { - thrown.expect(NullPointerException.class); - save(new Config(), null); + public void save_nullFile_throwsNullPointerException() { + assertThrows(NullPointerException.class, () -> save(new Config(), null)); } @Test diff --git a/src/test/java/seedu/address/commons/util/FileUtilTest.java b/src/test/java/seedu/address/commons/util/FileUtilTest.java index 4b30ea91ffcb..025280845850 100644 --- a/src/test/java/seedu/address/commons/util/FileUtilTest.java +++ b/src/test/java/seedu/address/commons/util/FileUtilTest.java @@ -1,12 +1,11 @@ package seedu.address.commons.util; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.Assert.assertThrows; import org.junit.Test; -import seedu.address.testutil.Assert; - public class FileUtilTest { @Test @@ -18,7 +17,7 @@ public void isValidPath() { assertFalse(FileUtil.isValidPath("a\0")); // null path -> throws NullPointerException - Assert.assertThrows(NullPointerException.class, () -> FileUtil.isValidPath(null)); + assertThrows(NullPointerException.class, () -> FileUtil.isValidPath(null)); } } diff --git a/src/test/java/seedu/address/commons/util/StringUtilTest.java b/src/test/java/seedu/address/commons/util/StringUtilTest.java index c0bbca33e188..ac545e09da8b 100644 --- a/src/test/java/seedu/address/commons/util/StringUtilTest.java +++ b/src/test/java/seedu/address/commons/util/StringUtilTest.java @@ -1,22 +1,15 @@ package seedu.address.commons.util; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.Assert.assertThrows; import java.io.FileNotFoundException; -import java.util.Optional; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; public class StringUtilTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - //---------------- Tests for isUnsignedPositiveInteger -------------------------------------- @Test @@ -63,31 +56,25 @@ public void isUnsignedPositiveInteger() { @Test public void containsWordIgnoreCase_nullWord_throwsNullPointerException() { - assertExceptionThrown(NullPointerException.class, "typical sentence", null, Optional.empty()); - } - - private void assertExceptionThrown(Class exceptionClass, String sentence, String word, - Optional errorMessage) { - thrown.expect(exceptionClass); - errorMessage.ifPresent(message -> thrown.expectMessage(message)); - StringUtil.containsWordIgnoreCase(sentence, word); + assertThrows(NullPointerException.class, () + -> StringUtil.containsWordIgnoreCase("typical sentence", null)); } @Test public void containsWordIgnoreCase_emptyWord_throwsIllegalArgumentException() { - assertExceptionThrown(IllegalArgumentException.class, "typical sentence", " ", - Optional.of("Word parameter cannot be empty")); + assertThrows(IllegalArgumentException.class, "Word parameter cannot be empty", () + -> StringUtil.containsWordIgnoreCase("typical scenario", " ")); } @Test public void containsWordIgnoreCase_multipleWords_throwsIllegalArgumentException() { - assertExceptionThrown(IllegalArgumentException.class, "typical sentence", "aaa BBB", - Optional.of("Word parameter should be a single word")); + assertThrows(IllegalArgumentException.class, "Word parameter should be a single word", () + -> StringUtil.containsWordIgnoreCase("typical scenario", "aaa BBB")); } @Test public void containsWordIgnoreCase_nullSentence_throwsNullPointerException() { - assertExceptionThrown(NullPointerException.class, null, "abc", Optional.empty()); + assertThrows(NullPointerException.class, () -> StringUtil.containsWordIgnoreCase(null, "abc")); } /* @@ -145,15 +132,13 @@ public void containsWordIgnoreCase_validInputs_correctResult() { @Test public void getDetails_exceptionGiven() { - assertThat(StringUtil.getDetails(new FileNotFoundException("file not found")), - containsString("java.io.FileNotFoundException: file not found")); + assertTrue(StringUtil.getDetails(new FileNotFoundException("file not found")) + .contains("java.io.FileNotFoundException: file not found")); } @Test public void getDetails_nullGiven_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - StringUtil.getDetails(null); + assertThrows(NullPointerException.class, () -> StringUtil.getDetails(null)); } - } diff --git a/src/test/java/seedu/address/logic/LogicManagerTest.java b/src/test/java/seedu/address/logic/LogicManagerTest.java index 56eb8d633185..7c8068923c19 100644 --- a/src/test/java/seedu/address/logic/LogicManagerTest.java +++ b/src/test/java/seedu/address/logic/LogicManagerTest.java @@ -7,6 +7,7 @@ import static seedu.address.logic.commands.CommandTestUtil.EMAIL_DESC_AMY; import static seedu.address.logic.commands.CommandTestUtil.NAME_DESC_AMY; import static seedu.address.logic.commands.CommandTestUtil.PHONE_DESC_AMY; +import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalPersons.AMY; import java.io.IOException; @@ -15,7 +16,6 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import seedu.address.logic.commands.AddCommand; @@ -34,13 +34,9 @@ import seedu.address.storage.StorageManager; import seedu.address.testutil.PersonBuilder; - public class LogicManagerTest { private static final IOException DUMMY_IO_EXCEPTION = new IOException("dummy exception"); - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); @@ -70,7 +66,7 @@ public void execute_commandExecutionError_throwsCommandException() { } @Test - public void execute_validCommand_success() { + public void execute_validCommand_success() throws Exception { String listCommand = ListCommand.COMMAND_WORD; assertCommandSuccess(listCommand, ListCommand.MESSAGE_SUCCESS, model); assertHistoryCorrect(listCommand); @@ -93,28 +89,30 @@ public void execute_storageThrowsIoException_throwsCommandException() throws Exc expectedModel.addPerson(expectedPerson); expectedModel.commitAddressBook(); String expectedMessage = LogicManager.FILE_OPS_ERROR_MESSAGE + DUMMY_IO_EXCEPTION; - assertCommandBehavior(CommandException.class, addCommand, expectedMessage, expectedModel); + assertCommandFailure(addCommand, CommandException.class, expectedMessage, expectedModel); assertHistoryCorrect(addCommand); } @Test public void getFilteredPersonList_modifyList_throwsUnsupportedOperationException() { - thrown.expect(UnsupportedOperationException.class); - logic.getFilteredPersonList().remove(0); + assertThrows(UnsupportedOperationException.class, () -> logic.getFilteredPersonList().remove(0)); } /** * Executes the command, confirms that no exceptions are thrown and that the result message is correct. * Also confirms that {@code expectedModel} is as specified. - * @see #assertCommandBehavior(Class, String, String, Model) + * @see #assertCommandFailure(String, Class, String, Model) */ - private void assertCommandSuccess(String inputCommand, String expectedMessage, Model expectedModel) { - assertCommandBehavior(null, inputCommand, expectedMessage, expectedModel); + private void assertCommandSuccess(String inputCommand, String expectedMessage, + Model expectedModel) throws Exception { + CommandResult result = logic.execute(inputCommand); + assertEquals(expectedMessage, result.getFeedbackToUser()); + assertEquals(model, expectedModel); } /** * Executes the command, confirms that a ParseException is thrown and that the result message is correct. - * @see #assertCommandBehavior(Class, String, String, Model) + * @see #assertCommandFailure(String, Class, String, Model) */ private void assertParseException(String inputCommand, String expectedMessage) { assertCommandFailure(inputCommand, ParseException.class, expectedMessage); @@ -122,7 +120,7 @@ private void assertParseException(String inputCommand, String expectedMessage) { /** * Executes the command, confirms that a CommandException is thrown and that the result message is correct. - * @see #assertCommandBehavior(Class, String, String, Model) + * @see #assertCommandFailure(String, Class, String, Model) */ private void assertCommandException(String inputCommand, String expectedMessage) { assertCommandFailure(inputCommand, CommandException.class, expectedMessage); @@ -130,31 +128,20 @@ private void assertCommandException(String inputCommand, String expectedMessage) /** * Executes the command, confirms that the exception is thrown and that the result message is correct. - * @see #assertCommandBehavior(Class, String, String, Model) + * @see #assertCommandFailure(String, Class, String, Model) */ - private void assertCommandFailure(String inputCommand, Class expectedException, String expectedMessage) { + private void assertCommandFailure(String inputCommand, Class expectedException, + String expectedMessage) { Model expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); - assertCommandBehavior(expectedException, inputCommand, expectedMessage, expectedModel); + assertCommandFailure(inputCommand, expectedException, expectedMessage, expectedModel); } /** - * Executes the command, confirms that the result message is correct and that the expected exception is thrown, - * and also confirms that the following two parts of the LogicManager object's state are as expected:
- * - the internal model manager data are same as those in the {@code expectedModel}
- * - {@code expectedModel}'s address book was saved to the storage file. + * Executes the command, confirms that the exception is thrown and that the result message and model is correct. */ - private void assertCommandBehavior(Class expectedException, String inputCommand, - String expectedMessage, Model expectedModel) { - - try { - CommandResult result = logic.execute(inputCommand); - assertEquals(expectedException, null); - assertEquals(expectedMessage, result.getFeedbackToUser()); - } catch (CommandException | ParseException e) { - assertEquals(expectedException, e.getClass()); - assertEquals(expectedMessage, e.getMessage()); - } - + private void assertCommandFailure(String inputCommand, Class expectedException, + String expectedMessage, Model expectedModel) { + assertThrows(expectedException, expectedMessage, () -> logic.execute(inputCommand)); assertEquals(expectedModel, model); } diff --git a/src/test/java/seedu/address/logic/commands/AddCommandTest.java b/src/test/java/seedu/address/logic/commands/AddCommandTest.java index f120a897779b..d55943fe1ac5 100644 --- a/src/test/java/seedu/address/logic/commands/AddCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddCommandTest.java @@ -1,18 +1,17 @@ package seedu.address.logic.commands; import static java.util.Objects.requireNonNull; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.Assert.assertThrows; import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.function.Predicate; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import javafx.beans.property.ReadOnlyProperty; import javafx.collections.ObservableList; @@ -30,15 +29,11 @@ public class AddCommandTest { private static final CommandHistory EMPTY_COMMAND_HISTORY = new CommandHistory(); - @Rule - public ExpectedException thrown = ExpectedException.none(); - private CommandHistory commandHistory = new CommandHistory(); @Test public void constructor_nullPerson_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - new AddCommand(null); + assertThrows(NullPointerException.class, () -> new AddCommand(null)); } @Test @@ -54,14 +49,13 @@ public void execute_personAcceptedByModel_addSuccessful() throws Exception { } @Test - public void execute_duplicatePerson_throwsCommandException() throws Exception { + public void execute_duplicatePerson_throwsCommandException() { Person validPerson = new PersonBuilder().build(); AddCommand addCommand = new AddCommand(validPerson); ModelStub modelStub = new ModelStubWithPerson(validPerson); - thrown.expect(CommandException.class); - thrown.expectMessage(AddCommand.MESSAGE_DUPLICATE_PERSON); - addCommand.execute(modelStub, commandHistory); + assertThrows(CommandException.class, AddCommand.MESSAGE_DUPLICATE_PERSON, () + -> addCommand.execute(modelStub, commandHistory)); } @Test diff --git a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java index eb87ddcfbcf3..d65d8561b25f 100644 --- a/src/test/java/seedu/address/logic/commands/CommandTestUtil.java +++ b/src/test/java/seedu/address/logic/commands/CommandTestUtil.java @@ -7,6 +7,7 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; +import static seedu.address.testutil.Assert.assertThrows; import java.util.ArrayList; import java.util.Arrays; @@ -115,18 +116,13 @@ public static void assertCommandFailure(Command command, Model actualModel, Comm CommandHistory expectedCommandHistory = new CommandHistory(actualCommandHistory); - try { - command.execute(actualModel, actualCommandHistory); - throw new AssertionError("The expected CommandException was not thrown."); - } catch (CommandException e) { - assertEquals(expectedMessage, e.getMessage()); - assertEquals(expectedAddressBook, actualModel.getAddressBook()); - assertEquals(expectedFilteredList, actualModel.getFilteredPersonList()); - assertEquals(expectedSelectedPerson, actualModel.getSelectedPerson()); - assertEquals(expectedCommandHistory, actualCommandHistory); - } - } + assertThrows(CommandException.class, expectedMessage, () -> command.execute(actualModel, actualCommandHistory)); + assertEquals(expectedAddressBook, actualModel.getAddressBook()); + assertEquals(expectedFilteredList, actualModel.getFilteredPersonList()); + assertEquals(expectedSelectedPerson, actualModel.getSelectedPerson()); + assertEquals(expectedCommandHistory, actualCommandHistory); + } /** * Updates {@code model}'s filtered list to show only the person at the given {@code targetIndex} in the * {@code model}'s address book. diff --git a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java index 78fd9c877708..d116dc415188 100644 --- a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java +++ b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertTrue; import static seedu.address.logic.parser.ParserUtil.MESSAGE_INVALID_INDEX; +import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; import java.util.Arrays; @@ -11,9 +12,7 @@ import java.util.HashSet; import java.util.Set; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.person.Address; @@ -21,7 +20,6 @@ import seedu.address.model.person.Name; import seedu.address.model.person.Phone; import seedu.address.model.tag.Tag; -import seedu.address.testutil.Assert; public class ParserUtilTest { private static final String INVALID_NAME = "R@chel"; @@ -39,20 +37,15 @@ public class ParserUtilTest { private static final String WHITESPACE = " \t\r\n"; - @Rule - public final ExpectedException thrown = ExpectedException.none(); - @Test public void parseIndex_invalidInput_throwsParseException() throws Exception { - thrown.expect(ParseException.class); - ParserUtil.parseIndex("10 a"); + assertThrows(ParseException.class, () -> ParserUtil.parseIndex("10 a")); } @Test public void parseIndex_outOfRangeInput_throwsParseException() throws Exception { - thrown.expect(ParseException.class); - thrown.expectMessage(MESSAGE_INVALID_INDEX); - ParserUtil.parseIndex(Long.toString(Integer.MAX_VALUE + 1)); + assertThrows(ParseException.class, MESSAGE_INVALID_INDEX, () + -> ParserUtil.parseIndex(Long.toString(Integer.MAX_VALUE + 1))); } @Test @@ -66,12 +59,12 @@ public void parseIndex_validInput_success() throws Exception { @Test public void parseName_null_throwsNullPointerException() { - Assert.assertThrows(NullPointerException.class, () -> ParserUtil.parseName((String) null)); + assertThrows(NullPointerException.class, () -> ParserUtil.parseName((String) null)); } @Test public void parseName_invalidValue_throwsParseException() { - Assert.assertThrows(ParseException.class, () -> ParserUtil.parseName(INVALID_NAME)); + assertThrows(ParseException.class, () -> ParserUtil.parseName(INVALID_NAME)); } @Test @@ -89,12 +82,12 @@ public void parseName_validValueWithWhitespace_returnsTrimmedName() throws Excep @Test public void parsePhone_null_throwsNullPointerException() { - Assert.assertThrows(NullPointerException.class, () -> ParserUtil.parsePhone((String) null)); + assertThrows(NullPointerException.class, () -> ParserUtil.parsePhone((String) null)); } @Test public void parsePhone_invalidValue_throwsParseException() { - Assert.assertThrows(ParseException.class, () -> ParserUtil.parsePhone(INVALID_PHONE)); + assertThrows(ParseException.class, () -> ParserUtil.parsePhone(INVALID_PHONE)); } @Test @@ -112,12 +105,12 @@ public void parsePhone_validValueWithWhitespace_returnsTrimmedPhone() throws Exc @Test public void parseAddress_null_throwsNullPointerException() { - Assert.assertThrows(NullPointerException.class, () -> ParserUtil.parseAddress((String) null)); + assertThrows(NullPointerException.class, () -> ParserUtil.parseAddress((String) null)); } @Test public void parseAddress_invalidValue_throwsParseException() { - Assert.assertThrows(ParseException.class, () -> ParserUtil.parseAddress(INVALID_ADDRESS)); + assertThrows(ParseException.class, () -> ParserUtil.parseAddress(INVALID_ADDRESS)); } @Test @@ -135,12 +128,12 @@ public void parseAddress_validValueWithWhitespace_returnsTrimmedAddress() throws @Test public void parseEmail_null_throwsNullPointerException() { - Assert.assertThrows(NullPointerException.class, () -> ParserUtil.parseEmail((String) null)); + assertThrows(NullPointerException.class, () -> ParserUtil.parseEmail((String) null)); } @Test public void parseEmail_invalidValue_throwsParseException() { - Assert.assertThrows(ParseException.class, () -> ParserUtil.parseEmail(INVALID_EMAIL)); + assertThrows(ParseException.class, () -> ParserUtil.parseEmail(INVALID_EMAIL)); } @Test @@ -158,14 +151,12 @@ public void parseEmail_validValueWithWhitespace_returnsTrimmedEmail() throws Exc @Test public void parseTag_null_throwsNullPointerException() throws Exception { - thrown.expect(NullPointerException.class); - ParserUtil.parseTag(null); + assertThrows(NullPointerException.class, () -> ParserUtil.parseTag(null)); } @Test public void parseTag_invalidValue_throwsParseException() throws Exception { - thrown.expect(ParseException.class); - ParserUtil.parseTag(INVALID_TAG); + assertThrows(ParseException.class, () -> ParserUtil.parseTag(INVALID_TAG)); } @Test @@ -183,14 +174,12 @@ public void parseTag_validValueWithWhitespace_returnsTrimmedTag() throws Excepti @Test public void parseTags_null_throwsNullPointerException() throws Exception { - thrown.expect(NullPointerException.class); - ParserUtil.parseTags(null); + assertThrows(NullPointerException.class, () -> ParserUtil.parseTags(null)); } @Test public void parseTags_collectionWithInvalidTags_throwsParseException() throws Exception { - thrown.expect(ParseException.class); - ParserUtil.parseTags(Arrays.asList(VALID_TAG_1, INVALID_TAG)); + assertThrows(ParseException.class, () -> ParserUtil.parseTags(Arrays.asList(VALID_TAG_1, INVALID_TAG))); } @Test diff --git a/src/test/java/seedu/address/model/AddressBookTest.java b/src/test/java/seedu/address/model/AddressBookTest.java index 433147e1534a..7bb409570739 100644 --- a/src/test/java/seedu/address/model/AddressBookTest.java +++ b/src/test/java/seedu/address/model/AddressBookTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertTrue; import static seedu.address.logic.commands.CommandTestUtil.VALID_ADDRESS_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND; +import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalPersons.ALICE; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; @@ -13,9 +14,7 @@ import java.util.Collections; import java.util.List; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import javafx.beans.InvalidationListener; import javafx.beans.property.SimpleIntegerProperty; @@ -27,9 +26,6 @@ public class AddressBookTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - private final AddressBook addressBook = new AddressBook(); @Test @@ -39,8 +35,7 @@ public void constructor() { @Test public void resetData_null_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - addressBook.resetData(null); + assertThrows(NullPointerException.class, () -> addressBook.resetData(null)); } @Test @@ -58,14 +53,12 @@ public void resetData_withDuplicatePersons_throwsDuplicatePersonException() { List newPersons = Arrays.asList(ALICE, editedAlice); AddressBookStub newData = new AddressBookStub(newPersons); - thrown.expect(DuplicatePersonException.class); - addressBook.resetData(newData); + assertThrows(DuplicatePersonException.class, () -> addressBook.resetData(newData)); } @Test public void hasPerson_nullPerson_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - addressBook.hasPerson(null); + assertThrows(NullPointerException.class, () -> addressBook.hasPerson(null)); } @Test @@ -89,8 +82,7 @@ public void hasPerson_personWithSameIdentityFieldsInAddressBook_returnsTrue() { @Test public void getPersonList_modifyList_throwsUnsupportedOperationException() { - thrown.expect(UnsupportedOperationException.class); - addressBook.getPersonList().remove(0); + assertThrows(UnsupportedOperationException.class, () -> addressBook.getPersonList().remove(0)); } @Test diff --git a/src/test/java/seedu/address/model/ModelManagerTest.java b/src/test/java/seedu/address/model/ModelManagerTest.java index e865629884d5..4ad457d629fe 100644 --- a/src/test/java/seedu/address/model/ModelManagerTest.java +++ b/src/test/java/seedu/address/model/ModelManagerTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertTrue; import static seedu.address.logic.commands.CommandTestUtil.VALID_EMAIL_BOB; import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; +import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalPersons.ALICE; import static seedu.address.testutil.TypicalPersons.BENSON; import static seedu.address.testutil.TypicalPersons.BOB; @@ -14,9 +15,7 @@ import java.util.Arrays; import java.util.Collections; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import seedu.address.commons.core.GuiSettings; import seedu.address.model.person.NameContainsKeywordsPredicate; @@ -26,8 +25,6 @@ import seedu.address.testutil.PersonBuilder; public class ModelManagerTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); private ModelManager modelManager = new ModelManager(); @@ -41,8 +38,7 @@ public void constructor() { @Test public void setUserPrefs_nullUserPrefs_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - modelManager.setUserPrefs(null); + assertThrows(NullPointerException.class, () -> modelManager.setUserPrefs(null)); } @Test @@ -61,8 +57,7 @@ public void setUserPrefs_validUserPrefs_copiesUserPrefs() { @Test public void setGuiSettings_nullGuiSettings_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - modelManager.setGuiSettings(null); + assertThrows(NullPointerException.class, () -> modelManager.setGuiSettings(null)); } @Test @@ -74,8 +69,7 @@ public void setGuiSettings_validGuiSettings_setsGuiSettings() { @Test public void setAddressBookFilePath_nullPath_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - modelManager.setAddressBookFilePath(null); + assertThrows(NullPointerException.class, () -> modelManager.setAddressBookFilePath(null)); } @Test @@ -87,8 +81,7 @@ public void setAddressBookFilePath_validPath_setsAddressBookFilePath() { @Test public void hasPerson_nullPerson_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - modelManager.hasPerson(null); + assertThrows(NullPointerException.class, () -> modelManager.hasPerson(null)); } @Test @@ -131,14 +124,12 @@ public void setPerson_personIsSelected_selectedPersonUpdated() { @Test public void getFilteredPersonList_modifyList_throwsUnsupportedOperationException() { - thrown.expect(UnsupportedOperationException.class); - modelManager.getFilteredPersonList().remove(0); + assertThrows(UnsupportedOperationException.class, () -> modelManager.getFilteredPersonList().remove(0)); } @Test public void setSelectedPerson_personNotInFilteredPersonList_throwsPersonNotFoundException() { - thrown.expect(PersonNotFoundException.class); - modelManager.setSelectedPerson(ALICE); + assertThrows(PersonNotFoundException.class, () -> modelManager.setSelectedPerson(ALICE)); } @Test diff --git a/src/test/java/seedu/address/model/VersionedAddressBookTest.java b/src/test/java/seedu/address/model/VersionedAddressBookTest.java index d5a31a48e53f..9b38ef3b8423 100644 --- a/src/test/java/seedu/address/model/VersionedAddressBookTest.java +++ b/src/test/java/seedu/address/model/VersionedAddressBookTest.java @@ -1,9 +1,9 @@ package seedu.address.model; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalPersons.AMY; import static seedu.address.testutil.TypicalPersons.BOB; import static seedu.address.testutil.TypicalPersons.CARL; diff --git a/src/test/java/seedu/address/model/person/AddressTest.java b/src/test/java/seedu/address/model/person/AddressTest.java index 11974544d81d..ac29a8317216 100644 --- a/src/test/java/seedu/address/model/person/AddressTest.java +++ b/src/test/java/seedu/address/model/person/AddressTest.java @@ -1,29 +1,28 @@ package seedu.address.model.person; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.Assert.assertThrows; import org.junit.Test; -import seedu.address.testutil.Assert; - public class AddressTest { @Test public void constructor_null_throwsNullPointerException() { - Assert.assertThrows(NullPointerException.class, () -> new Address(null)); + assertThrows(NullPointerException.class, () -> new Address(null)); } @Test public void constructor_invalidAddress_throwsIllegalArgumentException() { String invalidAddress = ""; - Assert.assertThrows(IllegalArgumentException.class, () -> new Address(invalidAddress)); + assertThrows(IllegalArgumentException.class, () -> new Address(invalidAddress)); } @Test public void isValidAddress() { // null address - Assert.assertThrows(NullPointerException.class, () -> Address.isValidAddress(null)); + assertThrows(NullPointerException.class, () -> Address.isValidAddress(null)); // invalid addresses assertFalse(Address.isValidAddress("")); // empty string diff --git a/src/test/java/seedu/address/model/person/EmailTest.java b/src/test/java/seedu/address/model/person/EmailTest.java index ecbd0ee50208..150a78fe6a54 100644 --- a/src/test/java/seedu/address/model/person/EmailTest.java +++ b/src/test/java/seedu/address/model/person/EmailTest.java @@ -1,29 +1,28 @@ package seedu.address.model.person; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.Assert.assertThrows; import org.junit.Test; -import seedu.address.testutil.Assert; - public class EmailTest { @Test public void constructor_null_throwsNullPointerException() { - Assert.assertThrows(NullPointerException.class, () -> new Email(null)); + assertThrows(NullPointerException.class, () -> new Email(null)); } @Test public void constructor_invalidEmail_throwsIllegalArgumentException() { String invalidEmail = ""; - Assert.assertThrows(IllegalArgumentException.class, () -> new Email(invalidEmail)); + assertThrows(IllegalArgumentException.class, () -> new Email(invalidEmail)); } @Test public void isValidEmail() { // null email - Assert.assertThrows(NullPointerException.class, () -> Email.isValidEmail(null)); + assertThrows(NullPointerException.class, () -> Email.isValidEmail(null)); // blank email assertFalse(Email.isValidEmail("")); // empty string diff --git a/src/test/java/seedu/address/model/person/NameTest.java b/src/test/java/seedu/address/model/person/NameTest.java index b4a356b6f011..2b5999f73a42 100644 --- a/src/test/java/seedu/address/model/person/NameTest.java +++ b/src/test/java/seedu/address/model/person/NameTest.java @@ -1,29 +1,28 @@ package seedu.address.model.person; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.Assert.assertThrows; import org.junit.Test; -import seedu.address.testutil.Assert; - public class NameTest { @Test public void constructor_null_throwsNullPointerException() { - Assert.assertThrows(NullPointerException.class, () -> new Name(null)); + assertThrows(NullPointerException.class, () -> new Name(null)); } @Test public void constructor_invalidName_throwsIllegalArgumentException() { String invalidName = ""; - Assert.assertThrows(IllegalArgumentException.class, () -> new Name(invalidName)); + assertThrows(IllegalArgumentException.class, () -> new Name(invalidName)); } @Test public void isValidName() { // null name - Assert.assertThrows(NullPointerException.class, () -> Name.isValidName(null)); + assertThrows(NullPointerException.class, () -> Name.isValidName(null)); // invalid name assertFalse(Name.isValidName("")); // empty string diff --git a/src/test/java/seedu/address/model/person/PersonTest.java b/src/test/java/seedu/address/model/person/PersonTest.java index ac75b257fa39..191764caa6f8 100644 --- a/src/test/java/seedu/address/model/person/PersonTest.java +++ b/src/test/java/seedu/address/model/person/PersonTest.java @@ -7,24 +7,20 @@ import static seedu.address.logic.commands.CommandTestUtil.VALID_NAME_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_PHONE_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND; +import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalPersons.ALICE; import static seedu.address.testutil.TypicalPersons.BOB; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import seedu.address.testutil.PersonBuilder; public class PersonTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); @Test public void asObservableList_modifyList_throwsUnsupportedOperationException() { Person person = new PersonBuilder().build(); - thrown.expect(UnsupportedOperationException.class); - person.getTags().remove(0); + assertThrows(UnsupportedOperationException.class, () -> person.getTags().remove(0)); } @Test diff --git a/src/test/java/seedu/address/model/person/PhoneTest.java b/src/test/java/seedu/address/model/person/PhoneTest.java index c721cbbfc048..c15f3e3c2aff 100644 --- a/src/test/java/seedu/address/model/person/PhoneTest.java +++ b/src/test/java/seedu/address/model/person/PhoneTest.java @@ -1,29 +1,28 @@ package seedu.address.model.person; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.Assert.assertThrows; import org.junit.Test; -import seedu.address.testutil.Assert; - public class PhoneTest { @Test public void constructor_null_throwsNullPointerException() { - Assert.assertThrows(NullPointerException.class, () -> new Phone(null)); + assertThrows(NullPointerException.class, () -> new Phone(null)); } @Test public void constructor_invalidPhone_throwsIllegalArgumentException() { String invalidPhone = ""; - Assert.assertThrows(IllegalArgumentException.class, () -> new Phone(invalidPhone)); + assertThrows(IllegalArgumentException.class, () -> new Phone(invalidPhone)); } @Test public void isValidPhone() { // null phone number - Assert.assertThrows(NullPointerException.class, () -> Phone.isValidPhone(null)); + assertThrows(NullPointerException.class, () -> Phone.isValidPhone(null)); // invalid phone numbers assertFalse(Phone.isValidPhone("")); // empty string diff --git a/src/test/java/seedu/address/model/person/UniquePersonListTest.java b/src/test/java/seedu/address/model/person/UniquePersonListTest.java index 6389cd81703f..737c57ac28eb 100644 --- a/src/test/java/seedu/address/model/person/UniquePersonListTest.java +++ b/src/test/java/seedu/address/model/person/UniquePersonListTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertTrue; import static seedu.address.logic.commands.CommandTestUtil.VALID_ADDRESS_BOB; import static seedu.address.logic.commands.CommandTestUtil.VALID_TAG_HUSBAND; +import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalPersons.ALICE; import static seedu.address.testutil.TypicalPersons.BOB; @@ -12,24 +13,19 @@ import java.util.Collections; import java.util.List; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import seedu.address.model.person.exceptions.DuplicatePersonException; import seedu.address.model.person.exceptions.PersonNotFoundException; import seedu.address.testutil.PersonBuilder; public class UniquePersonListTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); private final UniquePersonList uniquePersonList = new UniquePersonList(); @Test public void contains_nullPerson_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - uniquePersonList.contains(null); + assertThrows(NullPointerException.class, () -> uniquePersonList.contains(null)); } @Test @@ -53,33 +49,28 @@ public void contains_personWithSameIdentityFieldsInList_returnsTrue() { @Test public void add_nullPerson_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - uniquePersonList.add(null); + assertThrows(NullPointerException.class, () -> uniquePersonList.add(null)); } @Test public void add_duplicatePerson_throwsDuplicatePersonException() { uniquePersonList.add(ALICE); - thrown.expect(DuplicatePersonException.class); - uniquePersonList.add(ALICE); + assertThrows(DuplicatePersonException.class, () -> uniquePersonList.add(ALICE)); } @Test public void setPerson_nullTargetPerson_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - uniquePersonList.setPerson(null, ALICE); + assertThrows(NullPointerException.class, () -> uniquePersonList.setPerson(null, ALICE)); } @Test public void setPerson_nullEditedPerson_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - uniquePersonList.setPerson(ALICE, null); + assertThrows(NullPointerException.class, () -> uniquePersonList.setPerson(ALICE, null)); } @Test public void setPerson_targetPersonNotInList_throwsPersonNotFoundException() { - thrown.expect(PersonNotFoundException.class); - uniquePersonList.setPerson(ALICE, ALICE); + assertThrows(PersonNotFoundException.class, () -> uniquePersonList.setPerson(ALICE, ALICE)); } @Test @@ -115,20 +106,17 @@ public void setPerson_editedPersonHasDifferentIdentity_success() { public void setPerson_editedPersonHasNonUniqueIdentity_throwsDuplicatePersonException() { uniquePersonList.add(ALICE); uniquePersonList.add(BOB); - thrown.expect(DuplicatePersonException.class); - uniquePersonList.setPerson(ALICE, BOB); + assertThrows(DuplicatePersonException.class, () -> uniquePersonList.setPerson(ALICE, BOB)); } @Test public void remove_nullPerson_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - uniquePersonList.remove(null); + assertThrows(NullPointerException.class, () -> uniquePersonList.remove(null)); } @Test public void remove_personDoesNotExist_throwsPersonNotFoundException() { - thrown.expect(PersonNotFoundException.class); - uniquePersonList.remove(ALICE); + assertThrows(PersonNotFoundException.class, () -> uniquePersonList.remove(ALICE)); } @Test @@ -141,8 +129,7 @@ public void remove_existingPerson_removesPerson() { @Test public void setPersons_nullUniquePersonList_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - uniquePersonList.setPersons((UniquePersonList) null); + assertThrows(NullPointerException.class, () -> uniquePersonList.setPersons((UniquePersonList) null)); } @Test @@ -156,8 +143,7 @@ public void setPersons_uniquePersonList_replacesOwnListWithProvidedUniquePersonL @Test public void setPersons_nullList_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - uniquePersonList.setPersons((List) null); + assertThrows(NullPointerException.class, () -> uniquePersonList.setPersons((List) null)); } @Test @@ -173,13 +159,12 @@ public void setPersons_list_replacesOwnListWithProvidedList() { @Test public void setPersons_listWithDuplicatePersons_throwsDuplicatePersonException() { List listWithDuplicatePersons = Arrays.asList(ALICE, ALICE); - thrown.expect(DuplicatePersonException.class); - uniquePersonList.setPersons(listWithDuplicatePersons); + assertThrows(DuplicatePersonException.class, () -> uniquePersonList.setPersons(listWithDuplicatePersons)); } @Test public void asUnmodifiableObservableList_modifyList_throwsUnsupportedOperationException() { - thrown.expect(UnsupportedOperationException.class); - uniquePersonList.asUnmodifiableObservableList().remove(0); + assertThrows(UnsupportedOperationException.class, () + -> uniquePersonList.asUnmodifiableObservableList().remove(0)); } } diff --git a/src/test/java/seedu/address/model/tag/TagTest.java b/src/test/java/seedu/address/model/tag/TagTest.java index 80bfd156d816..2c5fbe2d006f 100644 --- a/src/test/java/seedu/address/model/tag/TagTest.java +++ b/src/test/java/seedu/address/model/tag/TagTest.java @@ -1,26 +1,27 @@ package seedu.address.model.tag; +import static seedu.address.testutil.Assert.assertThrows; + import org.junit.Test; -import seedu.address.testutil.Assert; public class TagTest { @Test public void constructor_null_throwsNullPointerException() { - Assert.assertThrows(NullPointerException.class, () -> new Tag(null)); + assertThrows(NullPointerException.class, () -> new Tag(null)); } @Test public void constructor_invalidTagName_throwsIllegalArgumentException() { String invalidTagName = ""; - Assert.assertThrows(IllegalArgumentException.class, () -> new Tag(invalidTagName)); + assertThrows(IllegalArgumentException.class, () -> new Tag(invalidTagName)); } @Test public void isValidTagName() { // null tag name - Assert.assertThrows(NullPointerException.class, () -> Tag.isValidTagName(null)); + assertThrows(NullPointerException.class, () -> Tag.isValidTagName(null)); } } diff --git a/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java b/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java index c21a37283d81..22ab01b6e6e3 100644 --- a/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java +++ b/src/test/java/seedu/address/storage/JsonAdaptedPersonTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertEquals; import static seedu.address.storage.JsonAdaptedPerson.MISSING_FIELD_MESSAGE_FORMAT; +import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalPersons.BENSON; import java.util.ArrayList; @@ -104,7 +105,7 @@ public void toModelType_invalidTags_throwsIllegalValueException() { invalidTags.add(new JsonAdaptedTag(INVALID_TAG)); JsonAdaptedPerson person = new JsonAdaptedPerson(VALID_NAME, VALID_PHONE, VALID_EMAIL, VALID_ADDRESS, invalidTags); - Assert.assertThrows(IllegalValueException.class, person::toModelType); + assertThrows(IllegalValueException.class, person::toModelType); } } diff --git a/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java b/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java index 6c18ecd0edd9..a06c8559e0ed 100644 --- a/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java +++ b/src/test/java/seedu/address/storage/JsonAddressBookStorageTest.java @@ -1,7 +1,8 @@ package seedu.address.storage; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalPersons.ALICE; import static seedu.address.testutil.TypicalPersons.HOON; import static seedu.address.testutil.TypicalPersons.IDA; @@ -13,7 +14,6 @@ import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import seedu.address.commons.exceptions.DataConversionException; @@ -23,16 +23,13 @@ public class JsonAddressBookStorageTest { private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "JsonAddressBookStorageTest"); - @Rule - public ExpectedException thrown = ExpectedException.none(); @Rule public TemporaryFolder testFolder = new TemporaryFolder(); @Test public void readAddressBook_nullFilePath_throwsNullPointerException() throws Exception { - thrown.expect(NullPointerException.class); - readAddressBook(null); + assertThrows(NullPointerException.class, () -> readAddressBook(null)); } private java.util.Optional readAddressBook(String filePath) throws Exception { @@ -52,24 +49,17 @@ public void read_missingFile_emptyResult() throws Exception { @Test public void read_notJsonFormat_exceptionThrown() throws Exception { - - thrown.expect(DataConversionException.class); - readAddressBook("notJsonFormatAddressBook.json"); - - // IMPORTANT: Any code below an exception-throwing line (like the one above) will be ignored. - // That means you should not have more than one exception test in one method + assertThrows(DataConversionException.class, () -> readAddressBook("notJsonFormatAddressBook.json")); } @Test public void readAddressBook_invalidPersonAddressBook_throwDataConversionException() throws Exception { - thrown.expect(DataConversionException.class); - readAddressBook("invalidPersonAddressBook.json"); + assertThrows(DataConversionException.class, () -> readAddressBook("invalidPersonAddressBook.json")); } @Test public void readAddressBook_invalidAndValidPersonAddressBook_throwDataConversionException() throws Exception { - thrown.expect(DataConversionException.class); - readAddressBook("invalidAndValidPersonAddressBook.json"); + assertThrows(DataConversionException.class, () -> readAddressBook("invalidAndValidPersonAddressBook.json")); } @Test @@ -100,8 +90,7 @@ public void readAndSaveAddressBook_allInOrder_success() throws Exception { @Test public void saveAddressBook_nullAddressBook_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - saveAddressBook(null, "SomeFile.json"); + assertThrows(NullPointerException.class, () -> saveAddressBook(null, "SomeFile.json")); } /** @@ -118,7 +107,6 @@ private void saveAddressBook(ReadOnlyAddressBook addressBook, String filePath) { @Test public void saveAddressBook_nullFilePath_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - saveAddressBook(new AddressBook(), null); + assertThrows(NullPointerException.class, () -> saveAddressBook(new AddressBook(), null)); } } diff --git a/src/test/java/seedu/address/storage/JsonSerializableAddressBookTest.java b/src/test/java/seedu/address/storage/JsonSerializableAddressBookTest.java index 9bd56c0e6c5a..997290f6823c 100644 --- a/src/test/java/seedu/address/storage/JsonSerializableAddressBookTest.java +++ b/src/test/java/seedu/address/storage/JsonSerializableAddressBookTest.java @@ -1,13 +1,12 @@ package seedu.address.storage; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static seedu.address.testutil.Assert.assertThrows; import java.nio.file.Path; import java.nio.file.Paths; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import seedu.address.commons.exceptions.IllegalValueException; import seedu.address.commons.util.JsonUtil; @@ -21,9 +20,6 @@ public class JsonSerializableAddressBookTest { private static final Path INVALID_PERSON_FILE = TEST_DATA_FOLDER.resolve("invalidPersonAddressBook.json"); private static final Path DUPLICATE_PERSON_FILE = TEST_DATA_FOLDER.resolve("duplicatePersonAddressBook.json"); - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test public void toModelType_typicalPersonsFile_success() throws Exception { JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(TYPICAL_PERSONS_FILE, @@ -37,17 +33,15 @@ public void toModelType_typicalPersonsFile_success() throws Exception { public void toModelType_invalidPersonFile_throwsIllegalValueException() throws Exception { JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(INVALID_PERSON_FILE, JsonSerializableAddressBook.class).get(); - thrown.expect(IllegalValueException.class); - dataFromFile.toModelType(); + assertThrows(IllegalValueException.class, () -> dataFromFile.toModelType()); } @Test public void toModelType_duplicatePersons_throwsIllegalValueException() throws Exception { JsonSerializableAddressBook dataFromFile = JsonUtil.readJsonFile(DUPLICATE_PERSON_FILE, JsonSerializableAddressBook.class).get(); - thrown.expect(IllegalValueException.class); - thrown.expectMessage(JsonSerializableAddressBook.MESSAGE_DUPLICATE_PERSON); - dataFromFile.toModelType(); + assertThrows(IllegalValueException.class, JsonSerializableAddressBook.MESSAGE_DUPLICATE_PERSON, + dataFromFile::toModelType); } } diff --git a/src/test/java/seedu/address/storage/JsonUserPrefsStorageTest.java b/src/test/java/seedu/address/storage/JsonUserPrefsStorageTest.java index 5d2699cc889a..02f8a4350d64 100644 --- a/src/test/java/seedu/address/storage/JsonUserPrefsStorageTest.java +++ b/src/test/java/seedu/address/storage/JsonUserPrefsStorageTest.java @@ -1,7 +1,8 @@ package seedu.address.storage; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static seedu.address.testutil.Assert.assertThrows; import java.io.IOException; import java.nio.file.Path; @@ -10,7 +11,6 @@ import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import seedu.address.commons.core.GuiSettings; @@ -21,16 +21,13 @@ public class JsonUserPrefsStorageTest { private static final Path TEST_DATA_FOLDER = Paths.get("src", "test", "data", "JsonUserPrefsStorageTest"); - @Rule - public ExpectedException thrown = ExpectedException.none(); @Rule public TemporaryFolder testFolder = new TemporaryFolder(); @Test public void readUserPrefs_nullFilePath_throwsNullPointerException() throws DataConversionException { - thrown.expect(NullPointerException.class); - readUserPrefs(null); + assertThrows(NullPointerException.class, () -> readUserPrefs(null)); } private Optional readUserPrefs(String userPrefsFileInTestDataFolder) throws DataConversionException { @@ -45,12 +42,7 @@ public void readUserPrefs_missingFile_emptyResult() throws DataConversionExcepti @Test public void readUserPrefs_notJsonFormat_exceptionThrown() throws DataConversionException { - thrown.expect(DataConversionException.class); - readUserPrefs("NotJsonFormatUserPrefs.json"); - - /* IMPORTANT: Any code below an exception-throwing line (like the one above) will be ignored. - * That means you should not have more than one exception test in one method - */ + assertThrows(DataConversionException.class, () -> readUserPrefs("NotJsonFormatUserPrefs.json")); } private Path addToTestDataPathIfNotNull(String userPrefsFileInTestDataFolder) { @@ -89,14 +81,12 @@ private UserPrefs getTypicalUserPrefs() { @Test public void savePrefs_nullPrefs_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - saveUserPrefs(null, "SomeFile.json"); + assertThrows(NullPointerException.class, () -> saveUserPrefs(null, "SomeFile.json")); } @Test public void saveUserPrefs_nullFilePath_throwsNullPointerException() { - thrown.expect(NullPointerException.class); - saveUserPrefs(new UserPrefs(), null); + assertThrows(NullPointerException.class, () -> saveUserPrefs(new UserPrefs(), null)); } /** diff --git a/src/test/java/seedu/address/testutil/Assert.java b/src/test/java/seedu/address/testutil/Assert.java index c72a7622b080..6590f6bfb4df 100644 --- a/src/test/java/seedu/address/testutil/Assert.java +++ b/src/test/java/seedu/address/testutil/Assert.java @@ -1,53 +1,33 @@ package seedu.address.testutil; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.function.Executable; + /** * A set of assertion methods useful for writing tests. */ public class Assert { /** - * Asserts that the {@code callable} throws the {@code expected} Exception. - */ - public static void assertThrows(Class expected, VoidCallable callable) { - assertThrows(expected, null, callable); - } - - /** - * Asserts that the {@code callable} throws the {@code expectedException} and the {@code expectedMessage}. - * If there's no need for the verification of the exception's error message, call - * {@code assertThrows(Class, VoidCallable)} instead. - * {@see assertThrows(Class, VoidCallable} + * Asserts that the {@code executable} throws the {@code expectedType} Exception. + * This is a wrapper method that invokes + * {@code Assertions.assertThrows(Class, Executable)}, to maintain consistency + * with our custom {@see assertThrows(Class, String, Executable)} method. */ - public static void assertThrows(Class expectedException, String expectedMessage, - VoidCallable callable) { - try { - callable.call(); - } catch (Throwable actualException) { - String errorMessage; - - if (!actualException.getClass().isAssignableFrom(expectedException)) { - errorMessage = String.format("Expected exception thrown: %s, actual: %s", - expectedException.getName(), actualException.getClass().getName()); - } else if (expectedMessage != null && !expectedMessage.equals(actualException.getMessage())) { - errorMessage = String.format( - "Expected message thrown: %s, actual: %s", expectedMessage, actualException.getMessage()); - } else { - return; - } - - throw new AssertionError(errorMessage, actualException); - } - - throw new AssertionError(String.format( - "Expected %s to be thrown, but nothing was thrown.", expectedException.getName())); + public static void assertThrows(Class expectedType, Executable executable) { + Assertions.assertThrows(expectedType, executable); } /** - * Represents a function which does not return anything and may throw an exception. + * Asserts that the {@code executable} throws the {@code expectedType} Exception with the + * {@code expectedMessage} message. If there's no need for the verification of the exception's error + * message, call {@code assertThrows(Class, Executable)} instead. + * {@see assertThrows(Class, Executable}} */ - @FunctionalInterface - public interface VoidCallable { - void call() throws Exception; + public static void assertThrows(Class expectedType, String expectedMessage, + Executable executable) { + Throwable thrownException = Assertions.assertThrows(expectedType, executable); + Assertions.assertEquals(expectedMessage, thrownException.getMessage()); } }