Home » Java » java.lang.StackOverflowError while JUNIT testing

java.lang.StackOverflowError while JUNIT testing

Posted by: admin June 30, 2018 Leave a comment

Questions:

I want to learn how to create unit tests. I’ve never done this before so I read some tutorial and I tried to test my addTraining to Day method. But when I did my test method (imo I wrote this right way) I have java.lang.StackOverflowError. Can someone help me with that? I dont know what to do.

This is method I want to test, I think I have to create three cases to test this right?

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

And this is my Test class:

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