Skip to main content


Showing posts from March, 2020

Mock unit testing with Mockito and Spring (Ejb and Rest service)

In this tutorial we are going to learn about mock unit testing. We are using here Mockito with Spring to write unit tests. We will also see how to mock an EJB session bean inside mocked service without constructor/setters injection. What is mocking Mocking is a technique to replace the actual implementation with some dummy or provided code chunk during unit testing. For example if we need to unit test some service which persist data to database then we can mock that persist method to add it to some in-memory collection. Also we can mock data retrieval to get the data from same collection or so. Mockito provide various class and features to implement the mocking seamlessly. Maven dependency We are using Spring (XML base configuration) and mockito for mock testing. Below dependencies are required. <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.9.RELEASE</ver

CORS implementation using Java Filter

What is CORS CORS is cross origin resource sharing which allows or block the cross domain calls from a web application between different domains. By default you are able to make Ajax calls to other domain. To enable it that provider also need to add certain headers to allow the requester domain. CORS Headers Below are the required headers to implement the CORS. Access-Control-Allow-Origin In this header we specify the domain from which we want to allow the access, for example: Access-Control-Allow-Credentials This header specifies if it can pass authorization details in cross domain request. for example: true/false. Access-Control-Max-Age We can use this header to specify how long we want to cache the preflight request details like allowed methods, allowed headers etc. We can specify value in seconds and we can cache them for a long time as such details are not change frequently. Access-Control-Allow-Methods Here we specify the methods we want to allow for requ