test: improve unit test clarity

This commit is contained in:
Zhongheng Liu 2025-11-23 20:04:59 +01:00
commit 1e9bd169d0

View file

@ -3,15 +3,19 @@ package server.api;
import commons.Recipe;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ActiveProfiles;
import server.database.RecipeRepository;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.LongStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -28,7 +32,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class RecipeControllerTest {
private RecipeController controller;
private List<Recipe> recipes;
private RecipeRepository recipeRepository;
private final RecipeRepository recipeRepository;
private List<Long> recipeIds;
public static final int NUM_RECIPES = 10;
// Injects a test repository into the test class
@ -38,12 +43,29 @@ public class RecipeControllerTest {
}
@BeforeEach
public void setup() {
public void setup(TestInfo info) {
recipes = LongStream
.range(0, NUM_RECIPES)
.mapToObj(x -> new Recipe(null, "Recipe " + x, List.of(), List.of()))
.toList();
controller = new RecipeController(recipeRepository);
Set<String> tags = info.getTags();
List<Long> ids = new ArrayList<>();
// Some tests want initial data to be created.
if (tags.contains("test-from-init-data")) {
ids = LongStream
.range(0, NUM_RECIPES)
.map(idx -> recipeRepository.save(recipes.get((int) idx)).getId())
.boxed().toList();
}
// Some tests need to know the stored IDs of objects
if (tags.contains("need-ids")) {
recipeIds = ids;
}
// If no tags specified, the repository is initialized as empty.
}
@Test
public void createOneRecipe() {
@ -60,31 +82,27 @@ public class RecipeControllerTest {
assertEquals(recipes.size(), recipeRepository.count());
}
@Test
@Tag("test-from-init-data")
public void getManyRecipes() {
recipes.forEach(recipe -> controller.createRecipe(recipe));
// The number of recipes returned is the same as the entire input list
assertEquals(recipes.size(), controller.getRecipes(Optional.empty()).getBody().size());
}
@Test
@Tag("test-from-init-data")
public void getSomeRecipes() {
final int LIMIT = 5;
recipes.forEach(recipe -> controller.createRecipe(recipe));
// The number of recipes returned is the same as the entire input list
assertEquals(LIMIT, controller.getRecipes(Optional.of(LIMIT)).getBody().size());
}
@Test
@Tag("test-from-init-data")
@Tag("need-ids")
public void findOneRecipeExists() {
final int CHECK_INDEX = 3;
List<Long> ids = LongStream
.range(0, NUM_RECIPES)
.map(recipe -> controller.createRecipe(recipes.get((int) recipe)).getBody().getId())
.boxed().toList();
// The third item in the input list is the same as the third item retrieved from the database
assertEquals(
recipes.get(CHECK_INDEX),
controller.getRecipe(ids.get(CHECK_INDEX)).getBody());
controller.getRecipe(recipeIds.get(CHECK_INDEX)).getBody());
}
@Test
public void findOneRecipeNotExists() {
@ -95,26 +113,22 @@ public class RecipeControllerTest {
controller.getRecipe((long) CHECK_INDEX).getStatusCode());
}
@Test
@Tag("test-from-init-data")
@Tag("need-ids")
public void deleteOneRecipeGood() {
final int DELETE_INDEX = 5;
List<Long> ids = LongStream
.range(0, NUM_RECIPES)
.map(recipe -> controller.createRecipe(recipes.get((int) recipe)).getBody().getId())
.boxed().toList();
// The object has been successfully deleted
assertEquals(HttpStatus.OK, controller.deleteRecipe(ids.get(DELETE_INDEX)).getStatusCode());
assertEquals(HttpStatus.OK, controller.deleteRecipe(recipeIds.get(DELETE_INDEX)).getStatusCode());
}
@Test
@Tag("test-from-init-data")
@Tag("need-ids")
public void deleteOneRecipeCountGood() {
final int DELETE_INDEX = 5;
List<Long> ids = LongStream
.range(0, NUM_RECIPES)
.map(recipe -> controller.createRecipe(recipes.get((int) recipe)).getBody().getId())
.boxed().toList();
controller.deleteRecipe(ids.get(DELETE_INDEX));
controller.deleteRecipe(recipeIds.get(DELETE_INDEX));
// The count of items decreased by 1 after the 5th item has been removed.
assertEquals(ids.size() - 1, recipeRepository.count());
assertEquals(recipeIds.size() - 1, recipeRepository.count());
}
@Test
public void deleteOneRecipeFail() {
@ -123,15 +137,13 @@ public class RecipeControllerTest {
}
@Test
@Tag("test-from-init-data")
@Tag("need-ids")
public void updateOneRecipeHasNewData() {
final int UPDATE_INDEX = 5;
List<Long> ids = LongStream
.range(0, NUM_RECIPES)
.map(recipe -> controller.createRecipe(recipes.get((int) recipe)).getBody().getId())
.boxed().toList();
Recipe newRecipe = controller.getRecipe(ids.get(UPDATE_INDEX)).getBody();
Recipe newRecipe = controller.getRecipe(recipeIds.get(UPDATE_INDEX)).getBody();
newRecipe.setName("New recipe");
controller.updateRecipe(newRecipe.getId(), newRecipe);
assertEquals("New recipe", recipeRepository.getReferenceById(ids.get(UPDATE_INDEX)).getName());
assertEquals("New recipe", recipeRepository.getReferenceById(recipeIds.get(UPDATE_INDEX)).getName());
}
}