简体   繁体   English

单元测试Junit NullPointerException和NoSuchElement帮助! 其余端点

[英]Unit Tests Junit NullPointerException and NoSuchElement help! Rest endpoints

When I am accessing my class to test the methods, I am getting exceptions every time I try to define a new object and use the newly defined object. 当我访问类以测试方法时,每次尝试定义新对象并使用新定义的对象时都会遇到异常。

Tests in error: 错误测试:

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

My assignment is to make 4 happy cases and 4 unhappy cases for each class 我的任务是每节课做4个快乐案例和4个不快乐案例

I am thoroughly confused. 我很困惑。

My test class for UserInformation 我的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
}

} }

UserInformationController UserInformationController

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);
}

} }

userInformationHashMap within UserInformationController is never initialized, this is why you're getting the NullPointerException s. UserInformationController userInformationHashMap从未初始化,这就是为什么要获取NullPointerException的原因。

You're initializing the HashMap in the createUser endpoint and it's never being called within the test. 您正在createUser端点中初始化HashMap,并且永远不会在测试中调用它。

The createUser endpoint I can't see where it's failing, but anyway this code should really be reorganized because it has many points of failure. 我看不到createUser终结点在哪里出现故障,但是无论如何,此代码应该真正进行重组,因为它存在很多故障点。 The HashMap should really be initialized when the Bean is created, and you should revisit the way you are calculating the Key. 在创建Bean时,应该真正初始化HashMap ,并且应该重新访问计算Key的方式。

Also for Controller testing purposes, you should be using MockMvc instead of calling controller methods directly. 同样出于控制器测试的目的,您应该使用MockMvc而不是直接调用控制器方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM