What actually spring framework is? What it is all about?
Source: https://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/ |
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/
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
Post a Comment