gpt4 book ai didi

java - 单元测试 Junit NullPointerException 和 NoSuchElement 有帮助!休息终点

转载 作者:行者123 更新时间:2023-12-02 11:38:57 24 4
gpt4 key购买 nike

当我访问类来测试方法时,每次尝试定义新对象并使用新定义的对象时都会遇到异常。

测试错误:

  UserInformationControllerTest.deleteUser:83 » NullPointer
UserInformationControllerTest.getUserInfo:27 » NullPointer
UserInformationControllerTest.updateUserInfo:68 » NullPointer
UserOrderControllerTest.createUserOrder:60 » NoSuchElement
UserOrderControllerTest.getUserOrder:47 » NullPointer
UserOrderControllerTest.updateUserOrder:85 » NullPointer

我的任务是为每个类(class)制作 4 个满意的案例和 4 个不满意的案例

我完全困惑了。

我的 UserInformation 测试类

private HashMap<Integer,UserInformation> userInformationHashMap;

@Test
public void getUserInfo(){
UserInformationController userInformationController = new UserInformationController();
this.userInformationHashMap = new HashMap<>();

int user0 = 0;
int user1 = 1;

UserInformation userInformation0 = new UserInformation("Doug","Jones", "djones@gmail.com","17073");
UserInformation userInformation1 = new UserInformation("Natalie","Peirce", "nataliepeirce12@yahoo.com","dynamicrabbit");

this.userInformationHashMap.put(user0,userInformation0);
this.userInformationHashMap.put(user1,userInformation1);

userInformationController.getUserInfo(user0);
userInformationController.getUserInfo(user1);

Assert.assertEquals(userInformationController.getUserInfo(user0),userInformationController.getUserInfo(user1)); //False

Assert.assertNotEquals(user0,user1); //True

}

@Test
public void createUser(){
UserInformationController userInformationController = new UserInformationController();
this.userInformationHashMap = new HashMap<>();

UserInformation userInformation0 = new UserInformation("Troy","Green","tjg217@verizon.com","2012hummingbirds");
UserInformation userInformation1 = new UserInformation("Sierra", "West","themostimportantwest@msn.com","shadeyglasses77");

int user0 = userInformationController.createUser(userInformation0);//Can you tell me why this does not work
int user1 = userInformationController.createUser(userInformation1);//Can you tell me why this does not work

this.userInformationHashMap.put(user0, userInformation0);
this.userInformationHashMap.put(user1, userInformation1);

Assert.assertNotEquals(this.userInformationHashMap.get(user0),this.userInformationHashMap.get(user1)); //True

Assert.assertNotNull(this.userInformationHashMap.get(user0)); //False
}

@Test
public void updateUserInfo(){
UserInformationController userInformationController = new UserInformationController();
this.userInformationHashMap = new HashMap<>();

int userId = 0;
UserInformation userInformation = new UserInformation("Nicole", "Rigby", "sexygirl69@babellon.com","throwmethemoney");
UserInformation newUserInformation = new UserInformation("Kitty", "Morgan", "ilovecats@cats.com","cats");

this.userInformationHashMap.put(userId,userInformation);

Assert.assertEquals(this.userInformationHashMap.get(userId),userInformation); //True

userInformationController.updateUserInfo(userId,newUserInformation); //Can you tell me why this does not work

Assert.assertNotEquals(this.userInformationHashMap.get(userId),newUserInformation); //False
}

@Test
public void deleteUser(){
UserInformationController userInformationController = new UserInformationController();
this.userInformationHashMap = new HashMap<>();

int user = 0;
UserInformation userInformation = new UserInformation("Camryn","Resele","smartcookie@email.com","28564088");

this.userInformationHashMap.put(user,userInformation);

userInformationController.deleteUser(user);

Assert.assertNull(this.userInformationHashMap.get(user)); //True
Assert.assertTrue(this.userInformationHashMap.containsKey(user)); //False
}

}

用户信息 Controller

private HashMap<Integer,UserInformation> userInformationHashMap;


/**
* Default json constructor`enter code here`
* @return new user object
*/
@GetMapping(path = "/defaultUserInformation")
public UserInformation test()
{
return new UserInformation("fname", "lname", "email", "pass");
}
/**
* Gets the users information
* @return users information
*/
@GetMapping (path = "/userInfo")
public UserInformation getUserInfo(@RequestParam ("id") int id){
return userInformationHashMap.get(id);
}

/**
* Sets the users information
* @param userInformation userInformation model
* @return users key
*/
@PostMapping (path = "/createUser")
public int createUser(@RequestBody UserInformation userInformation){

if(this.userInformationHashMap == null){
this.userInformationHashMap = new HashMap<>();
}

int maxKey = 0;

if(this.userInformationHashMap.size() != 0){
maxKey = Collections.max(this.userInformationHashMap.keySet()) + 1;
}

this.userInformationHashMap.put(maxKey,userInformation);

return maxKey;
}

@PutMapping (path = "/updateUserInfo")
public void updateUserInfo(@RequestParam ("id") int id, @RequestBody UserInformation userInformation) {
if (this.userInformationHashMap.containsKey(id)) {
this.userInformationHashMap.put(id, userInformation);
}
}

@DeleteMapping (path = "/deleteUser")
public void deleteUser(@RequestParam ("id") int id){
this.userInformationHashMap.remove(id);
}

}

最佳答案

UserInformationController 中的

userInformationHashMap 从未初始化,这就是您收到 NullPointerException 的原因。

您正在 createUser 端点中初始化 HashMap,但在测试中从未调用它。

createUser 端点我看不出它在哪里失败,但无论如何,这段代码确实应该重新组织,因为它有很多失败点。 HashMap确实应该在创建 Bean 时初始化,并且您应该重新审视计算 Key 的方式。

此外,出于 Controller 测试目的,您应该使用 MockMvc而不是直接调用 Controller 方法。

关于java - 单元测试 Junit NullPointerException 和 NoSuchElement 有帮助!休息终点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48716692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com