java.lang.NullPointerException when i run my unit test - mockito - spring-mvc

i have a spring mvc application and using mockito for my unit test.I keep getting null pointer exception when i run my unit test. :(
Please find below the method that my unit test will be based on:
#Controller
public class LogInController {
#Autowired
private MessageSource messageSource;
#RequestMapping(method = RequestMethod.POST, value = "/login")
public ModelAndView validateViewLogin(#ModelAttribute Person person,
BindingResult result, HttpServletRequest request) {
ModelAndView mav = new ModelAndView();
String userName = person.getFirstName();
String password = person.getPassword();
boolean isUserValid = false;
if (userName != null && password != null) {
isUserValid = userManagerService.validateUserLogin(userName,password);
}
if (!isUserValid) {
mav.addObject("failLog",messageSource.getMessage("login.user.fail", new String[] {" ", "" }, request.getLocale()));
mav.addObject("isUserValid", false);
mav.setViewName("login");
return mav;
}
mav.addObject("isUserValid", isUserValid);
mav.setViewName("home");
return mav;
}
Please find below my unit test:
#Test
public void validateViewLogin_NotValidLogin_returnsLoginPage() {
MockHttpServletRequest request = new MockHttpServletRequest();
Person person = new Person();
person.setFirstName("John");
person.setPassword("123");
isUserValid = false;
LogInController controller = new LogInController();
// Inject mock user service into controller
UserManagerService mockUserService = mock(UserManagerService.class);
controller.setUserManagerService(mockUserService);
// Configure mock user service to accept the person
when(mockUserService.validateUserLogin("John", "123")).thenReturn(
isUserValid);
// Attempt the validation
ModelAndView mav = controller
.validateViewLogin(person, result, request);
// Check the result
assertEquals("login", mav.getViewName());
}
Please find below stack trace error message when i run the unit test above:
java.lang.NullPointerException
at com.gemstone.presentation.LogInController.validateViewLogin(LogInController.java:99)
at com.gemstone.presentation.LogInControllerTest.validateViewLogin_NotValidLogin_returnsLoginPage(LogInControllerTest.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Basically line 99 which is shown below is returning null according to the stack trace:
mav.addObject(
"failLog",
messageSource.getMessage("login.user.fail", new String[] {
" ", "" }, request.getLocale()));
Any ideas why i am getting the null pointer on this line please?However when i run my application it works fine and failLog message is displayed on my page.
thanks in advance.

My guess is that your messageSource is null. You are mocking your request, but not your messageSource, and where you are newing the login controller it's not being injected by Spring.
While running it would get injected by Spring automatically.
Edit to clarify:
First you need to be able to set it some how for testing. You can either add a setter or a constructor to get to it, such as:
#Controller
public class LogInController {
#Autowired
private MessageSource messageSource;
public void setMessageSource(MessageSource ms){
this.messageSource = ms;
}
// ......
}
Then your test would need to change as well:
#Test
public void validateViewLogin_NotValidLogin_returnsLoginPage() {
MockHttpServletRequest request = new MockHttpServletRequest();
Person person = new Person();
person.setFirstName("John");
person.setPassword("123");
isUserValid = false;
LogInController controller = new LogInController();
// Inject mock user service into controller
UserManagerService mockUserService = mock(UserManagerService.class);
/** ADDED **/
MessageSource ms = mock(MessageSource.class);
controller.setUserManagerService(mockUserService);
/** ADDED **/
controller.setMessageSource(ms);
// Configure mock user service to accept the person
when(mockUserService.validateUserLogin("John", "123")).thenReturn(
isUserValid);
/** ADDED **/
when(ms.getMessage("login.user.fail", new String[] {" ", "" }).thenReturn("message");
// Attempt the validation
ModelAndView mav = controller
.validateViewLogin(person, result, request);
// Check the result
assertEquals("login", mav.getViewName());
}
Just free handing this, so make sure I don't have any typos! Essentially you try the MessageSource as you did your UserManagerService, it's just an interface after all and can be mocked exactly the same way.

Related

SpringWebMvcTest - Test Requestbody using #Valid and custom validation

I am trying to test my controller endpoint and my requestbody annotated with #Valid annotation. My Testclass looks like the follow:
#RunWith(SpringRunner.class)
#WebMvcTest(value = BalanceInquiryController.class, secure = false)
public class BalanceInquiryControllerTest {
#Autowired
private MockMvc mockMvc;
#MockBean
private BalanceInquiryController balanceInquiryController;
#Test
public void testGetBalanceInquiry() throws Exception {
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post("/com/balanceInquiry")
.accept(MediaType.APPLICATION_JSON)
.content("{\"comGiftCard\":{\"cardNumber\":\"1234567890\",\"pinNumber\":\"0123\"},\"comMerchant\":\"MERCHANT1\"}")
.contentType(MediaType.APPLICATION_JSON);
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse response = mvcResult.getResponse();
assertEquals(HttpStatus.OK.value(), response.getStatus());
}
}
My Controller - #PostMapping looks like that:
#PostMapping(value = "/com/balanceInquiry")
public ResponseEntity<?> getBalanceInquiry(#Valid #RequestBody BalanceInquiryModel balanceInquiry, Errors errors) {
if (errors.hasErrors()) {
return new ResponseEntity<String>("Validation error", HttpStatus.BAD_REQUEST);
}
//do any stuff...
return new ResponseEntity<BalanceInquiryResponse>(balanceInquiryResponse, HttpStatus.OK);
}
My BalanceInquiryModel is annotated with #Valid and has some hibernate and custom validations behind. Those validations are all ok and already unit tested.
What I like to test is my endpoint where I send a valid json request body expecting a 200 response and also an invalid json request body expecting a 400 response validated by the set #Valid implementation.
For example an unvalid call is to send no pinNumber or length < 4.
I have read some threads and some uses MockMvcBuilders.standaloneSetup() to mock the full controller. But I wont do a full integration test.
Not quite sure how to go on with this situation and if I should go on.
P.S.: At the moment I get always a 200 response no matter if the validation should give an error or not.
Here a gist for more code and the validation classes/models.
Here's one of my example I work on my project
hope it help you out:
I have a global exception handler to handler my MethodArgumentNotValidException and throw it
#RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> createUser(#Valid #RequestBody User user) {
User savedUser = userService.save(user);
return new ResponseEntity<User>(savedUser, HttpStatus.CREATED);
}
public void testAdduser() throws Exception{
final User request = new User();
request.setFirstName("Test");
request.setLastName("some description");
mockMvc.perform(post(END_POINT+"/add")
.contentType(MediaType.APPLICATION_JSON)
.content(stringify(request))
).andDo(print()).andExpect(status().isUnprocessableEntity())
;
}
private String stringify(Object object) throws JsonProcessingException {
return new ObjectMapper().writeValueAsString(object);
}
Update:
I think your main problem is that you are using #WebMvcTest in stead of #SpringBootTest.
the different between 2 of them is that:
#SpringBootTest annotation will loads complete application and injects all the beans which is can be slow.
#WebMvcTest - for testing the controller layer. it doesn't inject other bean beside the #RestController
so if you are just testing just pure controller to see u can reach the endpont then you can just use #WebMvcTest which will make your test run faster.
but in your case, you want it to run the spring validation, you will need to use #SpringBootTest
for detailed: https://spring.io/guides/gs/testing-web/

SoapFault handling with Spring WS client - WebServiceGatewaySupport and WebServiceTemplate

I am trying to write a Spring WS client using WebServiceGatewaySupport. I managed to test the client for a successful request and response. Now I wanted to write test cases for soap faults.
public class MyClient extends WebServiceGatewaySupport {
public ServiceResponse method(ServiceRequest serviceRequest) {
return (ServiceResponse) getWebServiceTemplate().marshalSendAndReceive(serviceRequest);
}
#ActiveProfiles("test")
#RunWith(SpringRunner.class)
#SpringBootTest(classes = SpringTestConfig.class)
#DirtiesContext
public class MyClientTest {
#Autowired
private MyClient myClient;
private MockWebServiceServer mockServer;
#Before
public void createServer() throws Exception {
mockServer = MockWebServiceServer.createServer(myClient);
}
}
My question is how do i stub the soap fault response in the mock server, so that my custom FaultMessageResolver will be able to unmarshall soap fault?
I tried couple of things below, but nothing worked.
// responsePayload being SoapFault wrapped in SoapEnvelope
mockServer.expect(payload(requestPayload))
.andRespond(withSoapEnvelope(responsePayload));
// tried to build error message
mockServer.expect(payload(requestPayload))
.andRespond(withError("soap fault string"));
// tried with Exception
mockServer.expect(payload(requestPayload))
.andRespond(withException(new RuntimeException));
Any help is appreciated. Thanks!
Follow Up:
Ok so, withSoapEnvelope(payload) I managed to get the controller to go to my custom MySoapFaultMessageResolver.
public class MyCustomSoapFaultMessageResolver implements FaultMessageResolver {
private Jaxb2Marshaller jaxb2Marshaller;
#Override
public void resolveFault(WebServiceMessage message) throws IOException {
if (message instanceof SoapMessage) {
SoapMessage soapMessage = (SoapMessage) message;
SoapFaultDetailElement soapFaultDetailElement = (SoapFaultDetailElement) soapMessage.getSoapBody()
.getFault()
.getFaultDetail()
.getDetailEntries()
.next();
Source source = soapFaultDetailElement.getSource();
jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller.setContextPath("com.company.project.schema");
Object object = jaxb2Marshaller.unmarshal(source);
if (object instanceof CustomerAlreadyExistsFault) {
throw new CustomerAlreadyExistsException(soapMessage);
}
}
}
}
But seriously!!! I had to unmarshall every message and check the instance of it. Being a client I should be thorough with all possible exceptions of the service here, and create custom runtime exceptions and throw it from the resolver. Still at the end, its been caught in WebServiceTemplate and re thrown as just a runtime exception.
You could try with something like this:
#Test
public void yourTestMethod() // with no throw here
{
Source requestPayload = new StringSource("<your request>");
String errorMessage = "Your error message from WS";
mockWebServiceServer
.expect(payload(requestPayload))
.andRespond(withError(errorMessage));
YourRequestClass request = new YourRequestClass();
// TODO: set request properties...
try {
yourClient.callMethod(request);
}
catch (Exception e) {
assertThat(e.getMessage()).isEqualTo(errorMessage);
}
mockWebServiceServer.verify();
}
In this part of code mockWebServiceServer represents the instance of MockWebServiceServer class.

How to test common error ErrorController in spring-boot with junit

In a spring boot project, I'd like to test my ErrorController with Junit.
The code is as the following snippet.
#RestController
public class ApiErrorController implements ErrorController {
private static final Logger LOGGER = LoggerFactory.getLogger(ApiErrorController.class);
#Value("${server.error.path}")
private String errorPath;
#Override
public String getErrorPath() {
return this.errorPath;
}
#RequestMapping("/error")
public ResponseEntity<ErrorResult> error(HttpServletRequest request, HttpServletResponse response) {
String requestURI = (String) request.getAttribute("javax.servlet.forward.request_uri");
LOGGER.info("error handling start url = {}", requestURI);
String servletMessage = (String) request.getAttribute("javax.servlet.error.message");
Integer servletStatus = (Integer) request.getAttribute("javax.servlet.error.status_code");
String[] messages = new String[0];
if (!StringUtils.isNullOrEmpty(servletMessage)) {
messages = new String[] { servletMessage };
}
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
try {
if (servletStatus != null && servletStatus instanceof Integer) {
status = HttpStatus.valueOf(servletStatus);
}
} catch (Exception ex) { // test this exception
LOGGER.warn("http status not converted.{}", request.getAttribute("javax.servlet.error.status_code"), ex);
}
ErrorResult body = new ErrorResult();
body.setMessages(messages);
ResponseEntity<ErrorResult> responseResult = new ResponseEntity<>(body, status);
return responseResult;
}
}
When a business exception happened in my Controller(for example AbcController), then the program goes into the ExceptionControllerAdvice class.
If an exception happened in ExceptionControllerAdvice, then the program goes into the above ApiErrorController class.
Could someone tell me how to test the case that HttpStatus.valueOf(servletStatus) fail?
In addition, I want request.getAttribute("javax.servlet.error.message") return a non-empty string.
How to achieve what I'd like to test?
By the way, I don't want to only test the logic of error method. I'd like to use AbcController I mentioned to make the test. What I want is when a error happens in AbcController, then the error method in ApiErrorController can handle it successfully.
APPEND:
For example, ExceptionControllerAdvice will handle the business exception.
#ControllerAdvice(annotations = RestController.class)
public class ExceptionControllerAdvice {
private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionControllerAdvice.class);
#ExceptionHandler({ BusinessCloudException.class })
public ResponseEntity<ErrorResult> handleBlCloudException(HttpServletRequest request, HttpServletResponse response,
BlCloudException ex) {
HttpStatus status = ErrorUtils.toHttpStatus(ex.getType());
ErrorResult body = new ErrorResult();
body.setMessages(ex.getMessageArray());
ResponseEntity<ErrorResult> responseResult = new ResponseEntity<>(body, status);
return responseResult;
}
}
If there's an error happened in the handleBlCloudException method, then the program goes into ApiErrorController to handle this error.
How do the program produce the a specific servletStatus and javax.servlet.error.message? How to mock to do this?
First of all there is quite a lot going on in that error method. You might consider moving some of the logic to a specialized class / public methods.
Apart from that i would suggest using Mockito.
Fist of all create a method to encapsulate the HttpStatus retrieval:
HttpStatus getHttpStatusByServletStatus(Integer servletStatus){
return HttpStatus.valueOf(servletStatus);
}
and change your code to :
if (servletStatus != null && servletStatus instanceof Integer) {
status = getHttpStatusByServletStatus(servletStatus);
}
Then the test class:
public ApiErrorControllerTest{
#Spy
private ApiErrorController apiErrorController;
#Mock
HttpServletRequest requestMock;
#Mock
HttpServletResponse responseMock;
#Befire
public void init(){
MockitoAnnotations.initMocks(this);
}
#Test
public void test(){
// Arrange
HttpStatus expectedStatus = // expected status
String expectedErrorMessage = // ..
doReturn(expectedStatus).when(apiErrorController)
.getHttpStatusByServletStatus(Mockito.anyString());
when(requestMock.getAttribute("javax.servlet.error.message"))
.thenReturn(expectedErrorMessage);
// other setup..
// Act
apiErrorController.error(requestMock, responseMock);
// Assertions
}

Right way to do JMS Application with MDB and Glassfish 3

I am new to JMS and I Wrote a Wrote a sender and receiver and it has worked fine . Now i want a MDB to be used for this. I have googled a lot for this but did not find any good website so i am summering the way i wrote. Please correct me if i am wrong and
This is not completely working so i think there is something wrong in my code.
Sender:
public class MySender {
/**
* #param args
*/
public static void main(String[] args) {
try
{ //Create and start connection
Hashtable hashTable = new Hashtable();
hashTable.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.impl.SerialInitContextFactory");
hashTable.put(Context.URL_PKG_PREFIXES, "com.sun.enterprise.naming");
hashTable.put(Context.PROVIDER_URL, "http://localhost:4848");
//1) Create and start connection
InitialContext ctx=new InitialContext(hashTable);
QueueConnectionFactory f=(QueueConnectionFactory)ctx.lookup("myQueueConnectionFactory");
QueueConnection con=f.createQueueConnection();
con.start();
//2) create queue session
QueueSession ses=con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
//3) get the Queue object
Queue t=(Queue)ctx.lookup("myQueue");
//4)create QueueSender object
QueueSender sender=ses.createSender(t);
//5) create TextMessage object
//5) create TextMessage object
TextMessage msg=ses.createTextMessage();
msg.setText("Hello ************************************");
sender.send(msg);
con.close();
System.out.println("*****************end********************");
}catch(Exception e){System.out.println(e);} }
}
MDB and Listner:
#MessageDriven(mappedName="myQueue")
public class MyListener implements MessageListener {
public void onMessage(Message message) {
try{
if (message != null && TextMessage.class.isInstance(message)) {
System.out.println("((((((((((((((((((((((((((((((((((");
TextMessage msg=(TextMessage)message;
final XStream xsStream = new XStream();
final TextMessage textMessage = (TextMessage) message;
Reader xmlMessage = new StringReader(textMessage.getText());
Object obj = xsStream.fromXML(xmlMessage);
System.out.println("obj:::::::::::"+obj);
if (obj != null && LetterOutHeader.class.isInstance(obj)) {
LetterOutHeader letterObj = (LetterOutHeader)obj;
System.out.println("one:::::::"+letterObj.getState());
}
System.out.println("following message is received::::::::::::::::::"+msg.getText());
}
}catch(JMSException e){System.out.println(e);}
}
}
Have Created a EJB Project, added a class MyListener and has exported that as a jar and have deployed that to the server.
Wrote a normal java application and have added a class MySender.
Run the MySender as a java program.

How to test Spring MVC controller with RedirectAttributes

I am using:
Spring 3.1
JUnit 4.10
I am trying to write a junit test case for a controller with RedirectAttributes.
Controller's signature is:
#RequestMapping(method = RequestMethod.POST)
public String homePOST(#Validated({ IBasic.class }) #ModelAttribute("userCommand") User userCommand,
BindingResult result, Model model,
RedirectAttributes flashAttributes)
JUnit test case is:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "/root-context.xml", "/servlet-context.xml" })
public class HomeControllerTest {
#Autowired
private RequestMappingHandlerAdapter handlerAdapter;
#Autowired
private RequestMappingHandlerMapping handlerMapping;
#Test
public void testHomePOST() {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/");
request.addParameter("username", "user1");
request.addParameter("password", "mypasswd");
MockHttpServletResponse response = new MockHttpServletResponse();
Object handler;
try {
handler = handlerMapping.getHandler(request).getHandler();
ModelAndView modelAndView = handlerAdapter.handle(request,
response, handler);
assertViewName(modelAndView, "redirect:/myview");
} catch (Exception e) {
String err = "Error executing controller : " + e.toString();
fail(err);
}
}
}
When handlerAdapter.handle() is executed, i am getting :
java.lang.AssertionError: Error executing controller : java.lang.NullPointerException
at org.junit.Assert.fail(Assert.java:93)
at com.myapps.service.impl.test.HomeControllerTest.testHomePOST(HomeControllerTest.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
If i remove the RedirectAttributes from the controller, it is working properly.
Can anyone please provide some help how to test a controller with RedirectAttributes?
Thank you in advance.
In order to simulate this, here is the controller code:
#Controller
#RequestMapping(value = "/")
public class HomeController {
#RequestMapping(method = RequestMethod.POST)
public String homePOST(
#Validated({ IBasic.class }) #ModelAttribute("userCommand") User userCommand,
BindingResult result, Model model,
RedirectAttributes flashAttributes) {
return "redirect:/myview";
}
}
I am able to replicate your issue - the underlying reason for the issue is that if a RedirectAttributes is present as a handler method parameter, then just before returning from the handler adapter, the output "flashmap" is pulled and the flashAttributes added to it. Now, this output flashMap is retrieved from the httprequest - in your case, this is null for your MockHttpServletRequest.
The fix is to simply set a dummy output flashMap in the MockHttpServletRequest:
request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE,new FlashMap());
This works for me now. Can you please try and see if it fixes this issue for you.
Use mockito framework to mock it.
use
RedirectAttributes flashAttributes;
flashAttributes=Mockito.mock(RedirectAttributes.class)
in the setup function.

Resources