user14065864
user14065864

Reputation:

how to add junit testcase for null hashmap

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() ?

Upvotes: 0

Views: 3313

Answers (2)

Tsing
Tsing

Reputation: 119

The key is what result you expect.

  1. Expect the map must be null:

    Assert.assertNull("The map should be null", map);
    

    If the map is not null, this test should failed.

  2. 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);

Upvotes: 2

Anto Livish A
Anto Livish A

Reputation: 332

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

Upvotes: 1

Related Questions