Java Junit测试具有零和两个结果的ArrayList
我需要这段代码的帮助。我需要编写一个测试,就像下面代码中的TestFindVerviewByStatus示例一样,但我需要为2个数据段和0个数据段编写测试。TestFindVerviewByStatus用于测试列表中是否只有一条数据。我试图在代码末尾编写我需要的两个不同的测试,但没有成功。有人能帮我吗Java Junit测试具有零和两个结果的ArrayList,java,arraylist,junit,junit4,Java,Arraylist,Junit,Junit4,我需要这段代码的帮助。我需要编写一个测试,就像下面代码中的TestFindVerviewByStatus示例一样,但我需要为2个数据段和0个数据段编写测试。TestFindVerviewByStatus用于测试列表中是否只有一条数据。我试图在代码末尾编写我需要的两个不同的测试,但没有成功。有人能帮我吗 package com.primatics.greensight.dashboard.controller; import static org.junit.Assert.assertEqual
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());
}
}
问题在于测试的布局。您有多个@Before方法,这些方法都是模拟scenarioDashboardService.getOverviewByStatusactive的 让测试正常工作的最简单方法是删除@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);
}
正如我所提到的,这是最简单的方法,但从长远来看,很可能有更好的方法来布局测试类。在Mockito已经是依赖项的情况下,您可能会发现使用自定义参数匹配器更容易实现这一点:
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;
}
}
尝试过这样做,但返回的值仍然是not,这可能是因为在GettWoOverview中,您只向列表添加了一个元素,即只调用了一次add方法。scenarioListTest2.addac;因此,为了解决这个问题,我尝试将一个名为scenarioListTest的新列表添加到getTwoOverviews函数中,然后在函数的末尾放入scenarioListTest.addac并添加了另一个mockito语句,但使用scenarioListTest而不是scenarioListTest2。这仍然不起作用。我对java和junit测试非常陌生,所以我不太确定如何修复这个问题。不需要在这个方法中添加另一个List对象。如果您希望在测试中有2个元素,那么只需要向scenarioListTest2添加2个元素。很高兴这有帮助。由于这个解决方案有效,你能接受答案吗?不知道为什么,但我用来编写Spring Boot代码的程序根本不接受这个代码。他说这不够笼统,不可能出现在辩论中。
assertThat(scenarioList, argThat(new isListOfSize(2)))