Testing Module,controllers and Factory Service using Jasmine-Karma - jasmine

Am new to write Unit test case in jasmine..I have a doubt on loading the modules in the unit test case..
A scenario where my comple REST application with AngularJS has the same Module name but with different controllers,directives and Factory services..If i were to load the module simply by adding
module("module name") ---- Does this mean that am also loading all the controllers,Factory service and Directives along with it?
If my module has 100 Factory services,,then is it mandatory to mock all the services in BeforeEach block or just to mock the service for which am writing the unit test case for??
If the Factory service for which am writing the Unit test case has dependency on other services, should those services also be mocked in beforeEach() Block with its all the dependent service methods spied??
If the Factory service which am going to mock is present inside a controller then should the controller be mocked first and then the Factory service???
Is there a specific way of spying on a method that has arguments or the traditional way using createSpy() with the method name will do the trick no matter how many arguments the method has??
And also if a Directive has a Template Url that is pointing to a HTML file..Will that also be loaded as i load the module?
In case a directive requires an HTML file as template URL..How to mock the template URL? to prevent it from trying to output a HTML page

Q.1 - If my module has 100 Factory services,,then is it mandatory to mock all the services in beforeEach block or just to mock the service for which am writing the unit test case for?
Ans. - No. Just mock the services and spyOn the service's methods, your controller is relying on.
Q. 2 - If the Factory service for which am writing the Unit test case has dependency on other services, should those services also be mocked in beforeEach() Block with its all the dependent service methods spied??
Ans. - Yes. Those services should be mocked. But only the methods of those services on which, your Factory is relying should be spied on. There's no need to spy on all the methods in that service.
Q.3 - If the Factory service which am going to mock is present inside a controller then should the controller be mocked first and then the Factory service???
Ans. - Didn't exactly got what you meant by Factory service present inside a controller. Does that mean it is present inside a controller as a dependency? Or does it mean you have chained a .factory/.service method to .controller method. In any case, you don't need to mock the controller if you're writing the test case for service. But if you're writing test cases for controller, then you'll have to mock the service and spy on it's dependent methods.
Q.4 - Is there a specific way of spying on a method that has arguments or the traditional way using createSpy() with the method name will do the trick no matter how many arguments the method has??
Ans. - As far as I know, the number of arguments a method has, has nothing to do with it's spy. But yes, if you're using callFake, you must provide the method with the same number of parameters that the actual method accepts.
I don't have much knowledge about writing unit test cases for Directives, maybe somebody else can help you with that.
Hope this answers some of your questions.

Related

Restcontroller method naming convention spring boot

Are there conventions for naming methods in the Restful spring boot conrtoller layer ?
I am in dilemma choosing the two name - getSomeData vs fetchSomeData.
Is it OK to use HTTP Verbs (get,post,put) inside controller method names?
I think the most important thing is to be consistent in all your Controllers and to be explicit about what the method is supposed to be doing. It is completely ok to use HTTP verbs in the method names, especially in regards to GET. But when you have POSTs for example, that is usually a creation of a resource, so a method called createWhateverResource instead of postWhateverResource. The important thing is to be clear and let the name of the method be self-explanatory.
I checked a bit on the net. My conclusions:
There are no official naming rules
Official Spring Boot documentation uses short names: all(), one(), etc.
Names for the URLs are most important, method names are secondary
You never call these methods directly in code, they are only called by Spring framework.
A related note - for methods returning HTML (using Thymeleaf templates) I would probably call the methods by the page that they return: home(), orderDetails(), etc. Again for the same reason - we never call the methods directly. At the same time, it is very clear that #Controller and #RestController classes contain only methods returning HTTP responses to specific endpoints. Therefore, the verbs are probably not necessary.

How to Mock a Declarative Client in Micronaut?

