Skip to main content

Spring 4: Unit Testing Classic Controller

Spring Boot is a great gift for all Spring developers. The productivity boost is enormous. It also makes it extremely easy to unit tests slices of your layered application. This feature is available from Spring Boot 1.4.x onwards. In this post, I am going to focus only on unit testing of Spring MVC classic controller i.e non-REST controller.

Spring Boot test slice support for MVC controller is enabled by the following annotation


This tells Spring MVC test framework that we only intend to test EmployeeController So it will only make the web MVC components (controllers, interceptors etc) available with mocking support. For more details of this annotation please refer to the Javadoc here.

In the example above, we are testing the request to copy the Employee. Mockito is used to create and inject the mock objects. Spring takes care of the mock objects, dependencies for us. This makes it extremely easy to write unit tests for classic Spring MVC controllers. Note that for quick feedback unit tests must be cheap i.e quick to develop, setup and execute. This goal is achieved very easily with this setup.

Beware

However, the Spring MVC controller test has one drawback. In this scenario, you would want to just test the functionality of the controller method by actually simulating the request/response cycle. This is done perfectly by Spring MVC test framework. However, it also loads/generates the view. In other words, it takes the logical view name from  ModelAndView object generates HTML content. This can potentially slow down the unit test execution time. This is also redundant as you will not be interested in testing the content of the HTML when testing for a controller (this can be done by Thyemeleaf test framework). In order to circumvent this limitation, I have added a blank test Thymeleaf template. Given this limitation, it will be very nice if Spring provides a switch/configuration to turn off the view generation. Github repository for this project is available at

Github Repo :

https://github.com/kdhrubo/playground

Comments

Popular posts from this blog

Breaking down the CRM monolith

In my previous posts, I have shared some theory regarding microservices. But it's time to start some implementation. I love to write code and see and feel things working. So I will start a series to refactor a monolithic CRM system and transform it into microservices based flexible software. Big ball of mud. Customer Relationship Management(CRM) is that giant software which existed since time immemorial and is used by all companies in some form or shape. Big enterprises will buy CRM software (also known as packages) from top CRM vendors like Oracle, SAP, Salesforce etc and then employ an army of consultants to try and implement it. Most of the classic CRM systems in the market today, even if deployed on the cloud are the big monolithic ball of mud. They are the gigantic piece of software with the huge feature set. Most often those requirements are surplus to the requirement or they will not fit into the processes of the company. So the company has to hire these certified consu...

Part 3 - Integrating Tiles, Thymeleaf and Spring MVC 3

In this post I will demonstrate how to integrate Apache Tiles with Thymeleaf. This is very simple. The first step is to include the tiles and thymeleaf-tiles extension dependencies. I will include them in the pom.xml. Note we wil lbe using Tiles 2.2.2 Listing 1 - parent/pom.xml --- thymeleaf-tiles and tiles dependencies <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <!-- Tiles --> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-core</artifactId> <version>${tiles.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-template</artifactId> <version>${tiles.version}</version> <scope>compile</s...

CKEDITOR 3.x - Simplest Ajax Submit Plugin

  I have assumed that you have downloaded and got started with CKEDITOR. Step 1 – The html file is shown below: <html> <head> <title>Writer</title> <meta content="text/html; charset=utf-8" http-equiv="content-type" /> <script type="text/javascript" src="ckeditor/ckeditor.js"></script> <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> <style> .cke_contents { height: 400px !important; } </style> </head> <body> <form action="sample_posteddata.php" method="post"> <textarea id="editor" > </textarea> <script type="text/javascript"> //<![CDATA[ CKEDITOR.replace( 'editor', { fullPage : true, uiColor : '#9AB8F3', toolbar : 'MyToolbar' }); //]]> </script> </form> </body> </html> Note that the jquery js...