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 contr...

Google Code-In 2020 Wrap Up

"You do not need to be a programmer, you don’t have to know how to code at all!" is where it all started.    Google Code-in  ( GCI ) As its name suggests, is an annual programming competition hosted by Google that allows students to complete tasks specified by various open-source organizations. the organization which I chose was - OpenMRS.  What is OpenMRS?  OpenMRS is both a software and a community. It serves as a medical record system (EMR) which is designed for developing countries. OpenMRS follows a principle that information should be stored in a way that makes it easy to summarize and analyze. If you are interested in learning more about the OpenMRS community, you may go  here  and know more about it. If one would ask me to define OpenMRS in one line, it would be "we write code for humans first and then computers." as their main motto is to save lives.  My interactions in OpenMRS My interaction with OpenMRS ...