英文:
how to add junit testcase for null hashmap
问题
以下是翻译好的部分:
我有一个哈希映射的代码片段:
如果(defgmap==null){
返回Collections.emptylist();
如何为相同的空检查添加JUnit测试用例/使用Mockito对象?由于我对此不熟悉,任何线索都将有所帮助。
使用assertnull()如何?
请注意,代码部分已被保留为英文。
英文:
I have below code snippet of hashmap
if (defgmap==null){
return Collections.emptylist();
}
How can we add junit test case / using mockito object for the same null check .As I am new to this, any leads would be helpful.
What about using assertnull() ?
答案1
得分: 2
-
期望地图必须为空:
Assert.assertNull("地图应该为null", map);
如果地图不为null,此测试应该失败。
-
期望返回的列表必须为空:
Assert.assertEquals("返回的列表应该为空", Collections.emptyList(), returnedList);
你不能将Assert.*方法用作if语句。
如果你不知道要使用哪个方法,这是一个通用示例:
// 条件是一个布尔表达式或变量
Assert.assertTrue("条件应该为真", condition);
Assert.assertFalse("条件应该为假", condition);
英文:
The key is what result you expect.
-
Expect the map must be null:
Assert.assertNull("The map should be null", map);
If the map is not null, this test should failed.
-
Expect the returned list must be empty:
Assert.assertEquals("The returned list should be empty", Collections.emptyList(), returnedList);
You can't use Assert.* methods as if-statement.
If you don't know which method to use, here is a generic example:
// condition is a boolean expression or variable
Assert.assertTrue("The condition should be true", condition);
Assert.assertFalse("The condition should be false", condition);
答案2
得分: 1
Sample是类名,test是方法名。
public List<String> test(List<String> request)
{
if (request==null){
return Collections.emptyList();
}
return request;
}
@Test
public void test()
{
List<String> result = sample.test(null);
Assert.assertEquals(new ArrayList<>(), result);
}
英文:
Sample is the class name and test is the method name.
public List<String> test(List<String> request)
{
if (request==null){
return Collections.emptyList();
}
return request;
}
@Test
public void test()
{
List<String> result = sample.test(null);
Assert.assertEquals(new ArrayList<>(), result);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论