gpt4 book ai didi

JUNIT 测试时出现 java.lang.StackOverflowError

转载 作者:行者123 更新时间:2023-11-28 21:07:56 25 4
gpt4 key购买 nike

我想学习如何创建单元测试。我以前从未这样做过,所以我阅读了一些教程并尝试测试我的 addTraining to Day 方法。但是当我做我的测试方法时(imo 我写的是正确的方式)我有 java.lang.StackOverflowError。有人可以帮我吗?我不知道该怎么办。

这是我要测试的方法,我想我必须创建三个案例来测试它对吗?

    public void addTraining(String userId, Training training, LocalDate localDate) {
Day activityBoardDay = getActivityBoardDay(localDate, userId);
List<Training> trainingList = activityBoardDay.getTrainings();
if (trainingList == null || trainingList.isEmpty()) {
activityBoardDay.setTrainings(Collections.singletonList(training));
} else {
trainingList.add(training);
activityBoardDay.setTrainings(trainingList);
}
activityBoardDay.setTrainings(trainingList);
}

这是我的测试类:

@RunWith(MockitoJUnitRunner.class)
public class ActivitiesServiceTest {


@Mock
private DbService databaseControllerMock;


private ActivitiesService activitiesService;

private String userId = "test";


@Before
public void setUp() throws Exception {
activitiesService = new ActivitiesService(databaseControllerMock);
}

@After
public void tearDown() throws Exception {
getDayWithNulls().setTrainings(null);
getDayWithNulls().setMeasurements(null);
getDayWithNulls().setTrainingCycles(null);
getDayWithNulls().setDiets(null);
getDayWithNulls().setMeals(null);
getDayWithNulls().setLocalDate(LocalDate.now());
}


@Test
public void addTraining() {
//WITH NULLS
getDayWithNulls();
activitiesService.addTraining(userId, getTrainingForTest(), LocalDate.now());
Assert.assertEquals(getDayWithNulls(), expectedDayWithAddedTrainingToNulls());

}


private Exercise getSquatExcercise() {
Exercise exercise = new Exercise();
exercise.setId("1");
exercise.setName("Squat");
exercise.setDescription("How to squat properly?");
exercise.setCalories(3);
exercise.setNotes("Calories per one squat");
return exercise;
}

private Exercise getRunExcercise() {
Exercise exercise = new Exercise();
exercise.setId("2");
exercise.setName("Run");
exercise.setDescription("How to run properly?");
exercise.setCalories(3);
exercise.setNotes("Calories per one running session");
return exercise;
}

private Exercise getWalkExcercise() {
Exercise exercise = new Exercise();
exercise.setId("3");
exercise.setName("Walk");
exercise.setDescription("How to walk properly?");
exercise.setCalories(3);
exercise.setNotes("Calories per one walking session");
return exercise;
}

private Training getTrainingForTest() {
Training training = new Training();
List<Exercise> exercises = new ArrayList<>();
exercises.add(getRunExcercise());
exercises.add(getSquatExcercise());
exercises.add(getWalkExcercise());
training.setExerciseList(exercises);
return training;
}

private Day getDayWithNulls() {
getDayWithNulls().setTrainings(null);
getDayWithNulls().setMeasurements(null);
getDayWithNulls().setTrainingCycles(null);
getDayWithNulls().setDiets(null);
getDayWithNulls().setMeals(null);
getDayWithNulls().setLocalDate(LocalDate.now());
return getDayWithNulls();
}

private Day expectedDayWithAddedTrainingToNulls() {
expectedDayWithAddedTrainingToNulls().setTrainings(Collections.singletonList(getTrainingForTest()));
expectedDayWithAddedTrainingToNulls().setMeasurements(null);
expectedDayWithAddedTrainingToNulls().setTrainingCycles(null);
expectedDayWithAddedTrainingToNulls().setDiets(null);
expectedDayWithAddedTrainingToNulls().setMeals(null);
expectedDayWithAddedTrainingToNulls().setLocalDate(LocalDate.now());
return expectedDayWithAddedTrainingToNulls();
}
}

最佳答案

getDayWithNulls() 正在调用自身,这会导致无限递归和 StackOverflowErrorgetDayWithNulls() 可能应该调用一些其他方法来生成它返回的 Day 实例。

private Day getDayWithNulls() {
Day day = ... // call some method to create a Day instance,
// but don't call getDayWithNulls() !

day.setTrainings(null);
day.setMeasurements(null);
day.setTrainingCycles(null);
day.setDiets(null);
day.setMeals(null);
day.setLocalDate(LocalDate.now());

return day;
}

关于JUNIT 测试时出现 java.lang.StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50468480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com