nico43624
nico43624

Reputation: 29

ArrayLists With Zero and Two Outcomes for Junit Tests

I need help with this piece of code. I need to write a test just like testfindOverviewByStatus() example in the code below but I need to write it for 2 pieces of data and 0 pieces. testfindOverviewByStatus() is a test for if there is only one piece of data in the list. I tried writing the two different tests I need at the end of the code but was unsuccessful in doing so. Can anyone help me?

package com.primatics.greensight.dashboard.controller;

import static org.junit.Assert.assertEquals;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;

import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import com.primatics.greensight.dashboard.UnitTest;
import com.primatics.greensight.dashboard.model.Overview;
import com.primatics.greensight.dashboard.repositories.ScenarioDashboardRepository;
import com.primatics.greensight.dashboard.repositories.impl.ScenarioDashboardRepositoryImpl;
import com.primatics.greensight.dashboard.services.ScenarioDashboardService;
import com.primatics.greensight.dashboard.services.impl.ScenarioDashboardServiceImpl;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ScenarioDashboardServiceImpl.class)
@AutoConfigureMockMvc
@EnableAutoConfiguration
@Category(UnitTest.class)
public class ScenarioDashboardServiceImplUnitTest {

    @Configuration
    static class AccountServiceTestContextConfiguration {
        @Bean
        public ScenarioDashboardRepositoryImpl scenarioDashboardService() {
            return new ScenarioDashboardRepositoryImpl();
        }

        @Bean
        public ScenarioDashboardRepository scenarioDashboardtRepository() {
            return Mockito.mock(ScenarioDashboardRepositoryImpl.class);
        }
    }

    @Mock
    private ScenarioDashboardService scenarioDashboardService;

    @Before
    public void doSetUp() {
        List<Overview> scenarioListTest = new ArrayList<Overview>();

        String name = "first_scenario-test";
        ISO8601DateFormat df = new ISO8601DateFormat();
        Date estimationDate = null;
        try {
            estimationDate = df.parse("2017-01-28T22:25:51Z");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date creationDate = null;
        try {
            creationDate = df.parse("2017-02-28T22:25:51Z");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Double balance = 131750000.0;
        Double individualReviewImpairment = 1000.00;
        Map<String, Double> baseline = new HashMap<String, Double>();
        baseline.put("complete", 1000.0);

        Map<String, Double> macroAdjustment = new HashMap<String, Double>();
        macroAdjustment.put("complete", 2000.0);

        Map<String, Double> qualitativeAdjustment = new HashMap<String, Double>();
        qualitativeAdjustment.put("complete", 3000.0);

        Date positionDate = null;
        try {
            positionDate = df.parse("2017-01-28T22:25:51Z");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date lossHistoryDate = null;
        try {
            lossHistoryDate = df.parse("2017-01-28T22:25:51Z");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String status = "active";

        Map<String, Integer> period = new HashMap<String, Integer>();
        period.put("Q1", 2017);

        boolean publish = true;

        Overview ac = new Overview(4, name, estimationDate, creationDate, balance, individualReviewImpairment, baseline,
                macroAdjustment, qualitativeAdjustment, positionDate, lossHistoryDate, status, period, publish);

        scenarioListTest.add(ac);

        Mockito.when(scenarioDashboardService.getOverviewByStatus("active")).thenReturn(scenarioListTest);

    }

    @Before
    public void getTwoOverviews() {

        List<Overview> scenarioListTest2 = new ArrayList<Overview>();

        String name = "first_scenario-test";
        ISO8601DateFormat df = new ISO8601DateFormat();
        Date estimationDate = null;
        try {
            estimationDate = df.parse("2017-01-28T22:25:51Z");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date creationDate = null;
        try {
            creationDate = df.parse("2017-02-28T22:25:51Z");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Double balance = 131750000.0;
        Double individualReviewImpairment = 1000.00;
        Map<String, Double> baseline = new HashMap<String, Double>();
        baseline.put("complete", 1000.0);

        Map<String, Double> macroAdjustment = new HashMap<String, Double>();
        macroAdjustment.put("complete", 2000.0);

        Map<String, Double> qualitativeAdjustment = new HashMap<String, Double>();
        qualitativeAdjustment.put("complete", 3000.0);

        Date positionDate = null;
        try {
            positionDate = df.parse("2017-01-28T22:25:51Z");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date lossHistoryDate = null;
        try {
            lossHistoryDate = df.parse("2017-01-28T22:25:51Z");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String status = "active";

        Map<String, Integer> period = new HashMap<String, Integer>();
        period.put("Q1", 2017);

        boolean publish = true;

        Overview ac = new Overview(4, name, estimationDate, creationDate, balance, individualReviewImpairment, baseline,
                macroAdjustment, qualitativeAdjustment, positionDate, lossHistoryDate, status, period, publish);

        scenarioListTest2.add(ac);

        Mockito.when(scenarioDashboardService.getOverviewByStatus("active")).thenReturn(scenarioListTest2);
    }

    @Test
    public void testfindOverviewByStatus() {
        List<Overview> scenarioListTest = scenarioDashboardService.getOverviewByStatus("active");
        assertEquals(1, scenarioListTest.size());

        String scenarioName = scenarioListTest.get(0).getScenarioName();
        assertEquals("first_scenario-test", scenarioName);
    }

    @Test
    public void testfindOverviewByStatusTwo() {

        List<Overview> scenarioList = scenarioDashboardService.getOverviewByStatus("active");
        assertEquals(2, scenarioList.size());

        String scenarioName = scenarioList.get(0).getScenarioName();
        String scenarioName2 = scenarioList.get(1).getScenarioName();
        assertEquals("first_scenario-test", scenarioName);
        assertEquals("second_scenario-test", scenarioName2);
    }

    @Test
    public void testfindOverviewByStatusNull() {

        List<Overview> scenarioListNull = scenarioDashboardService.getOverviewByStatus("active");
        assertEquals(0, scenarioListNull.size());
    }
}

Upvotes: 0

Views: 106

Answers (2)

Paul Benn
Paul Benn

Reputation: 2031

In situations where Mockito is already a dependency you might find it easier to use a custom argument matcher for this:

private class isListOfSize extends ArgumentMatcher<List<Overview>> {
    int size;

    isListOfSize(int size) {
        this.size = size;
    }

    @Override
    public boolean matches(Object list) {
        return ((List) list).size() == size;
    }
}

Usage:

assertThat(scenarioList, argThat(new isListOfSize(2)))

Upvotes: 0

Garreth Golding
Garreth Golding

Reputation: 1005

The problem is with the layout of your test. You have multiple @Before methods which all mock scenarioDashboardService.getOverviewByStatus("active")

The simplest way to get your tests working would be to remove the @Before annotations and instead invoke them methods in the @Test method.

So for example:

public void getTwoOverviews() {
    //Contents of method
}

@Test
public void testfindOverviewByStatusTwo() {
    //Setup
    getTwoOverviews();

    List<Overview> scenarioList = scenarioDashboardService.getOverviewByStatus("active");
    assertEquals(2, scenarioList.size());

    String scenarioName = scenarioList.get(0).getScenarioName();
    String scenarioName2 = scenarioList.get(1).getScenarioName();
    assertEquals("first_scenario-test", scenarioName);
    assertEquals("second_scenario-test", scenarioName2);
}

As I mentioned this would be the simplest way but in the long run there most likely is a better way to layout your test class.

Upvotes: 1

Related Questions