- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我访问类来测试方法时,每次尝试定义新对象并使用新定义的对象时都会遇到异常。
测试错误:
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/
我创建了一个读取这样的文件集的方法(//...是注释,忽略它们): 5 // n jobs 2 // n tools 1 4 5 6 2 1 5 4 2 3 矩阵代表每项工作使用的工具,但在这里并不重
我正在编写读取用户输入的代码,因此我编写了一个程序来执行此操作,读取并返回字符串。当从另一个过程调用它两次时,返回一个 NoSuchElementException 错误,我不知道为什么。 输入阅读器
I have declared two strings and reading the input using Scanner(System.in). 在此之后,当我关闭扫描仪并再次使用扫描仪读取另一
有人可以帮忙解释一下为什么会抛出 NoSuchElement 异常吗?它似乎发生在最后一行 scan.nextInt(); 我正在尝试从文件中读入名称并将它们排序到数组中,然后在打印后从用户那里读入一
对于一个类,我必须创建一个程序,该程序有一个菜单,用户可以根据自己的选择从菜单中选择运行某些方法。我遇到的问题是在我调用我的方法之后,程序在第 30 行抛出 NoSuchElement 异常(当我将代
我们正在一个大项目中使用 GWT。在某些情况下,此代码可以生成 NoSuchElementException : for (Object o: myList) { } 内部没有对ArrayList进行
我已经分享了下面的代码,请让我知道更正 import org.openqa.selenium.By; import org.openqa.selenium.ie.InternetExplorerDri
我有一个 JSP 页面,用户可以在其中选择多个复选框(最多 10 个)。有时,如果选中多个复选框,则会引发 NoSuchElement 异常。 Servlet 代码: Iterator iterat
这个问题已经被一遍又一遍地问了——尽管尝试了所有的技巧,但我似乎仍然无法弄清楚问题出在哪里。 我尝试将 implicitly_wait 增加到 30(甚至增加到 100)- 但它没有用。 用例 -:我
这个问题已经有答案了: Selenium "selenium.common.exceptions.NoSuchElementException" when using Chrome (1 个回答) 已
我正在尝试自动化网络数据表测试。我在用于定位的每个 WebElement 对象中都收到此 NullPointerException。我的代码尽可能简单: public class tableTestS
我有这个方法,应该从 txt 文件中读取,然后在交互 Pane 中打印出读取的信息。该方法有效,我获得了文件中的所有数据,但最终我还打印出了 java.util.NoSuchElementExcept
我试图在程序中使用 Scanner 再次从控制台获取输入,但是当在另一种方法中调用第二个 Scanner 时,它会出现 NoSuchElement异常(exception)。 如果我从运行 fileR
出于某种原因,在 Selenium WebDriver 中单击链接对我来说似乎不起作用,而在 Selenium IDE 中却工作得很好。 我在 Selenium IDE 上构建到 Selenium W
我有以下代码用于从给定列表中选择一个选项,它通常有效,但有时它会失败并在第二个 if 上出现 NoSuchElement 异常。我的印象是,如果它找不到元素,它就会再次返回循环。我相信解释很简单...
我目前有以下代码 while (!visibleTiles.isEmpty())) { tile = visibleTiles.keySet().iterator().next();
我正在编写一个使用 Selenium 的代码。为了测试 find_element_by_css_selector 函数,我编写了以下代码: self.browserProfile = webdrive
我的 Java 1.7 程序执行各种数学函数。我已将函数分成方法,它们可以单独工作。我还制作了一个提示方法,询问用户是否要继续。但是,在输入数字后,我不断收到 NoSuchElement 异常。打印“
当我访问类来测试方法时,每次尝试定义新对象并使用新定义的对象时都会遇到异常。 测试错误: UserInformationControllerTest.deleteUser:83 » NullPoi
我必须创建一个程序,对用户进行 10 个多项选择题的测验,这些问题必须从 .txt 文件读入主类。 到目前为止,为了做到这一点,我已使用 while 循环(在 hasNextLine 条件下)将 .t
我是一名优秀的程序员,十分优秀!