MBBS Admission in Pakistan

We all know that MBBS is a noble profession. Because medical field is directly related to someone live So to get in this noble profession you need to clear your entrance test in pakistan known as…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Testing Spring Controller

There are many controllers in the Spring MVC module, and each of them is used in specific cases. Some of the controllers are:

SimpleFormController is used in the “Form” interface. In form, the user inputs data, edits it, etc. The SimpleFormController is responsible for representing the form, calling a class related to that field, and sending a successful form to the server.

The AbstractController offers caching support. AbstractController can be extended and then implemented as a unique controller from scratch. It should only be used for simple purposes, such as returning a resource to the client without checking request parameters.

AbstractCommandController extends BaseCommandController. Abstract base class for custom command controllers. Auto populates a command bean from the request. For command validation, a validator (property inherited from BaseCommandController ) can be used.

CancellableFormController extends SimpleFormController. Extension of SimpleFormController that supports “cancellation” of form processing. By default, this controller looks for a given parameter in the request, identified by the cancelParamKey.

In the Spring MVC application, MultiActionController is used to group related actions into a single controller. In this way, other controllers that are similar to each other for any purpose can be combined.

Spring provides the ParameterizableViewController class, which is used for parameterizing view names in a configuration file instead of writing hard-coded view names in the handler method.

ServletForwardingController extends AbstractController implements BeanNameAware. Spring Controller implementation that forwards to a named servlet, i.e., the “servlet-name” in the web. XML rather than a URL path mapping. A target servlet doesn’t even need a “servlet-mapping” on the web.

ServletWrappingController is a class in the Spring API. Useful to invoke an existing servlet via Spring’s dispatching infrastructure, for example, to apply Spring HandlerInterceptors to its requests.

UrlFileNameViewController transforms the virtual path of a URL into a view name and returns that view JSP or HTML page. It binds incoming URLs with the View name automatically.

And many more……

Here I’ll only tell you the steps for testing a controller.

You can write a Unit Test for testing because it is easy for beginners. There are other ways for testing like Integration Tests, Server, and Client Side Tests, etc.

Here I’ll tell you the steps of the Unit Test in short. You have to find out details on the web.

We can do Unit Testing for a controller method by following the steps:

a) Create the test data which is returned when our service method is called.

b) Configure the used mock object to return the created test data when its findAll() method is called.

c) Execute a GET request to URL ‘/’.

This may seem complex to you as I only mentioned the steps. So, I’ll only demonstrate one Test of a controller. For this example, I’ll be testing SimpleFormController. Let’s start.

In this example I’ll be will be around some Person management system. First, assume we have a Person object :

Our controller will be configured in a context file like the below one.

The @Autowired annotation will do a “by-type” bean injection. This means that since our context contains a single bean of type myProject.web.EditPersonController, that bean (the one we named “/editPerson”) will be injected in the field “controller” at runtime before the test methods are called. But for now, the test just doesn’t compile because we haven’t created the EditPersonController class. So let’s create the controller :

Now the user is provided a form with two input fields. One for the “firstName” and one for the “lastName”. He fills those fields and submits the form. The controller is then called with the “firstName” and “lastName” posted by the user. It must normally save the person and return the “successView” which we named “personSaved” (see the XML context defined previously). Let’s now add this behavior to our test :

So far we’ve tested that while creating a new Person, the user first asks for the Person form (with an HTTP GET call), then submits that form (with an HTTP POST call that details the person’s information), and is redirected to a success page when finished. We can say we tested the navigation part of the “create new Person” use case.

To compile, I need to add a PersonService field and its getter/setter in the controller.

Now the test asserts the service has been called correctly. As we run it, we can see it fails with the following message :

This just means our controller didn’t call the service’s save method as expected. Let’s make our controller do its job by adding the expected call :

Running the test now should succeed. But, unfortunately, the bar is red again. For time consideration, I’ll leave aside the investigation and go right to the solution: we did not override the equals and hashcode methods of the Person class. Our controller that received the form submission did create a new Person, setting its first and last name. It then passed this new object to the doFormSubmitAction(Object) that called our mock passing the new person. But we told EasyMock we expected our service to be called with another Person object. The default implementation of the equals method in an object makes the following statement false :
(new Person(“Richard”, “Barabé”)).equals(new Person(“Richard”,“Barabé”))

So let’s implement those hashCodes and equals methods.

Now the code behaves correctly when we have two persons with the same fistName and lastName. And that green bar is back again.

These may seem complex to you but you can find this one and also other & every one of these controllers on the web with examples. As this is a short blog, I just summarized it.

You can also find other & every one of these controllers on the web with examples. As this is a short blog, I just summarized it.

Add a comment

Related posts:

Volume of a wheel

Math has been an integral part of almost every culture from the time civilization began. From the ancient Indians, to the ancient Greeks, every civilization gave importance to math. As vast and…

CONTROL STATEMENTS IN JAVA.

if statement is the most basic of all the control statements. It tells your program to execute a certain section of code only if a particular test evaluates to true. An if statement consists of a…

Todo santo dia

Meu pai acorda como se o sol dormisse com ele. Um desperta quando o outro se mexe. Nada de preguiça nem protesto, já que a rotina não é fardo, é bênção. Abre os olhos e habitua-se ao quarto que nasce…