Я хочу научиться создавать модульные тесты.Я никогда не делал этого раньше, поэтому я прочитал учебник и попытался протестировать свой метод addTraining to Day.Но когда я сделал свой тестовый метод (я написал это правильно), у меня есть 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);
}
И это мой класс Test:
@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();
}
}