这是一个非常基本的问题,但我似乎无法解决/找出问题,所以希望一些新的眼睛可以提供帮助。
我有一个具有用户名和密码的模型用户。我想允许用户更新他们的密码,所以我添加了 update() 方法,如下所示:
public static void update(User user) throws UserException {
User record = User.getById(user.id);
if (null == record) {
throw new UserException(BaseException.invalidRecordMessageException(User.class.getSimpleName()));
}
if (!StringUtils.equals(record.username, user.username)) {
throw new UserException(BaseException.unmodifiableFieldMessageException(User.class.getSimpleName(), "username"));
}
validatePassword(user.password);
saveUser(user);
}
这是我的 saveUser():
private static void saveUser(User user) {
user.password = encryptPassword(user.password);
user.save();
System.out.println("n: " + user.id);
System.out.println("n: " + user.username);
System.out.println("n: " + user.password);
User r = User.getById(user.id);
System.out.println("r: " + r.id);
System.out.println("r: " + r.username);
System.out.println("r: " + r.password);
}
我添加了一个测试以确认该功能运行良好。
@Test
public void testUpdateUserPasswordWithValidPasswordShouldUpdateRecord() {
try {
User dummy = new User(DEFAULT_USERNAME, DEFAULT_RAW_PASSWORD);
User.create(dummy);
assertNotNull(dummy.id);
User record = User.getById(dummy.id);
assertEquals(DEFAULT_USERNAME, record.username);
assertEquals(DEFAULT_RAW_PASSWORD, record.password);
dummy.password = DEFAULT_RAW_PASSWORD + "_extra";
User.update(dummy);
User updatedRecord = User.getById(dummy.id);
assertEquals(dummy.username, updatedRecord.username);
assertEquals(dummy.password, updatedRecord.password);
} catch (UserException ue) {
fail("Test should not throw exception.");
}
}
但是,测试失败,因为记录似乎没有更新:
Test UserTest.testUpdateUserPasswordWithValidPasswordShouldUpdateRecord failed: expected:<$uP3rsecretpassword[_extra]> but was:<$uP3rsecretpassword[]>
DEFAULT_PASSWORD = $uP3rsecretpassword
这是所有 System.out.println() 调用的输出:
--- 初始创建 ---
n: 1
n: 哞
n: $uP3rsecretpassword
r: 1
r: 哞
r: $uP3rsecretpassword
- - 更新 - -
n: 1
n: 哞
n: $uP3rsecretpassword_extra
r: 1
r: 哞
r: $uP3rsecretpassword
提前感谢任何帮助。