I am getting false positive for my test. I am looking for the key, "1234", and I know it does not exist but the test still passes.
I have MAP method (reportUIDsAndTitles) that stores a bunch of keys and values for UIDs and Titles. My test loops through these keys and if it finds it asserts that the title matches the key. But in the code below if it does not find the key the test still passes.
How do I fix this? I have verified that my MAP method works and the loop works, but not sure how to set the failure.
@Test
public void apiTitleOfReport() {
Map<String, String> pairs;
pairs = reportUIDsAndTitles();
for (Map.Entry pairEntry : pairs.entrySet()) {
if (pairEntry.getKey().equals("1234")) {
String keyValue = (String) pairEntry.getValue();
assertEquals(keyValue, "Name of title I am looking for", "Report Title not found.");
}}}
******* Here is my updated code with the missing Assert before the IF block as suggested. Now if the key does not exist it fails. But if it does exist the code continues and checks that the title is correct. Thank you all for you suggestions.
@Test
public void apiTitleOfReport() {
Map<String, String> pairs;
pairs = reportUIDsAndTitles();
assertTrue(pairs.containsKey("1234"));
for (Map.Entry pairEntry : pairs.entrySet()) {
if (pairEntry.getKey().equals("1234")) {
String key = (String) pairEntry.getValue();
assertEquals(key, "Name of title I am looking for", "Report Title not found.");
}}}