Skip to content

Commit 16c6635

Browse files
committed
fix user update
1 parent 10ca458 commit 16c6635

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

src/main/java/NextLevel/demo/user/entity/UserEntity.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import jakarta.persistence.Table;
2222
import java.lang.reflect.Field;
2323
import java.lang.reflect.InvocationTargetException;
24+
import java.lang.reflect.Method;
2425
import lombok.*;
2526
import org.hibernate.annotations.ColumnDefault;
2627

@@ -120,11 +121,16 @@ public void updateUserInfo(String name, String value) {
120121
throw new CustomException(ErrorCode.CAN_NOT_CHANGE_EMAIL);
121122

122123
try{
123-
Field field = UserEntity.class.getDeclaredField(name);
124-
field.set(this, value);
125-
} catch (IllegalAccessException | NoSuchFieldException e) {
124+
java.lang.reflect.Method method = UserEntity.class.getDeclaredMethod(StringUtil.setGetterName(name));
125+
method.invoke(this, value);
126+
} catch (IllegalAccessException | NoSuchMethodException e) {
126127
e.printStackTrace();
127128
throw new CustomException(ErrorCode.CAN_NOT_INVOKE, name);
129+
} catch (InvocationTargetException e) {
130+
if(e.getTargetException() instanceof CustomException)
131+
throw (CustomException) e.getTargetException();
132+
else
133+
throw new CustomException(ErrorCode.SIBAL_WHAT_IS_IT, e.getTargetException().getMessage());
128134
}
129135

130136
checkRole();

src/test/java/NextLevel/demo/user/service/userService/UserServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import java.util.Optional;
2828

2929
@ExtendWith(MockitoExtension.class)
30-
@Disabled
30+
// @Disabled
3131
public class UserServiceTest {
3232

3333
@InjectMocks

0 commit comments

Comments
 (0)