I am trying to mock the HttpServletRequest and HttpServletResponse objects in order to set parameters and attributes to the request.
I don't have the package MockHttpServletRequest. Is there any way I can get the mock object of HttpServletRequest without using MockHttpServletRequest?
You can simply use mockito to mock HttpServletRequest and HttpServletResponse
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
I was using the same code as want2learn, but still facing errors. Long story short dependency issue. I was using runner which was dependent on Mockito2 and mocking with the help of Mockito1 or just Mockito causing it to fail. (I am still learning JUnit so I don't know how it works yet). Also, I am doing everything without Spring. If anyone needs example code comment below.
Related
Since upgrading spring-integration to 5.3.10.RELEASE we can't mock the requestFactory on HttpRequestExecutingMessageHandler anymore in the unit tests, it is actually checked if the requestFactory setter has been called and an exception is thrown in that case.
So, how to configure the HttpRequestExecutingMessageHandler.requestFactory now with a Mockito instance?
The check there is like this:
public void setRequestFactory(ClientHttpRequestFactory requestFactory) {
assertLocalRestTemplate("requestFactory");
this.restTemplate.setRequestFactory(requestFactory);
}
That means that you have been providing a RestTemplate into this HttpRequestExecutingMessageHandler. The requestFactory is essentially a property of the RestTemplate. So, consider to set your mock into that external RestTemplate instead.
I have a class that has a static method that passes in request and that calls server to retrieve the response.
is there a way to mock that since it unit test I do not want to make a service call.
String jsonResponse = getMeMyMoney(request)
protected static String getMeMyMoney(request)
{
response = executeService(request)
return response
}
I tried this which is supposed to bypass the method but it still went it. Any one knows how to do this
doReturn("1").when(TestClass.getMeMyMoney("S"));
You cannot mock static methods with Mockito, it is also stated in FAQ.
Use PowerMock on top of Mockito.
PowerMockito.mockStatic(TestClass.class);
when(TestClass.getMeMyMoney("S")).thenReturn("1");
I am writing an Jersey Response filter. I am using Jersey 1.17. I want to get access to some attributes of the httpServletRequest in the filter API. The way what i am doing right now is as below. Is it safe to inject the servletRequest like in the snippet below or will this cause some kind of concurrency issues? If there are multiple requests coming in conncurrently, will the servletRequest in different requests overwrite each other? Thanks for your hlep.
public class LoggingFilter implements ContainerResponseFilter {
#Context private HttpServletRequest servletRequest;
#Override
public ContainerResponse filter(final ContainerRequest req, final ContainerResponse resp) {
String s = this.servletRequest.getAttribute("xxx");
....
}
}
Section 9.1 (latest, 5.1 previously) Concurrency of the JAX-RS specification states:
Context is speciļ¬c to a particular request but instances of certain
JAX-RS components (providers and resource classes with a lifecycle
other than per-request) may need to support multiple concurrent
requests. When injecting an instance of one of the types listed in
Section 9.2, the instance supplied MUST be capable of selecting the
correct context for a particular request. Use of a thread-local proxy is a common way to
achieve this.
So, as per the specification, JAX-RS implementations (e.g. Jersey) are required to ensure that the context is safe. Keep doing what you're doing.
See also: Extract request attributes from container request of Jersey
You're safe. When you're injecting HttpServletRequest / HttpServletResponse you're not dealing with a particular instance but rather with a proxy through which you're invoking calls on a real instance stored in a ThreadLocal object. Each request is processed by a separate thread which has access to it's own HttpServletRequest / HttpServletResponse. Beside injecting HttpServletRequest / HttpServletResponse you can also inject ThreadLocal<HttpServletRequest> / ThreadLocal<HttpServletResponse> and through '#get()` method you can obtain the real request / response instances intead of proxies.
I'm working on Spring MVC with richfaces.
Is there a way to call a method in a managed bean Controller from URL?
e.g: website.com/somecontroller/somemethod?x=1
I tried #RequestMapping but didn't work.
Thanks in advance
When the browser client want to access to an URL, the managed beans declared in the page will be created, the constructor and #PostConstruct methods will be invoked server side.
You can recover the parameters using #ManagedProperty as proposed by BalusC (as he says, the JSF-ish way):
Parameters in URL JSF 2
If that answer doesn't fit for your needs, you can recover the request object and get the parameters one by one, as stated in the question:
HttpServletRequest request = (HttpServletRequest)FacesContext.
getCurrentInstance().getExternalContext().getRequest();
String clipId = request.getParameter("x");
I have a simple question, but I'm searching for longer time, but I always found the same answers,which i don't really know how to handle...
i want to get the IP adress of the client, when he registers to my application...
i found something like this:
#ManagedBean(name="testController")
#SessionScoped
public class TestController implements Serializable {
private static final long serialVersionUID = -3244711761400747261L;
protected final HttpServletRequest req;
public TestController(HttpServletRequest req) {
this.req = req;
System.out.println(this.req.getRemoteAddr().toString());
}
}
but i don't have the HttpServletRequest in the constructor....
or i don't know how to use it, all i get are errors....
It's available by the ExternalContext#getRequest().
public TestController() {
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
System.out.println(request.getRemoteAddr());
}
Note that you're making one major conceptual mistake in your initial attempt. You're attempting to assign the current HTTP request as a property of a session scoped managed bean. The HTTP request instance will expire by the end of the current HTTP response and thus not be valid anymore and throw exceptions in all colors when you try to access its methods in the subsequent requests following the initial request when the session scoped bean was been created.
I'd go for a different approach, also used in the Seam Solder project: Make a servlet filter that captures the servlet request and makes it available via an application scoped producer. See corresponding source code of the solder project.