0

I am currently stuck trying to create a unit test for this piece of code I have. I honestly can't figure out at all how to create a unit test for these lines of code. I have looked multiple places online and couldn't find anything. Its probably just because I don't understand unit test so I can't figure out how to create this one but could someone help me please?

public List<Overview> findOverviewByStatus(String status) throws CustomMongoException {

        List<Overview> scenarioList = new ArrayList<Overview>();
        LOGGER.info("Getting Scenario Summary Data for - {}", status);
        Query query = new Query(Criteria.where("status").is(status));

        if (mongoTemplate == null)
            throw new CustomMongoException("Connection issue - Try again in a few minutes",
                    HttpStatus.FAILED_DEPENDENCY);
        LOGGER.info("Running  Query - {}", query);
        scenarioList = mongoTemplate.find(query.with(new Sort(Sort.Direction.DESC, "lastUpdatedDate")), Overview.class);
        return scenarioList;
    }
nico43624
  • 29
  • 6
  • What are you trying to test? Start with the basics: What are you expecting this method to do and what do expect it to consume? I.e. if you pass a valid status to the method, what do you expect the resulting `List` to look like? What happens if you pass in different `status` values? Then work on some corner-cases: What happens is `status` is `null` or an empty string? – Justin Albano Apr 09 '18 at 12:30
  • Possible duplicate of [Assert about a List in Junit](https://stackoverflow.com/questions/3236880/assert-about-a-list-in-junit) – Cemal Apr 09 '18 at 12:41

1 Answers1

0

So you want to unit test the method. Start with pretending you don't know what the code looks like (black box testing).

What happens if you call it with status of null, and then status of empty string?
What are some status string that return expected values?

Add all these as asserts to your test method to make sure that if someone changes this method in the future the unit test makes sure that it returns the expected result.

That is all a unit test usually does, makes sure that the code behaves in a predictable way and safeguard against change that violates a contract you created for the method when you wrote it.

For example:

import org.junit.Assert;
import org.junit.Test;

public class MyObjectTest {
    @Test
    public void testMyObjectMethod() {
        // Create the object that contains your method (not in the sample you provided)
        MyObjectToTest obj = new MyObjectToTest();

        // Check that for a null status you get some result (assuming you want this)
        Assert.assertNotNull(obj.findOverviewByStatus(null));

        // Lets assume that a null status returns an empty array, add a check for it
        Assert.assertTrue("null parameter size should be 0", obj.findOverviewByStatus(null).size() == 0);

        //etc...
    }
}
AlexC
  • 1,395
  • 14
  • 26
  • Thanks this did help a lot. Yeah I needed a positive test case and then a negative test case which is null. This helps a lot though. Thanks! – nico43624 Apr 10 '18 at 18:18