This is a follow-up question for Android app class serialization. Some problems have been mentioned in forsvarir's answer. Then, I am following JUnit 5 User Guide to redesign the test cases for User
class. Because the part of Save
and Load
methods are the operations including read and write on storage. It seems that they're not a part of unit testing.
The experimental implementation
Project name: UnitTestsForUserClass
Unit Tests for
User
class:package com.example.unittestsforuserclass; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Assertions; import java.security.NoSuchAlgorithmException; class UserTest { @org.junit.jupiter.api.Test void getFullName() { Assertions.assertThrows(NoSuchAlgorithmException.class, () -> { User User1 = new User( "Mike", "M12345678", "1990/10/13", "(555) 555-1234", "[email protected]", "password"); assertEquals("Mike", User1.GetFullName()); }); } @org.junit.jupiter.api.Test void getPersonalID() { Assertions.assertThrows(NoSuchAlgorithmException.class, () -> { User User1 = new User( "Mike", "M12345678", "1990/10/13", "(555) 555-1234", "[email protected]", "password"); assertEquals("M12345678", User1.GetPersonalID()); }); } @org.junit.jupiter.api.Test void getDateOfBirth() { Assertions.assertThrows(NoSuchAlgorithmException.class, () -> { User User1 = new User( "Mike", "M12345678", "1990/10/13", "(555) 555-1234", "[email protected]", "password"); assertEquals("1990/10/13", User1.GetDateOfBirth()); }); } @org.junit.jupiter.api.Test void getCellPhoneNumber() { Assertions.assertThrows(NoSuchAlgorithmException.class, () -> { User User1 = new User( "Mike", "M12345678", "1990/10/13", "(555) 555-1234", "[email protected]", "password"); assertEquals("(555) 555-1234", User1.GetCellPhoneNumber()); }); } @org.junit.jupiter.api.Test void getEmailInfo() { Assertions.assertThrows(NoSuchAlgorithmException.class, () -> { User User1 = new User( "Mike", "M12345678", "1990/10/13", "(555) 555-1234", "[email protected]", "password"); assertEquals("[email protected]", User1.GetEmailInfo()); }); } @org.junit.jupiter.api.Test void checkPassword() { Assertions.assertThrows(NoSuchAlgorithmException.class, () -> { User User1 = new User( "Mike", "M12345678", "1990/10/13", "(555) 555-1234", "[email protected]", "password"); assertEquals(User1.CheckPassword("password"), true); assertEquals(User1.CheckPassword("password1"), false); }); } }
User
class implementation:package com.example.unittestsforuserclass; import android.content.Context; import android.util.Log; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class User implements java.io.Serializable{ private String fullName; private String personalID; private String dateOfBirth; private String cellPhoneNumber; private String emailInfo; private String password; public User(String fullNameInput, String personalIDInput, String dateOfBirthInput, String cellPhoneNumberInput, String emailInfoInput, String passwordInput) throws NoSuchAlgorithmException // User object constructor { this.fullName = fullNameInput; this.personalID = personalIDInput; this.dateOfBirth = dateOfBirthInput; this.cellPhoneNumber = cellPhoneNumberInput; this.emailInfo = emailInfoInput; this.password = HashingMethod(passwordInput); } public String GetFullName() { return this.fullName; } public String GetPersonalID() { return this.personalID; } public String GetDateOfBirth() { return this.dateOfBirth; } public String GetCellPhoneNumber() { return this.cellPhoneNumber; } public String GetEmailInfo() { return this.emailInfo; } public String GetHash() throws NoSuchAlgorithmException { return HashingMethod(this.fullName + this.personalID); } public String GetHashedPassword() throws NoSuchAlgorithmException { return this.password; } public boolean CheckPassword(String password) { boolean result = false; try { result = this.password.equals(HashingMethod(password)); } catch (Exception e) { e.printStackTrace(); } return result; } // Reference: https://stackoverflow.com/a/4118917/6667035 // fileName cannot contain any path separator public boolean Save(Context context, String fileName) { try { FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); ObjectOutputStream os = new ObjectOutputStream(fos); os.writeObject(this); os.close(); fos.close(); return true; } catch (IOException i) { i.printStackTrace(); return false; } } public void Load(Context context, String fileName){ try { FileInputStream fis = context.openFileInput(fileName); ObjectInputStream is = new ObjectInputStream(fis); User simpleClass = (User) is.readObject(); is.close(); fis.close(); this.fullName = simpleClass.fullName; this.personalID = simpleClass.personalID; this.dateOfBirth = simpleClass.dateOfBirth; this.cellPhoneNumber = simpleClass.cellPhoneNumber; this.emailInfo = simpleClass.emailInfo; this.password = simpleClass.password; } catch (Exception e){ e.printStackTrace(); } } //***************************************************************************** // Reference: https://stackoverflow.com/a/2624385/6667035 private String HashingMethod(String InputString) throws NoSuchAlgorithmException { MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); String stringToHash = InputString; messageDigest.update(stringToHash.getBytes()); String stringHash = new String(messageDigest.digest()); return stringHash; } }
All suggestions are welcome.
The summary information:
Which question it is a follow-up to?
What changes has been made in the code since last question?
I am following JUnit 5 User Guide to redesign the test cases for
User
class.Why a new review is being asked for?
If there is any possible improvement, please let me know.
NoSuchAlgorithmException
to be thrown. This may be a misinterpretation of what I said on your other question. You should allow any exceptions thrown by the test to escape the test case unless you are actually testing for an expected exception to be thrown. So, for examplevoid getFullName() throws NoSuchAlgorithmException
then perform the rest of the test with no exception handling. \$\endgroup\$