test: improve unit test clarity
This commit is contained in:
parent
fff5f2fffa
commit
1e9bd169d0
1 changed files with 40 additions and 28 deletions
|
|
@ -3,15 +3,19 @@ package server.api;
|
||||||
|
|
||||||
import commons.Recipe;
|
import commons.Recipe;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Tag;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.TestInfo;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
import server.database.RecipeRepository;
|
import server.database.RecipeRepository;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.stream.LongStream;
|
import java.util.stream.LongStream;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
@ -28,7 +32,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
public class RecipeControllerTest {
|
public class RecipeControllerTest {
|
||||||
private RecipeController controller;
|
private RecipeController controller;
|
||||||
private List<Recipe> recipes;
|
private List<Recipe> recipes;
|
||||||
private RecipeRepository recipeRepository;
|
private final RecipeRepository recipeRepository;
|
||||||
|
private List<Long> recipeIds;
|
||||||
public static final int NUM_RECIPES = 10;
|
public static final int NUM_RECIPES = 10;
|
||||||
|
|
||||||
// Injects a test repository into the test class
|
// Injects a test repository into the test class
|
||||||
|
|
@ -38,12 +43,29 @@ public class RecipeControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setup() {
|
public void setup(TestInfo info) {
|
||||||
recipes = LongStream
|
recipes = LongStream
|
||||||
.range(0, NUM_RECIPES)
|
.range(0, NUM_RECIPES)
|
||||||
.mapToObj(x -> new Recipe(null, "Recipe " + x, List.of(), List.of()))
|
.mapToObj(x -> new Recipe(null, "Recipe " + x, List.of(), List.of()))
|
||||||
.toList();
|
.toList();
|
||||||
controller = new RecipeController(recipeRepository);
|
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
|
@Test
|
||||||
public void createOneRecipe() {
|
public void createOneRecipe() {
|
||||||
|
|
@ -60,31 +82,27 @@ public class RecipeControllerTest {
|
||||||
assertEquals(recipes.size(), recipeRepository.count());
|
assertEquals(recipes.size(), recipeRepository.count());
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("test-from-init-data")
|
||||||
public void getManyRecipes() {
|
public void getManyRecipes() {
|
||||||
recipes.forEach(recipe -> controller.createRecipe(recipe));
|
|
||||||
|
|
||||||
// The number of recipes returned is the same as the entire input list
|
// The number of recipes returned is the same as the entire input list
|
||||||
assertEquals(recipes.size(), controller.getRecipes(Optional.empty()).getBody().size());
|
assertEquals(recipes.size(), controller.getRecipes(Optional.empty()).getBody().size());
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("test-from-init-data")
|
||||||
public void getSomeRecipes() {
|
public void getSomeRecipes() {
|
||||||
final int LIMIT = 5;
|
final int LIMIT = 5;
|
||||||
recipes.forEach(recipe -> controller.createRecipe(recipe));
|
|
||||||
// The number of recipes returned is the same as the entire input list
|
// The number of recipes returned is the same as the entire input list
|
||||||
assertEquals(LIMIT, controller.getRecipes(Optional.of(LIMIT)).getBody().size());
|
assertEquals(LIMIT, controller.getRecipes(Optional.of(LIMIT)).getBody().size());
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("test-from-init-data")
|
||||||
|
@Tag("need-ids")
|
||||||
public void findOneRecipeExists() {
|
public void findOneRecipeExists() {
|
||||||
final int CHECK_INDEX = 3;
|
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
|
// The third item in the input list is the same as the third item retrieved from the database
|
||||||
assertEquals(
|
assertEquals(
|
||||||
recipes.get(CHECK_INDEX),
|
recipes.get(CHECK_INDEX),
|
||||||
controller.getRecipe(ids.get(CHECK_INDEX)).getBody());
|
controller.getRecipe(recipeIds.get(CHECK_INDEX)).getBody());
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void findOneRecipeNotExists() {
|
public void findOneRecipeNotExists() {
|
||||||
|
|
@ -95,26 +113,22 @@ public class RecipeControllerTest {
|
||||||
controller.getRecipe((long) CHECK_INDEX).getStatusCode());
|
controller.getRecipe((long) CHECK_INDEX).getStatusCode());
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("test-from-init-data")
|
||||||
|
@Tag("need-ids")
|
||||||
public void deleteOneRecipeGood() {
|
public void deleteOneRecipeGood() {
|
||||||
final int DELETE_INDEX = 5;
|
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
|
// 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
|
@Test
|
||||||
|
@Tag("test-from-init-data")
|
||||||
|
@Tag("need-ids")
|
||||||
public void deleteOneRecipeCountGood() {
|
public void deleteOneRecipeCountGood() {
|
||||||
final int DELETE_INDEX = 5;
|
final int DELETE_INDEX = 5;
|
||||||
List<Long> ids = LongStream
|
controller.deleteRecipe(recipeIds.get(DELETE_INDEX));
|
||||||
.range(0, NUM_RECIPES)
|
|
||||||
.map(recipe -> controller.createRecipe(recipes.get((int) recipe)).getBody().getId())
|
|
||||||
.boxed().toList();
|
|
||||||
controller.deleteRecipe(ids.get(DELETE_INDEX));
|
|
||||||
// The count of items decreased by 1 after the 5th item has been removed.
|
// 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
|
@Test
|
||||||
public void deleteOneRecipeFail() {
|
public void deleteOneRecipeFail() {
|
||||||
|
|
@ -123,15 +137,13 @@ public class RecipeControllerTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Tag("test-from-init-data")
|
||||||
|
@Tag("need-ids")
|
||||||
public void updateOneRecipeHasNewData() {
|
public void updateOneRecipeHasNewData() {
|
||||||
final int UPDATE_INDEX = 5;
|
final int UPDATE_INDEX = 5;
|
||||||
List<Long> ids = LongStream
|
Recipe newRecipe = controller.getRecipe(recipeIds.get(UPDATE_INDEX)).getBody();
|
||||||
.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();
|
|
||||||
newRecipe.setName("New recipe");
|
newRecipe.setName("New recipe");
|
||||||
controller.updateRecipe(newRecipe.getId(), newRecipe);
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue