Posts

Showing posts with the label spring

Unit testing of Spring Service with constructor dependencies

Constructor based dependency injection is the recommended way to use dependency injection in Spring. In this post I'm going to demonstrate how to write unit test of a service class which has dependencies on other beans e.g. repositories. Let's assume our service class is like the following: @Service class MyService { private final UserRepository userRepository;     private final AddressRepository addressRepository;     @Autowired     public MyService(UserRepository userRepository, AddressRepository addressRepository) {         this.userRepository = userRepository;         this.addressRepository = addressRepository;     }     public boolean isUserHasAddress(String username){     // omitting the method details, will return true always     return true;     } } Now we need to write unit test for the above class. Here is a way to do it: @RunWith(S...

Generate a Excel file from Spring

We often need to generate a spreadsheet file from server and allow users to download the file. Here is how to do that with Spring using the Apache POI library. Dependency: We need to add the following dependencies in our spring application. Here is the Gradle dependencies. compile group: 'org.apache.poi', name: 'poi', version: '3.15' compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.15' Once we add the dependencies we'll be able to access the POI library in our application. Generate WorkBook We need to generate a Workbook object in POI. Here is a sample method that returns a Workbook object public Workbook downloadPersonList() throws IOException {     // sample list. this can be the output of a DB query     List<Person> personList = new ArrayList<>();       personList.add(new Person("nayan", 29));     personList.add(new Person("rafiq", 35)) ;     personList.add(ne...

Map GET parameters to Object in Spring

It's possbile to map GET parameters to an object in Spring. It's really useful when we need to pass lots of parameters as query string e.g. submitting a search form with many parameters. The following controller demonstrates this.   @RestController @RequestMapping("hello") public class MyController {     @RequestMapping(value = "data", method = RequestMethod.GET)     public MyRequest getData(MyRequest request){             return request;     }     public static class  MyRequest {         private String name;          private int age;         public String getName(){return this.name;}         public int getAge(){return this.name;}         public void setName(String name){this.name=name;}          public void setAge(int age){this.age=ag...

Run tasks in background in Spring

Why background task? There are tons of examples where we often need to run a task in background e.g. sending an email, sms or notification, writing some log in file etc. If a task is not required to maintain atomicity and can be executed after some delay, it's a good candidate to run in background.   How to run tasks in background in Java? In Java, the easiest way to run a task in background is to create a new thread and execute it. It's quite simple. Here is how to do this: new Thread(new Runnable(){ @Override public void run(){ // your background task } }).start(); It's easy! But there are some maintenance problem with the above code. The major problem is - there is no way to control how many background tasks we want to run simultaneously. For example, if 1000 users registers in your application at a time and you create 1000 background threads to send them a confirmation email, maybe you'll be in trouble. So you ne...