fix: testing with websocket connection

This commit is contained in:
Natalia Cholewa 2025-12-04 23:03:52 +01:00
commit 25fdbcf49e

View file

@ -8,9 +8,11 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo; 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.context.annotation.Import;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import server.WebSocketConfig;
import server.database.RecipeRepository; import server.database.RecipeRepository;
import java.util.ArrayList; import java.util.ArrayList;
@ -20,6 +22,7 @@ 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;
import static org.junit.jupiter.api.Assertions.assertTrue;
// Spring Boot unit testing magic // Spring Boot unit testing magic
// Before each test the state of the repository is reset by // Before each test the state of the repository is reset by
@ -30,11 +33,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
// resources/application-mock-data-test.properties // resources/application-mock-data-test.properties
// This config uses an in-memory database // This config uses an in-memory database
@ActiveProfiles("mock-data-test") @ActiveProfiles("mock-data-test")
// This is required to enable WebSocket messaging in tests
//
// Without this line, Spring screams about missing SimpMessagingTemplate bean
@Import(WebSocketConfig.class)
public class RecipeControllerTest { public class RecipeControllerTest {
@Autowired private final SimpMessagingTemplate template;
private SimpMessagingTemplate template;
private RecipeController controller; private RecipeController controller;
private List<Recipe> recipes; private List<Recipe> recipes;
private final RecipeRepository recipeRepository; private final RecipeRepository recipeRepository;
@ -43,8 +49,9 @@ public class RecipeControllerTest {
// Injects a test repository into the test class // Injects a test repository into the test class
@Autowired @Autowired
public RecipeControllerTest(RecipeRepository recipeRepository) { public RecipeControllerTest(RecipeRepository recipeRepository, SimpMessagingTemplate template) {
this.recipeRepository = recipeRepository; this.recipeRepository = recipeRepository;
this.template = template;
} }
@BeforeEach @BeforeEach
@ -72,6 +79,7 @@ public class RecipeControllerTest {
// If no tags specified, the repository is initialized as empty. // If no tags specified, the repository is initialized as empty.
} }
@Test @Test
public void createOneRecipe() { public void createOneRecipe() {
controller.createRecipe(recipes.getFirst()); controller.createRecipe(recipes.getFirst());
@ -92,6 +100,7 @@ public class RecipeControllerTest {
// 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") @Tag("test-from-init-data")
public void getSomeRecipes() { public void getSomeRecipes() {
@ -99,6 +108,7 @@ public class RecipeControllerTest {
// 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("test-from-init-data")
@Tag("need-ids") @Tag("need-ids")
@ -109,6 +119,7 @@ public class RecipeControllerTest {
recipes.get(CHECK_INDEX), recipes.get(CHECK_INDEX),
controller.getRecipe(recipeIds.get(CHECK_INDEX)).getBody()); controller.getRecipe(recipeIds.get(CHECK_INDEX)).getBody());
} }
@Test @Test
public void findOneRecipeNotExists() { public void findOneRecipeNotExists() {
final int CHECK_INDEX = 3; final int CHECK_INDEX = 3;
@ -117,6 +128,7 @@ public class RecipeControllerTest {
HttpStatus.NOT_FOUND, HttpStatus.NOT_FOUND,
controller.getRecipe((long) CHECK_INDEX).getStatusCode()); controller.getRecipe((long) CHECK_INDEX).getStatusCode());
} }
@Test @Test
@Tag("test-from-init-data") @Tag("test-from-init-data")
@Tag("need-ids") @Tag("need-ids")
@ -126,15 +138,18 @@ public class RecipeControllerTest {
// The object has been successfully deleted // The object has been successfully deleted
assertEquals(HttpStatus.OK, controller.deleteRecipe(recipeIds.get(DELETE_INDEX)).getStatusCode()); assertEquals(HttpStatus.OK, controller.deleteRecipe(recipeIds.get(DELETE_INDEX)).getStatusCode());
} }
@Test @Test
@Tag("test-from-init-data") @Tag("test-from-init-data")
@Tag("need-ids") @Tag("need-ids")
public void deleteOneRecipeCountGood() { public void deleteOneRecipeCountGood() {
final int DELETE_INDEX = 5; final int DELETE_INDEX = 5;
controller.deleteRecipe(recipeIds.get(DELETE_INDEX)); controller.deleteRecipe(recipeIds.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(recipeIds.size() - 1, recipeRepository.count()); assertEquals(recipeIds.size() - 1, recipeRepository.count());
} }
@Test @Test
public void deleteOneRecipeFail() { public void deleteOneRecipeFail() {
final Long DELETE_INDEX = 5L; final Long DELETE_INDEX = 5L;