diff --git a/src/test/java/io/hexlet/blog/controller/api/PostsCommentsControllerTest.java b/src/test/java/io/hexlet/blog/controller/api/PostsCommentsControllerTest.java index 8778a71..03a73c2 100644 --- a/src/test/java/io/hexlet/blog/controller/api/PostsCommentsControllerTest.java +++ b/src/test/java/io/hexlet/blog/controller/api/PostsCommentsControllerTest.java @@ -2,6 +2,7 @@ import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -20,12 +21,19 @@ import io.hexlet.blog.util.ModelGenerator; import io.hexlet.blog.util.UserUtils; import jakarta.transaction.Transactional; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import java.nio.charset.StandardCharsets; @SpringBootTest @Transactional @AutoConfigureMockMvc public class PostsCommentsControllerTest { + @Autowired + private WebApplicationContext wac; + @Autowired private MockMvc mockMvc; @@ -47,7 +55,13 @@ public class PostsCommentsControllerTest { @BeforeEach public void setUp() { + mockMvc = MockMvcBuilders.webAppContextSetup(wac) + .defaultResponseCharacterEncoding(StandardCharsets.UTF_8) + .apply(springSecurity()) + .build(); + token = jwt().jwt(builder -> builder.subject("hexlet@example.com")); + testPost = Instancio.of(modelGenerator.getPostModel()) .create(); testPost.setAuthor(userUtils.getTestUser()); diff --git a/src/test/java/io/hexlet/blog/controller/api/PostsControllerTest.java b/src/test/java/io/hexlet/blog/controller/api/PostsControllerTest.java index 67154e5..f283b21 100644 --- a/src/test/java/io/hexlet/blog/controller/api/PostsControllerTest.java +++ b/src/test/java/io/hexlet/blog/controller/api/PostsControllerTest.java @@ -4,6 +4,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -29,11 +30,18 @@ import io.hexlet.blog.repository.PostRepository; import io.hexlet.blog.util.ModelGenerator; import io.hexlet.blog.util.UserUtils; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import java.nio.charset.StandardCharsets; @SpringBootTest @AutoConfigureMockMvc public class PostsControllerTest { + @Autowired + private WebApplicationContext wac; + @Autowired private MockMvc mockMvc; @@ -58,7 +66,13 @@ public class PostsControllerTest { @BeforeEach public void setUp() { + mockMvc = MockMvcBuilders.webAppContextSetup(wac) + .defaultResponseCharacterEncoding(StandardCharsets.UTF_8) + .apply(springSecurity()) + .build(); + token = jwt().jwt(builder -> builder.subject("hexlet@example.com")); + testPost = Instancio.of(modelGenerator.getPostModel()) .create(); testPost.setAuthor(userUtils.getTestUser()); diff --git a/src/test/java/io/hexlet/blog/controller/api/UsersControllerTest.java b/src/test/java/io/hexlet/blog/controller/api/UsersControllerTest.java index 9b1a623..62c4940 100644 --- a/src/test/java/io/hexlet/blog/controller/api/UsersControllerTest.java +++ b/src/test/java/io/hexlet/blog/controller/api/UsersControllerTest.java @@ -3,11 +3,13 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import org.instancio.Instancio; @@ -26,11 +28,16 @@ import io.hexlet.blog.repository.UserRepository; import io.hexlet.blog.util.ModelGenerator; import net.datafaker.Faker; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; @SpringBootTest @AutoConfigureMockMvc public class UsersControllerTest { + @Autowired + private WebApplicationContext wac; + @Autowired private MockMvc mockMvc; @@ -52,7 +59,13 @@ public class UsersControllerTest { @BeforeEach public void setUp() { + mockMvc = MockMvcBuilders.webAppContextSetup(wac) + .defaultResponseCharacterEncoding(StandardCharsets.UTF_8) + .apply(springSecurity()) + .build(); + token = jwt().jwt(builder -> builder.subject("hexlet@example.com")); + testUser = Instancio.of(modelGenerator.getUserModel()) .create(); userRepository.save(testUser);