I have mocked out HttpServletRequest, a service in my test:
request = createNiceMock(HttpServletRequest.class);
service = createStrictMock(DataProviderService.class);
I am verifying behaviour that my controller handles exception thrown by service, by setting expectaion:
expect(service.getData(someObject)).andThrow(new MyException());
replay(endPoint);
I verify the same as:
ModelAndView mav = controller.provideDefaultScreen(request);
verify(service);
My controller invokes service and in case of exception, sets exception details in request
request.setAttribute("exceptionMessage", e.getMessage());
This line is throwing out an error:
java.lang.IllegalStateException: missing behavior definition for the preceding method call:
HttpServletRequest.getAttribute("someAttributeCheckedEarlier")
Usage is: expect(a.foo()).andXXX()
I am least bothered about what my controller does to request, I am only concerned that it should eat up the exception thrown by service. Correspondingly, I've created request as nick mock. Why do I still get this error?
Unless you have cut it from your snippets of code, you don't seem to have called replay on the NiceMock. This will move it out of the "record" mode and make it usable.
Apart from the above answer, have you considered using Spring Mock? It has nice support for mocking and provides a host of classes that would not required using easymock atleast for supporting out of web container testing.
This framework can be used independent of Spring and your application doesn't need to use Spring Framework. Here is a nice article too...
Hope that helps.
Related
So i'm quite new with test cases and I have a small questions (specific to my case).
I am currently developing an Angular app and started to do unit test with Karma (Mocha/Chai).
The back end of this app is a node RESTful API.
So basically, the app is a bunch of controllers and services making some basic CRUD operations.
On creation of a new user for example, I handle the verification in the html form using angular's form directives. In the server side, there is also a verification on the object received.
So generally my functions on controllers are no more then things like:
create() {
UserService.create(vm.newUser).then(callBackToDisplaySuccessOrErrorMessage);
}
It will probably sounds silly, but i'm new in this domain (test cases) and i am a little bit confused, so my first question is:
since the http calls are mocked, what is the point to do unit testing in app such as mine ?
And my second question is :
how to unit test basic app like in my case in a proper way ?
To clarify your doubts:
1. Mocking is used to test the communication.
While writing spec for your controller who are more interested in verifying whether a call to userService.create is invoked with expected parameters or not. Similarly you will mock the response from service if any. So you have validated the functionality of Controller-communicating with- Service.
Now when you write spec for service, yes http calls are mocked, but still you are validating the http url, expected method to be invoked like GET, POST etc, parameters to be passed. You only mock the expected success/failure response and validate your handlers for the same.
Someone else created a WSDL service for me in Java. There is so far only one simple method to call. We tested this method in a C# console app by adding a service reference with the service's url. If I make a call to the generated method that has 'Async' at the end, I get an error. However, since the synchronous method is available, making calls to that work perfectly.
The trouble is, when I add a service reference to the same service from within a Silverlight project, the synchronous method is not available. A call to the async method also errors out.
Is there a different way I can do this so I can call the synchronous method from Silverlight?
An error occurred while trying to make a request to URI
'http://localhost:42083/Services/MyService.asmx'. This could be due to
attempting to access a service in a cross-domain way without a proper
cross-domain policy in place, or a policy that is unsuitable for SOAP
services. You may need to contact the owner of the service to publish
a cross-domain policy file and to ensure it allows SOAP-related HTTP
headers to be sent. This error may also be caused by using internal
types in the web service proxy without using the
InternalsVisibleToAttribute attribute. Please see the inner exception
for more details.
And the most peculiar thing about is that before it worked just fine, but when I changed just
a little thing in a stored procedure it throws this exception. That's weird! I'm getting a little more suspect against SOAP services for now. Anybody who has any ideas?
After having read you comment, please make sure you have deployed a cross-domain policy file. Tim has a great blog about it: http://timheuer.com/blog/archive/2008/04/06/silverlight-cross-domain-policy-file-snippet-intellisense.aspx. It is really an essential asset to obtain when starting SL to server communication.
Hope that helps.
I have a Silverlight application that communicates to a Silverlight-enabled WCF service, both of which are under my control as a developer.
What are some of the best practices for handling exceptions that may occur in the WCF Service.
If you simply try to throw an exception in your WCF service, the exception information does not make it back to the Silverlight client. Instead, the Silverlight client receives a generic (and misleading) "Not Found" exception.
My service methods often return specific class objects. I have thought of a few approaches, but would love to get alternatives.
Add Exception type properties to your custom class objects and check properties of returned value.
Create a generic wrapper class that all methods return, that have transaction details (and exception details), as well as an attached return value object that can be any object type.
I did read something about modifying the WCF service to return detailed error information in the event of an exception (versus the unhelpful "not found"), but the article was incomplete and so I am still not sure that would work.
Thanks
Silverlight 4.0 does support the notion of Fault Contracts, but in order for exception details to be accessible in your client fault messages need to be returned with a HTTP 200 response. The following gives all the details (take a look under the heading Configuring WCF SOAP Faults for Use with Silverlight Clients):
http://msdn.microsoft.com/en-us/library/ee844556(VS.95).aspx
In terms of best practices take a look at the web service architecture guidelines of the Web Service Software factory:
http://msdn.microsoft.com/en-us/library/ff699426.aspx
Specifically, the topic on Exception Handling:
http://msdn.microsoft.com/en-us/library/ff699460.aspx
These guidances are all provided from the POV of WCF implementations...
Check out that link to trap the "Not Found" error :
http://blogs.runatserver.com/lppinson/post/2010/04/15/Debugging-WCF-Web-Services.aspx
I'm trying to use Guice to inject properties of a JSF managed bean. This is all running on Google App Engine (which may or may not be important)
I've followed the instructions here:
http://code.google.com/docreader/#p=google-guice&s=google-guice&t=GoogleAppEngine
One problem is in the first step. I can't subclass the Servlet module and setup my servlet mappings there because Faces is handled by the javax.faces.webapp.FacesServlet which subclasses Servlet, not HttpServlet. So, I tried leaving my servlet configuration in the web.xml file and simply instantiating a new ServletModel() along with my business module when creating the injector in the context listener described in the second step.
Having done all that, along with the web.xml configuration, my managed bean isn't getting any properties injected. The method is as follows
#ManagedBean
#ViewScoped
public class ViewTables implements Serializable
{
private DataService<Table> service;
#Inject
public void setService( DataService<Table> service )
{
this.service = service;
}
public List<Table> getTables()
{
return service.getAll();
}
}
So, I'm wondering if there is a trick to get Guice injecting into a JSF managed bean? I obviously can't use the constructor injection because JSF needs a no-arg constructor to create the bean.
Check the following JSF-Guice integration framework/advice:
http://code.google.com/p/jsf-sugar/
http://notdennisbyrne.blogspot.com/2007/09/integrating-guice-and-jsf.html
http://cagataycivici.wordpress.com/2007/03/26/integrating_guice_and_jsf/
http://snippets.dzone.com/posts/show/7171
You can also create an HTTP servlet that then simple delegates the request on to a FacesServlet (like a wrapper). This should give you the same effect using Guice Servlet.
How about this approach, works well for us:
http://uudashr.blogspot.com/2008/12/guicing-jsf-with-guice.html
being the developer of jsf sugar I really would like to know the problem you had using it. We are already using it in production here so there shouldn't be any "show stoppers", maybe something is just not well documented? Just drop me a mail: murbanek(at)gmx_net (replace the _ with a .) .
check out http://code.google.com/p/guice2jsf/, and website starchu.blogspot.com, it has excellent library that provides Guice and JSF 2.0 integration
As information in this post are getting out of date but the question is still relevant, I'd like to share my findings about this topic. I wrote a little tutorial including a runnable sample project on how to setup a fully guice powered web stack. You can find it here: https://github.com/skuzzle/guice-jsf