fix: testing with websocket connection
This commit is contained in:
parent
cfacc37739
commit
25fdbcf49e
1 changed files with 19 additions and 4 deletions
|
|
@ -8,9 +8,11 @@ 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.context.annotation.Import;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import server.WebSocketConfig;
|
||||
import server.database.RecipeRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -20,6 +22,7 @@ import java.util.Set;
|
|||
import java.util.stream.LongStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
// Spring Boot unit testing magic
|
||||
// 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
|
||||
// This config uses an in-memory database
|
||||
@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 {
|
||||
|
||||
@Autowired
|
||||
private SimpMessagingTemplate template;
|
||||
|
||||
private final SimpMessagingTemplate template;
|
||||
private RecipeController controller;
|
||||
private List<Recipe> recipes;
|
||||
private final RecipeRepository recipeRepository;
|
||||
|
|
@ -43,8 +49,9 @@ public class RecipeControllerTest {
|
|||
|
||||
// Injects a test repository into the test class
|
||||
@Autowired
|
||||
public RecipeControllerTest(RecipeRepository recipeRepository) {
|
||||
public RecipeControllerTest(RecipeRepository recipeRepository, SimpMessagingTemplate template) {
|
||||
this.recipeRepository = recipeRepository;
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
|
|
@ -72,6 +79,7 @@ public class RecipeControllerTest {
|
|||
|
||||
// If no tags specified, the repository is initialized as empty.
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createOneRecipe() {
|
||||
controller.createRecipe(recipes.getFirst());
|
||||
|
|
@ -92,6 +100,7 @@ public class RecipeControllerTest {
|
|||
// 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() {
|
||||
|
|
@ -99,6 +108,7 @@ public class RecipeControllerTest {
|
|||
// 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")
|
||||
|
|
@ -109,6 +119,7 @@ public class RecipeControllerTest {
|
|||
recipes.get(CHECK_INDEX),
|
||||
controller.getRecipe(recipeIds.get(CHECK_INDEX)).getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findOneRecipeNotExists() {
|
||||
final int CHECK_INDEX = 3;
|
||||
|
|
@ -117,6 +128,7 @@ public class RecipeControllerTest {
|
|||
HttpStatus.NOT_FOUND,
|
||||
controller.getRecipe((long) CHECK_INDEX).getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Tag("test-from-init-data")
|
||||
@Tag("need-ids")
|
||||
|
|
@ -126,15 +138,18 @@ public class RecipeControllerTest {
|
|||
// The object has been successfully deleted
|
||||
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;
|
||||
controller.deleteRecipe(recipeIds.get(DELETE_INDEX));
|
||||
|
||||
// The count of items decreased by 1 after the 5th item has been removed.
|
||||
assertEquals(recipeIds.size() - 1, recipeRepository.count());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteOneRecipeFail() {
|
||||
final Long DELETE_INDEX = 5L;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue