spring-test-mvc

mockmvc Load Redirect URL

安稳与你 提交于 2020-03-22 06:23:34
问题 I am writing test for a Spring Rest Service which redirects the url to another spring service. So the goal is to find the Bookmark using a bookmark name. The first service gets the BookmarkId using the Bookmark name and then it redirects the url to load the object based on the BookmarkId I can easily test the url redirect using, the below works fine mockMvc.perform(get("/bookmarks/name/" + "sample")) .andExpect(status().isMovedPermanently()).andExpect(redirectedUrl("/bookmarks/" + bookmark

JMockit: @Mocke and MockUp combination in the same test

二次信任 提交于 2020-01-16 02:55:08
问题 What I have to do: I have to test my spring mvc with JMockit. I need to do two things: Redefine MyService.doService method Check how many times redefined MyService.doService method is called What the problem: To cope with the first item, I should use MockUp ; to cope with the second item I should use @Mocked MyService . As I understand this two approaches are overriding each other . My questions: How to override MyService.doService method and simultaneously check how many times it was invoked

Using Spring mockMvc to test optional path variables

谁说我不能喝 提交于 2020-01-13 08:08:37
问题 I have a method in Spring MVC with optional path variable. I am trying to test it for a scenario when optional path variable is not provided. Snippet from Controller, resource URI to invoke- @RequestMapping(value = "/some/uri/{foo}/{bar}", method = RequestMethod.PUT) public <T> ResponseEntity<T> someMethod(@PathVariable("foo") String foo, @PathVariable(value = "bar", required = false) String bar) { LOGGER.info("foo: {}, bar: {}", foo, bar); } Snippet from my test using MockMvc- //inject

Using Spring mockMvc to test optional path variables

吃可爱长大的小学妹 提交于 2020-01-13 08:08:10
问题 I have a method in Spring MVC with optional path variable. I am trying to test it for a scenario when optional path variable is not provided. Snippet from Controller, resource URI to invoke- @RequestMapping(value = "/some/uri/{foo}/{bar}", method = RequestMethod.PUT) public <T> ResponseEntity<T> someMethod(@PathVariable("foo") String foo, @PathVariable(value = "bar", required = false) String bar) { LOGGER.info("foo: {}, bar: {}", foo, bar); } Snippet from my test using MockMvc- //inject

How to test spring 5 controllers with Junit5

对着背影说爱祢 提交于 2020-01-03 11:15:43
问题 I'm trying to test my Spring 5 web controllers with JUnit 5. The two way to test controller (as mentionned in spring documentation) always give me null pointer. This is my test class import com.lacunasaurus.gamesexplorer.test.configuration.TestBackEndConfiguration; import com.lacunasaurus.gamesexplorer.test.configuration.TestWebConfig; import org.junit.Before; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.platform.runner.JUnitPlatform;

Null Authentication during Spring Security test

烈酒焚心 提交于 2019-12-24 17:09:40
问题 I wanted to integration test a Spring security authentication. Just if logging in works. But during the test the following error appeared: java.lang.AssertionError: Authentication should not be null My custom UserDetailsService is just fetching User from databse and mapping it to my custom UserDetails. What am I doing wrong here? This is what else I have: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {Application.class}) @WebAppConfiguration public class

Spring Boot WebMvc Tests not including Spring Security

跟風遠走 提交于 2019-12-24 10:47:26
问题 I am developing a Spring Boot MVC application which has a certain number of controllers. My root controller is: @Controller @RequestMapping("/") public class RootController { @GetMapping public String showStartPage() { log.info("GET: Show home page"); return "index"; } } I have successfully implemented MVC tests for the controllers. The test for my RootController is: @RunWith(SpringRunner.class) @WebMvcTest(RootController.class) public class RootControllerMvcTest { @Autowired private MockMvc

Spring security DefaultMethodSecurityExpressionHandler bean is not registered for Integration Test's default spring security config

余生长醉 提交于 2019-12-24 02:56:20
问题 I am attempting to write Spring MVC integration test with Spring Security and Thymeleaf for the view layer. I have setup my MockMvc object with Spring Security Integration just like all the examples from the documentation. Integration Test setUp: import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; import static org.springframework.security

Spring 3.2.5 error “java.lang.AssertionError: No ModelAndView found” for all my controller Unit Test

狂风中的少年 提交于 2019-12-24 01:28:23
问题 I'm trying to write some unit tests for my controllers in a Spring MVC and i have already error for my unit test : "java.lang.AssertionError: No ModelAndView found. my app and conrollers works correcty. Does anyone have any ideas ? code controller : @Controller public class LoginController { @RequestMapping(value="/loginfailed", method = RequestMethod.GET) public String loginerror(ModelMap model) { model.addAttribute("error", "true"); return "accueilLogin"; } } unit test :