In my JUnit test, how do I verify a Spring RedirectView?

Dave

I'm using Spring 3.2.11.RELEASE and JUnit 4.11. In a particular Spring controller, I have a method that ends thusly ...

return new ModelAndView(new RedirectView(redirectUri, true));

In my JUnit test, how do I verify return from a submission to my controller in which this RedirectView is returned? I used to use org.springframework.test.web.AbstractModelAndViewTests.assertViewName, but that only returns "null", even when a non-empty ModelAndView object is returned. Here is how I'm constructing my JUnit test ...

    request.setRequestURI(“/mypage/launch");
    request.setMethod("POST");
    …
   final Object handler = handlerMapping.getHandler(request).getHandler();
    final ModelAndView mav = handlerAdapter.handle(request, response,  handler);
    assertViewName(mav, "redirect:/landing");

Any help on how to verify that a RedirectView comes back with the proper value is appreciatd,

Joao Evangelista

As Koiter said, consider moving to spring-test a and MockMvc

It providers some methods to test controllers and requests/reponses in a declarative way

you will need a @Autowired WebApplicationContext wac;

and on your @Before method setup this to use the @WebAppConfiguration of the class.

You'll end up with something

 @ContextConfiguration("youconfighere.xml")
 //or (classes = {YourClassConfig.class}
 @RunWith(SpringJUnit4ClassRunner.class)
 @WebAppConfiguration
 public class MyControllerTests {

 @Autowired WebApplicationContext wac
 private MockMvc mockMvc;


 @Before
 public void setup() {
      //setup the mock to use the web context
      this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); 
   }
}

Then you just need to use the MockMvcResultMatchers to assert things

 @Test
  public void testMyRedirect() throws Exception {
   mockMvc.perform(post("you/url/")
    .andExpect(status().isOk())
    .andExpect(redirectUrl("you/redirect")
}

Note: post(), status() isOk() redirectUrl() are statics imports from MockMvcResultMatchers

See more what you can match here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I addObject in Spring RedirectView

From Dev

How do I addObject in Spring RedirectView

From Dev

How do I JUnit test a Spring autowired constructor?

From Dev

How do I force a hit in my second-level cache in my JUnit test?

From Dev

How do I write a Junit test for a ThreadPoolExecutor

From Dev

How do I get my Maven Junit test to print out the same stack trace as what Eclipse JUnit generates?

From Dev

How can I test my svnadmin verify code?

From Dev

How do I stop other JUnit tests stealing my Spring Root Controller?

From Dev

How do I verify that ryujit is jitting my app?

From Dev

How do I verify that my SCXML defines a valid state machine?

From Dev

With Mockito, how do I verify my lambda expression was called?

From Dev

How do I verify that my eclipse project is effectively using tycho

From Dev

How can i test Optmistic Locking using junit, spring and JPA?

From Dev

How to test my @NotEmpty with JUnit

From Dev

How to test ConfigurationProperties in Spring with JUnit?

From Dev

How do I test my interface functions?

From Dev

How do I test my RPM?

From Dev

How do I disable Java assertions for a junit test in the code

From Dev

How do I access a text file for JUnit test in Android?

From Dev

How do I run JUnit 4.11 test cases with SBT?

From Dev

How do I run a junit test only after the build?

From Dev

How do I test a very simple void method in jUnit?

From Dev

How do I reuse JUnit test methods for different testing contexts?

From Dev

How do I reuse JUnit test methods for different testing contexts?

From Dev

How do I run JUnit 4.11 test cases with SBT?

From Dev

How do I assert an IOException message with JUnit Test annotation?

From Dev

Why is my Spring JUnit Test Rule not running?

From Dev

Using Spring's MockMvc framework, how do I test the value of an attribute of an attribute of my model?

From Dev

How do I unit test and integration test my SSIS packages?

Related Related

  1. 1

    How do I addObject in Spring RedirectView

  2. 2

    How do I addObject in Spring RedirectView

  3. 3

    How do I JUnit test a Spring autowired constructor?

  4. 4

    How do I force a hit in my second-level cache in my JUnit test?

  5. 5

    How do I write a Junit test for a ThreadPoolExecutor

  6. 6

    How do I get my Maven Junit test to print out the same stack trace as what Eclipse JUnit generates?

  7. 7

    How can I test my svnadmin verify code?

  8. 8

    How do I stop other JUnit tests stealing my Spring Root Controller?

  9. 9

    How do I verify that ryujit is jitting my app?

  10. 10

    How do I verify that my SCXML defines a valid state machine?

  11. 11

    With Mockito, how do I verify my lambda expression was called?

  12. 12

    How do I verify that my eclipse project is effectively using tycho

  13. 13

    How can i test Optmistic Locking using junit, spring and JPA?

  14. 14

    How to test my @NotEmpty with JUnit

  15. 15

    How to test ConfigurationProperties in Spring with JUnit?

  16. 16

    How do I test my interface functions?

  17. 17

    How do I test my RPM?

  18. 18

    How do I disable Java assertions for a junit test in the code

  19. 19

    How do I access a text file for JUnit test in Android?

  20. 20

    How do I run JUnit 4.11 test cases with SBT?

  21. 21

    How do I run a junit test only after the build?

  22. 22

    How do I test a very simple void method in jUnit?

  23. 23

    How do I reuse JUnit test methods for different testing contexts?

  24. 24

    How do I reuse JUnit test methods for different testing contexts?

  25. 25

    How do I run JUnit 4.11 test cases with SBT?

  26. 26

    How do I assert an IOException message with JUnit Test annotation?

  27. 27

    Why is my Spring JUnit Test Rule not running?

  28. 28

    Using Spring's MockMvc framework, how do I test the value of an attribute of an attribute of my model?

  29. 29

    How do I unit test and integration test my SSIS packages?

HotTag

Archive