Skip to main content

Spring Controllers!




Image result for spring framework png There are a lot of things that application developers in JAVA have to tackle with. That is the reason we have frameworks. Frameworks tackle common application problems by providing structure and common patterns which makes the process of building applications, easier.

So spring framework, as its name suggest, is a framework which provides patterns and structure for JAVA applications. In the process, it handles the common things the developers need to do while building a JAVA application.

Spring is not just the core framework, but there are a whole lot of projects which are all part of the spring family of projects. In that sense, it is not just a framework, it is an ecosystem.

3 common problems the Spring Framework addresses-

1.       APPLICATION CONTEXT AND DEPENDENCY INJECTION
When you add spring framework in your application, it acts as a container for your objects. It wraps your application into a wrapper called the spring application context, and spring manages and creates instances. It also puts the objects together, just the way you want, in your graph picture! Interesting right?


2.       Spring MVC
Many enterprise applications are web applications which need to serve up either in dynamic web pages, HTML pages (as front-end applications) or they need to expose REST API. For this, spring framework provides data access to simplify connecting to and working with databases, and also building web pages easily, by using spring MVC!

A very simple introductory example to Spring MVC can be found at https://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/
The above flowchart is an amazing and simple representation of a basic spring mvc.

3.       DATABASE CONNECTIVITY
Most of the java applications need to connect to a database, because data is what brings an application to life. But, the traditional way of connecting to JAVA is by using JDBC. Have you ever used it? Well, most of the people who use it don’t like using it because the way to work in JAVA through JDBC is not really appealing. So here’s where spring comes in, with a solution! Spring framework comes with different API and mechanisms for connectivity, transaction management and more. It allows you to work with JDBC but hides all the less important stuff, and makes the experience easier!

I would also like to talk about the ways in which spring controllers are tested. I will be short and crisp, but will try my best to comprehend whatever I read and understood.


So there are two ways for we can test the controllers,

1. Unit Testing
True unit tests typically run extremely quickly, emphasizing true unit tests as part of your development methodology can boost your productivity. For certain unit testing scenarios, however, the Spring Framework provides mock objects and testing support classes.
Every unit test which we write to test the behavior of a controller method consists of these steps:
1.      We send a request to the tested controller method.
2.      We verify that we received the expected response.
This means, that we would test the controller in isolation. We would instantiate a controller object, mocking away the business logic, and then call the controller’s methods and verify the response.
But it cannot do the following things-
1.      Listen to HTTP requests
2.       De-serialize Input
3.       It can also not validate input when dependent on bean validation
4.      Serialize the output
5.      Translate Exceptions
Concluding, a simple unit test will not cover the HTTP layer. So, we need to introduce spring to our test to do the HTTP magic for us. Thus, there is an integration test that tests the integration between our controller code and the components Spring provides for HTTP support.




2. Integration testing

·         It manages Spring IOC container caching between tests.
·         It provides Dependency Injection of test fixture instances.
·         It also provides transaction management appropriate to integration testing.
·         It supplies spring-specific base classes that assist developers in writing integration tests.

Concluding that an integration test with spring, fires up a Spring application context that contains all the beans we need, which will include framework beans that are responsible for listening to certain URLs. These beans will evaluate the things that would be ignored by a simple unit test.


Sources:

Comments

Popular posts from this blog

BASIC CONCEPTS OF SPRING MVC IN OPENMRS

To start with, let us first know the basics and understand what is    spring model view   controller   Spring MVC   It is a Java framework which is used to build web applications. It follows the Model-View-Controller design pattern. The model here, contains the data of the application. The view represents the provided information in a particular format. The controller you can see here, contains the business logic of an application. Here, the @Controller annotation is used to mark the class as the controller. Also In Spring Web MVC, the DispatcherServlet class works as the front controller. It is responsible to manage the flow of the Spring MVC application. What is the flow of Spring Web  MVC ? Your question is answered below, As you can see, all the incoming requests are coming through the DispatcherServlet that acts as the front controller. The controller returns an object of ModelAndView. The DispatcherServlet checks the entry of view in the

Cracking the enigma for Microfrontends!

“Software architecture should never be an end goal, but a means to an end” I write this blog from the perspective of an inexperienced learner and hence the language adopted here would be in the layman’s terms making it easier to understand. We begin with the most obvious and important question that one should know the answer to before studying the applications of Micro frontend, i.e.  what is meant by the term Micro frontend.  Micro frontend is basically a term for Self-contained Systems or Frontend Integration for Vertical Systems which simply refer to an idea that considers a website or a web app as a composition of features which are owned by independent teams. Each team has a distinct area of business or mission it cares about and specializes in. A team is cross-functional and develops its features end-to-end, from database to user interface. Now we consider the secondary question:  How would using micro frontend development architecture over the traditional mon