Java-spring-Junit5-Mockito测试设置

发布于 2025-02-10 16:37:27 字数 1957 浏览 1 评论 0原文

我正在尝试使用预期的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言月老 2025-02-17 16:37:27

在创建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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文