assertNull() is a built in JUint function to test your java programs before deploying the application on your production environment/server. In this tutorial, we are going to share the junit assertNull example.
Assert.assertNull() methods checks that the object is null or not. If it is not null then it throws an AssertionError.
Example 1. assertNull() Function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.java2novice.junit.tests; import java.util.HashMap; import java.util.Map; import static org.junit.Assert.*; import org.junit.Test; public class MyAssertNullTest { public String getPropValue(final String key){ Map<string, string=""> appProps = new HashMap<string, string="">(); appProps.put("key1", "value 1"); appProps.put("key2", "value 2"); appProps.put("key3", "value 3"); return appProps.get(key); } @Test public void test(){ MyAssertNullTest msnt = new MyAssertNullTest(); assertNotNull(msnt.getPropValue("key9")); } } |
Example 2. assertNull() Function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | private void testTruncate(List<LogEntryProto> entries, long fromIndex) throws Exception { try (SegmentedRaftLog raftLog = new SegmentedRaftLog(peerId, null, storage, -1, properties)) { raftLog.open(RaftServerConstants.INVALID_LOG_INDEX, null); // truncate the log raftLog.truncate(fromIndex).join(); checkEntries(raftLog, entries, 0, (int) fromIndex); } try (SegmentedRaftLog raftLog = new SegmentedRaftLog(peerId, null, storage, -1, properties)) { raftLog.open(RaftServerConstants.INVALID_LOG_INDEX, null); // check if the raft log is correct if (fromIndex > 0) { Assert.assertEquals(entries.get((int) (fromIndex - 1)), getLastEntry(raftLog)); } else { Assert.assertNull(raftLog.getLastEntryTermIndex()); } checkEntries(raftLog, entries, 0, (int) fromIndex); } } |
Example 3. assertNull() Function.
1 2 3 4 5 6 7 8 9 10 11 | @Override public void individualSearcherAssertions(CellSearcher searcher) { /** * The searcher should get a token mismatch on the "r" branch. Assert that it skips not only rA, * but rB as well. */ KeyValue afterLast = KeyValueUtil.createFirstOnRow(Bytes.toBytes("zzz")); CellScannerPosition position = searcher.positionAtOrAfter(afterLast); Assert.assertEquals(CellScannerPosition.AFTER_LAST, position); Assert.assertNull(searcher.current()); } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.