Object cannot be cast to javax.servlet.http.HttpServletRequest with Spring WebFlow

iaforek

I'm trying to integrate Spring MVC with Spring WebFlow. In order to share session data between both of them I came up with this solution that actually works fine:

public String prepareForPayment(RequestContext context, Authentication currentUser) {
    PaymentDetails paymentDetails = new PaymentDetails();

    // CODE HERE

    HttpServletRequest request =       (HttpServletRequest)context.getExternalContext().getNativeRequest();
    request.getSession().setAttribute("paymentDetails", paymentDetails);

    // CODE HERE

}

Then in a Controller outside webflow I can easily get session data:

PaymentDetails paymentDetails = (PaymentDetails)session.getAttribute("paymentDetails");

So above code works fine and I'm able to set and get session attributes.

Now, when I write a test for this class I get:

java.lang.ClassCastException: java.lang.Object cannot be cast to javax.servlet.http.HttpServletRequest

Why my test is throwing ClassCastException and how to solve it?

iaforek

I checked my code again and I think the whole "problem" came from me trying to return Object instead of HttpServletRequest directly. Therefore, I caused ClassCastException. This piece of code works fine:

@RunWith(MockitoJUnitRunner.class)
public class Test {
    @Mock
    private RequestContext           context;

    @Mock
    private ExternalContext          externalContext;

    @Mock
    private HttpServletRequest       httpServletRequest;

    @Mock
    private HttpSession              httpSession;

    @Before
    public void setup() {
        Mockito.when(context.getExternalContext()).thenReturn(externalContext);
        Mockito.when(externalContext.getNativeRequest()).thenReturn(httpServletRequest);
        Mockito.when(httpServletRequest.getSession()).thenReturn(httpSession);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

RepositoryRestMvcConfiguration cannot be cast to javax.servlet.Servlet

From Dev

javax.servlet.http.HttpServletRequest.getParts()Ljava/util/Collection error

From Dev

Ljava.lang.Object; cannot be cast to [Ljavax.servlet.http.Cookie;

From Dev

java.lang.ClassCastException: org.glassfish.jersey.servlet.ServletContainer cannot be cast to javax.servlet.Servlet

From Dev

org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher cannot be cast to javax.servlet.Servlet

From Dev

How to fix 'cannot be cast to javax.servlet.Servlet' error while trying to package REST App with Servlet

From Dev

java.lang.ClassCastException: Servlet.Telnet cannot be cast to javax.servlet.Servlet

From Dev

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted() while using Mockito with Junit

From Dev

How to parse the 'Cookie' header into `javax.servlet.http.Cookie` with Spring?

From Dev

Spring + Hibernate + TestNG + Mockito cannot cast to javax.sql.DataSource

From Dev

spring compatibility with javax servlet

From Dev

java.lang.ClassCastException: Cannot cast com.sun.faces.config.FacesInitializer to javax.servlet.ServletContainerInitializer

From Dev

JBoss 7: ClassCastException: MyCustomTag cannot be cast to javax.servlet.jsp.tagext.Tag

From Dev

java.lang.ClassCastException: javax.faces.component.StateHolderSaver cannot be cast to [Ljava.lang.Object;

From Dev

Spring Boot Rest App on GAE throws an exception cannot be cast to javax.persistence.EntityManagerFactory"

From Dev

Object[] cannot be cast to resultado[]

From Dev

ClassCastException Object cannot be cast to

From Dev

Cannot cast Object to Comparable

From Dev

Cannot cast object to hashmap

From Dev

"cannot be cast" in Spring

From Dev

The import javax.servlet.ServletRegistration cannot be resolved

From Dev

javax.servlet.* cannot be resolved to a type

From Dev

javax.servlet.* cannot be resolved to a type

From Dev

Reflection on Servlet, Httpservletrequest type is getting RequestFacade type and cannot invoke method

From Dev

Can't resolve javax.servlet.http

From Dev

String cannot be cast to javax.jcr.Value

From Dev

WildFlyDataSource cannot be cast to javax.naming.Context

From Dev

javax.swing.JComboBox cannot be cast to javax.swing.JButton

From Dev

Cannot access javax.servlet.Filter; class file for javax.servlet.Filter not found

Related Related

  1. 1

    RepositoryRestMvcConfiguration cannot be cast to javax.servlet.Servlet

  2. 2

    javax.servlet.http.HttpServletRequest.getParts()Ljava/util/Collection error

  3. 3

    Ljava.lang.Object; cannot be cast to [Ljavax.servlet.http.Cookie;

  4. 4

    java.lang.ClassCastException: org.glassfish.jersey.servlet.ServletContainer cannot be cast to javax.servlet.Servlet

  5. 5

    org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher cannot be cast to javax.servlet.Servlet

  6. 6

    How to fix 'cannot be cast to javax.servlet.Servlet' error while trying to package REST App with Servlet

  7. 7

    java.lang.ClassCastException: Servlet.Telnet cannot be cast to javax.servlet.Servlet

  8. 8

    java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted() while using Mockito with Junit

  9. 9

    How to parse the 'Cookie' header into `javax.servlet.http.Cookie` with Spring?

  10. 10

    Spring + Hibernate + TestNG + Mockito cannot cast to javax.sql.DataSource

  11. 11

    spring compatibility with javax servlet

  12. 12

    java.lang.ClassCastException: Cannot cast com.sun.faces.config.FacesInitializer to javax.servlet.ServletContainerInitializer

  13. 13

    JBoss 7: ClassCastException: MyCustomTag cannot be cast to javax.servlet.jsp.tagext.Tag

  14. 14

    java.lang.ClassCastException: javax.faces.component.StateHolderSaver cannot be cast to [Ljava.lang.Object;

  15. 15

    Spring Boot Rest App on GAE throws an exception cannot be cast to javax.persistence.EntityManagerFactory"

  16. 16

    Object[] cannot be cast to resultado[]

  17. 17

    ClassCastException Object cannot be cast to

  18. 18

    Cannot cast Object to Comparable

  19. 19

    Cannot cast object to hashmap

  20. 20

    "cannot be cast" in Spring

  21. 21

    The import javax.servlet.ServletRegistration cannot be resolved

  22. 22

    javax.servlet.* cannot be resolved to a type

  23. 23

    javax.servlet.* cannot be resolved to a type

  24. 24

    Reflection on Servlet, Httpservletrequest type is getting RequestFacade type and cannot invoke method

  25. 25

    Can't resolve javax.servlet.http

  26. 26

    String cannot be cast to javax.jcr.Value

  27. 27

    WildFlyDataSource cannot be cast to javax.naming.Context

  28. 28

    javax.swing.JComboBox cannot be cast to javax.swing.JButton

  29. 29

    Cannot access javax.servlet.Filter; class file for javax.servlet.Filter not found

HotTag

Archive