I have Class in which I call a method from a declarative client. But for my test I don't want it to call the actual URL. Instead I want to Mock it. How can I do that as it is not a class but an Interface annotated with #Client?
Example code:- here. Please check section 4.3
In your test you can replace the http client bean with a mock. Please find a Spock snippet below.
// replace the client with a mock
#MockBean(YourClientInterface)
YourClientInterface yourClientInterface() {
return Mock(YourClientInterface)
}
// inject the mock in order to configure responses when it gets called
#Inject
YourClientInterface client
Now you can write tests and your code will run against the mock instead of the actual http client.

Autowired not working with Azure function

I am quit new in Azure function.
I found it as a interesting topic.right now,I have already develped azure function and works fine.
BUT my story will not end here. In the Function Method, I am trying to Autowiring my Repository class in spring in order to access to my DB layer.
but it gives me a null pointer exception. means that , "#Autowired" annotation is not working and not initiate my HotelController Class.
Any Idea , why I am not able to get the instance in Azure function?
I think you should take a look at:
spring-cloud-function-adapter-azure
This project provides an adapter layer for a Spring Cloud Function application onto Azure. You can write an app with a single #Bean of type Function and it will be deployable in Azure if you get the JAR file laid out right.
There is an AzureSpringBootRequestHandler which you must extend, and provide the input and output types as annotated method parameters (enabling Azure to inspect the class and create JSON bindings). The base class has two useful methods (handleRequest and handleOutput) to which you can delegate the actual function call, so mostly the function will only ever have one line.
And the sample that shows how to use it.
Hope it helps!

If we integrate struts2 with spring then who will maintain the action instances, spring container or struts 2 container

I am asking this question because of the following reasons:
Usually struts 2 action instances will get create on the request. I mean per every request new action instance will get create. But if I integrate with spring then there will be only one action instance will get create (I am not sure correct me if i am wrong). So in this case what is if I have instance variables in the action class. First user he will set that instance with some instance variables and second user may set the something.
How it will behave at this time.
More clarification: Instance variable means, in struts 2, action forms wont be there so, your action itself work as a form to get the request parameters. First user enters something and second user enters something and both are setting to one instance action.
By default Spring will create a singleton instance of your action class. In that case, depending on how your action classes are written there might be such a danger.
But you can also specify that a bean be created prototypically (scope="prototype") so that a new instance of the class is created with each request.
First, If you integrated struts2 with spring, normally, the action instances are managed by spring container! this is supported by struts2 spring plugin: https://struts.apache.org/release/2.3.x/docs/spring-plugin.html
Second, as the plugin doc mentioned, by default, the action bean's scope is request, this is up to the struts2, but you can change yuor action scope to other type,i.e. session,application,etc.

Symfony2 dependency Injection : performances impact

I'm refactoring one of my controller to make it a service and I want to know if there is a performance impact to not inject whole service container into my controller.
Is this is more efficient :
innova.path.controller:
class: %innova.controller.path.class%
arguments:
entityManager: #doctrine.orm.entity_manager
session: #session
securityContext: #security.context
router: #router
translator: #translator
pathManager: #innova.manager.path_manager
calls:
- [setRequest, ["#?request="]]
scope: request
than this, for example ?
innova.path.controller:
class: %innova.controller.path.class%
arguments: [#service_container]
Official documentation explicitly tell to not inject whole DIC into a Controller (thanks #NHG for link).
Section How to work with scopes :
Injecting the whole container into a service is generally not a good
idea (only inject what you need).
But in section Service container :
When you ask for the my_mailer service from the container, the
container constructs the object and returns it. This is another major
advantage of using the service container. Namely, a service is never
constructed until it's needed. If you define a service and never use
it on a request, the service is never created. This saves memory and
increases the speed of your application. This also means that there's
very little or no performance hit for defining lots of services.
Services that are never used are never constructed.
So injecting the whole DIC to controller will not have performances impact because only the services used in controller are instanciated.
The idea of using controllers as service is injecting only necessery services. Standard controller extends Symfony\Bundle\FrameworkBundle\Controller\Controller which extending Symfony\Component\DependencyInjection\ContainerAware. So, injecting whole container is makes no sense...
Generally, injecting less service is more efficient than injecting whole container.
Additionally you should familiarize with base Symfony2 Controller class.

Resources