Moved integration tests to integrationTest suite.

This commit is contained in:
JesseBrault0709 2024-07-01 10:01:21 +02:00
parent 0f8012134e
commit 4bbf3cf4bd
7 changed files with 42 additions and 92 deletions

View File

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

View File

@ -7,7 +7,6 @@ import org.springframework.boot.test.context.SpringBootTest;
class MealsMadeEasyApiApplicationTests {
@Test
void contextLoads() {
}
void contextLoads() {}
}

View File

@ -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<String, Object> 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<String, Object> 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())

View File

@ -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<Recipe> zeroStars = this.recipeService.getByMinimumStars(0);
final List<Recipe> oneStar = this.recipeService.getByMinimumStars(1);
final List<Recipe> twoStars = this.recipeService.getByMinimumStars(2);
assertThat(zeroStars.size(), is(3));
assertThat(oneStar.size(), is(2));
assertThat(twoStars.size(), is(1));
}
}