From 4bbf3cf4bd6fde907d9df011b83b1e8fd20fc014 Mon Sep 17 00:00:00 2001 From: JesseBrault0709 <62299747+JesseBrault0709@users.noreply.github.com> Date: Mon, 1 Jul 2024 10:01:21 +0200 Subject: [PATCH] Moved integration tests to integrationTest suite. --- build.gradle | 37 +++++++- .../api/MealsMadeEasyApiApplicationTests.java | 3 +- .../api/auth/AuthControllerTests.java | 0 .../api/recipe/RecipeRepositoryTests.java | 0 .../api/signup/SignUpControllerTests.java | 8 +- .../resources/application.properties | 0 .../api/recipe/RecipeServiceTests.java | 86 ------------------- 7 files changed, 42 insertions(+), 92 deletions(-) rename src/{test => integrationTest}/java/app/mealsmadeeasy/api/MealsMadeEasyApiApplicationTests.java (88%) rename src/{test => integrationTest}/java/app/mealsmadeeasy/api/auth/AuthControllerTests.java (100%) rename src/{test => integrationTest}/java/app/mealsmadeeasy/api/recipe/RecipeRepositoryTests.java (100%) rename src/{test => integrationTest}/java/app/mealsmadeeasy/api/signup/SignUpControllerTests.java (95%) rename src/{test => integrationTest}/resources/application.properties (100%) delete mode 100644 src/test/java/app/mealsmadeeasy/api/recipe/RecipeServiceTests.java diff --git a/build.gradle b/build.gradle index 30bce49..7a24ce8 100644 --- a/build.gradle +++ b/build.gradle @@ -17,6 +17,32 @@ repositories { mavenCentral() } +sourceSets { + testFixtures { + compileClasspath += main.output + } + + integrationTest { + compileClasspath += main.output + testFixtures.output + runtimeClasspath += main.runtimeClasspath + testFixtures.runtimeClasspath + } +} + +configurations { + testFixturesImplementation { + extendsFrom implementation + } + + integrationTestImplementation { + extendsFrom implementation + extendsFrom testImplementation + } + + integrationTestRuntimeOnly { + extendsFrom testRuntimeOnly + } +} + dependencies { // From Spring Initalizr implementation 'org.springframework.boot:spring-boot-starter-data-jpa' @@ -39,8 +65,17 @@ dependencies { // Custom testing testRuntimeOnly 'com.h2database:h2' + + testFixturesImplementation 'org.hamcrest:hamcrest:2.2' } -tasks.named('test') { +tasks.register('integrationTest', Test) { + description = 'Run integration tests.' + group = 'verification' + testClassesDirs = sourceSets.integrationTest.output.classesDirs + classpath = sourceSets.integrationTest.runtimeClasspath +} + +tasks.withType(Test).configureEach { useJUnitPlatform() } diff --git a/src/test/java/app/mealsmadeeasy/api/MealsMadeEasyApiApplicationTests.java b/src/integrationTest/java/app/mealsmadeeasy/api/MealsMadeEasyApiApplicationTests.java similarity index 88% rename from src/test/java/app/mealsmadeeasy/api/MealsMadeEasyApiApplicationTests.java rename to src/integrationTest/java/app/mealsmadeeasy/api/MealsMadeEasyApiApplicationTests.java index afa6f3c..6bd1f7f 100644 --- a/src/test/java/app/mealsmadeeasy/api/MealsMadeEasyApiApplicationTests.java +++ b/src/integrationTest/java/app/mealsmadeeasy/api/MealsMadeEasyApiApplicationTests.java @@ -7,7 +7,6 @@ import org.springframework.boot.test.context.SpringBootTest; class MealsMadeEasyApiApplicationTests { @Test - void contextLoads() { - } + void contextLoads() {} } diff --git a/src/test/java/app/mealsmadeeasy/api/auth/AuthControllerTests.java b/src/integrationTest/java/app/mealsmadeeasy/api/auth/AuthControllerTests.java similarity index 100% rename from src/test/java/app/mealsmadeeasy/api/auth/AuthControllerTests.java rename to src/integrationTest/java/app/mealsmadeeasy/api/auth/AuthControllerTests.java diff --git a/src/test/java/app/mealsmadeeasy/api/recipe/RecipeRepositoryTests.java b/src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeRepositoryTests.java similarity index 100% rename from src/test/java/app/mealsmadeeasy/api/recipe/RecipeRepositoryTests.java rename to src/integrationTest/java/app/mealsmadeeasy/api/recipe/RecipeRepositoryTests.java diff --git a/src/test/java/app/mealsmadeeasy/api/signup/SignUpControllerTests.java b/src/integrationTest/java/app/mealsmadeeasy/api/signup/SignUpControllerTests.java similarity index 95% rename from src/test/java/app/mealsmadeeasy/api/signup/SignUpControllerTests.java rename to src/integrationTest/java/app/mealsmadeeasy/api/signup/SignUpControllerTests.java index cbcb371..701a555 100644 --- a/src/test/java/app/mealsmadeeasy/api/signup/SignUpControllerTests.java +++ b/src/integrationTest/java/app/mealsmadeeasy/api/signup/SignUpControllerTests.java @@ -12,11 +12,11 @@ import org.springframework.http.MediaType; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import java.util.Map; import static org.hamcrest.Matchers.containsString; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -37,12 +37,13 @@ public class SignUpControllerTests { private MockHttpServletRequestBuilder getCheckUsernameRequest(String usernameToCheck) throws JsonProcessingException { final Map body = Map.of("username", usernameToCheck); - return get("/sign-up/check-username") + return MockMvcRequestBuilders.get("/sign-up/check-username") .content(this.objectMapper.writeValueAsString(body)) .contentType(MediaType.APPLICATION_JSON); } @Test + @DirtiesContext public void checkUsernameExpectAvailable() throws Exception { this.mockMvc.perform(this.getCheckUsernameRequest("isAvailable")) .andExpect(status().isOk()) @@ -61,12 +62,13 @@ public class SignUpControllerTests { private MockHttpServletRequestBuilder getCheckEmailRequest(String emailToCheck) throws JsonProcessingException { final Map body = Map.of("email", emailToCheck); - return get("/sign-up/check-email") + return MockMvcRequestBuilders.get("/sign-up/check-email") .content(this.objectMapper.writeValueAsString(body)) .contentType(MediaType.APPLICATION_JSON); } @Test + @DirtiesContext public void checkEmailExpectAvailable() throws Exception { this.mockMvc.perform(this.getCheckEmailRequest("available@available.com")) .andExpect(status().isOk()) diff --git a/src/test/resources/application.properties b/src/integrationTest/resources/application.properties similarity index 100% rename from src/test/resources/application.properties rename to src/integrationTest/resources/application.properties diff --git a/src/test/java/app/mealsmadeeasy/api/recipe/RecipeServiceTests.java b/src/test/java/app/mealsmadeeasy/api/recipe/RecipeServiceTests.java deleted file mode 100644 index 668b0b7..0000000 --- a/src/test/java/app/mealsmadeeasy/api/recipe/RecipeServiceTests.java +++ /dev/null @@ -1,86 +0,0 @@ -package app.mealsmadeeasy.api.recipe; - -import app.mealsmadeeasy.api.user.User; -import app.mealsmadeeasy.api.user.UserEntity; -import app.mealsmadeeasy.api.user.UserRepository; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.annotation.DirtiesContext; - -import java.util.List; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; - -@SpringBootTest -public class RecipeServiceTests { - - @Autowired - private RecipeService recipeService; - - @Autowired - private UserRepository userRepository; - - private UserEntity createTestUser(String username) { - final UserEntity draft = UserEntity.getDefaultDraft(); - draft.setUsername(username); - draft.setEmail(username + "@test.com"); - draft.setPassword("test"); - return this.userRepository.save(draft); - } - - private Recipe createTestRecipe(User owner) throws RecipeException { - return this.recipeService.create(owner.getUsername(), "My Recipe" , "Hello!"); - } - - @Test - @DirtiesContext - public void simpleCreate() throws RecipeException { - final User user = this.createTestUser("recipeOwner"); - final Recipe recipe = this.recipeService.create(user.getUsername(), "My Recipe" , "Hello!"); - assertThat(recipe.getOwner(), is(user)); - assertThat(recipe.getTitle(), is("My Recipe")); - assertThat(recipe.getRawText(), is("Hello!")); - } - - @Test - @DirtiesContext - public void simpleGetById() throws RecipeException { - final Recipe testRecipe = this.createTestRecipe(this.createTestUser("recipeOwner")); - final Recipe byId = this.recipeService.getById(testRecipe.getId()); - assertThat(byId.getId(), is(testRecipe.getId())); - assertThat(byId.getTitle(), is("My Recipe")); - assertThat(byId.getRawText(), is("Hello!")); - } - - @Test - @DirtiesContext - public void getByMinimumStars() throws RecipeException { - final User owner = this.createTestUser("recipeOwner"); - final User u0 = this.createTestUser("u0"); - final User u1 = this.createTestUser("u1"); - - Recipe r0 = this.createTestRecipe(owner); - Recipe r1 = this.createTestRecipe(owner); - Recipe r2 = this.createTestRecipe(owner); - - r0 = this.recipeService.setPublic(r0, true); - r1 = this.recipeService.setPublic(r1, true); - r2 = this.recipeService.setPublic(r2, true); - - // r0.stars = 0, r1.stars = 1, r2.stars = 2 - this.recipeService.addStar(r1, u0); - this.recipeService.addStar(r2, u0); - this.recipeService.addStar(r2, u1); - - final List zeroStars = this.recipeService.getByMinimumStars(0); - final List oneStar = this.recipeService.getByMinimumStars(1); - final List twoStars = this.recipeService.getByMinimumStars(2); - - assertThat(zeroStars.size(), is(3)); - assertThat(oneStar.size(), is(2)); - assertThat(twoStars.size(), is(1)); - } - -}