Java-spring-Junit5-Mockito测试设置
我正在尝试使用预期的pojo设置测试环境,但是我继续遇到构造函数不确定的错误。从错误中,我看到它将预期的pojo中的localTimedate列为可能是原因的字符串,但我不熟悉LocalDateTime条目的测试。
public class UserPojo {
private int id;
private String name;
private String email;
private String phoneNumber;
private String username;
private String password;
private boolean darkModePreference;
private LocalDateTime registerDate;
private int roleId;
public UserPojo() {
}
public UserPojo(int id, String name, String email, String phoneNumber, String username, String password,
boolean darkModePreference, LocalDateTime registerDate, int roleId) {
super();
this.id = id;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.username = username;
this.password = password;
this.darkModePreference = darkModePreference;
this.registerDate = registerDate;
this.roleId = roleId;
}
@ExtendWith(MockitoExtension.class)
public class LoginAndRegisterTest {
@Mock
UserDao userDao;
@InjectMocks
UserServiceImpl userService;
private UserPojo expectedPojo;
private UserEntity dummyEntity;
@BeforeEach
public void setup() {
LocalDateTime now;
expectedPojo = new UserPojo(1, "Goldendeep", "[email protected]", "333-343-3434", "golden", "kaur11", false, "2022-06-25T22:37:24.894", 2);
dummyEntity = new UserEntity(1, "Goldendeep", "[email protected]", "333-343-3434", "golden", "kaur11", false, "2022-06-25T22:37:24.894", 2);
}
I am trying to set up a testing environment using an expectedPojo, but I continue getting the error that the constructor is undefined. From the error, I see it is listing the LocalTimeDate in the expectedPojo as String which may be the cause, but I am unfamiliar with testing with a LocalDateTime entry.
public class UserPojo {
private int id;
private String name;
private String email;
private String phoneNumber;
private String username;
private String password;
private boolean darkModePreference;
private LocalDateTime registerDate;
private int roleId;
public UserPojo() {
}
public UserPojo(int id, String name, String email, String phoneNumber, String username, String password,
boolean darkModePreference, LocalDateTime registerDate, int roleId) {
super();
this.id = id;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.username = username;
this.password = password;
this.darkModePreference = darkModePreference;
this.registerDate = registerDate;
this.roleId = roleId;
}
@ExtendWith(MockitoExtension.class)
public class LoginAndRegisterTest {
@Mock
UserDao userDao;
@InjectMocks
UserServiceImpl userService;
private UserPojo expectedPojo;
private UserEntity dummyEntity;
@BeforeEach
public void setup() {
LocalDateTime now;
expectedPojo = new UserPojo(1, "Goldendeep", "[email protected]", "333-343-3434", "golden", "kaur11", false, "2022-06-25T22:37:24.894", 2);
dummyEntity = new UserEntity(1, "Goldendeep", "[email protected]", "333-343-3434", "golden", "kaur11", false, "2022-06-25T22:37:24.894", 2);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在创建UserPojo类的对象时,您将次要参数传递为字符串,而是预期的是localdateTime类型的参数,这会导致问题。提供LocalDateTime类型的参数,或者修改构造函数定义以接受字符串参数。
While creating the object of UserPojo class you are passing the second-to-last argument as String instead an argument of LocalDateTime type is expected, which is causing the issue. Either provide the argument of LocalDateTime type or modify the constructor definition to accept String argument.