具有零和两个结果的ArrayLists用于Junit测试

时间:2018-05-01 14:58:26

标签: java arraylist junit junit4

我需要这段代码的帮助。我需要像下面的代码中的testfindOverviewByStatus()示例一样编写测试,但我需要为2个数据和0个部分编写它。 testfindOverviewByStatus()是对列表中是否只有一个数据的测试。我尝试在代码结束时编写我需要的两个不同的测试但是没有成功。任何人都可以帮助我吗?

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

2 个答案:

答案 0 :(得分:1)

问题在于测试的布局。您有多个@Before方法,都是模拟scenarioDashboardService.getOverviewByStatus("active")

让测试工作的最简单方法是删除@Before注释,而是在@Test方法中调用它们。

例如:

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

正如我所提到的,这将是最简单的方法,但从长远来看,最有可能是布局测试类的更好方法。

答案 1 :(得分:0)

在Mockito已经是依赖项的情况下,您可能会发现使用自定义参数匹配器更容易:

{path}

用法:

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