How to write the junit test code for the following controller code

Ads

 
 

Share on Google+Share on Google+

Hari Prasanth
How to write the junit test code for the following controller code
0 Answer(s)      5 years ago
Posted in : Java Magazine
//Controller Code

package com.payoda.springs;

import java.util.ArrayList;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StartController {

    @RequestMapping(value = "/Volume", method = RequestMethod.GET)
    public ModelAndView Volume() {
        /*
         * List<String> country = new ArrayList<String>();
         * country.add("Number 1"); country.add("Number 2");
         * country.add("Number 3"); country.add("Number 4");
         * country.add("Number 5"); model.addAttribute("country", country);
         */
        System.out.println("country");
        return new ModelAndView("Volumetest", "command", new test());
    }

    @RequestMapping(value = "/sendto", method = RequestMethod.POST)
    public String addStudent(@ModelAttribute("command") test test,
            ModelMap model, BindingResult result) {

        Validatorset Valid = new Validatorset();
        Valid.validate(test, result);

        if (result.hasErrors())
            return "Volumetest";
        else
            model.addAttribute("name", test.getName());
        return "Volumereturn";

    }
}


//  Mock test code

package com.payoda.test;
import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;

import com.payoda.springs.StartController;
/*
 * 
 * Junit tests for Spring(Web level)
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
[email protected](locations={"file:{/WEB-INF/newprog-servlet.xml},{/WEB-INF/application-context.xml}" })
@ContextConfiguration(locations={ "file:WEB-INF/newprog-servlet.xml"})
public class MockTest {

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;
    private AnnotationMethodHandlerAdapter handlerAdapter;

    @Before
    public void setUp() throws Exception {
        this.request = new MockHttpServletRequest();
        this.response = new MockHttpServletResponse();
        handlerAdapter = new AnnotationMethodHandlerAdapter();
    }

    @Test
    public void testNewForm() throws Exception {
        request.setMethod("GET");
        request.setRequestURI("/Volume");
        final ModelAndView mav = handlerAdapter.handle(request, response,
                new StartController());
        Assert.assertEquals("user", mav.getViewName());
    }
}

//Could any1 pls explain the problem in my 'mock test code' becoz am getting the java.illegal.state: Failed to load application context...

//Awaiting for ur reply... urgent ...

Ads
View Answers
Ads









Related Tutorials/Questions & Answers:
How to write the junit test code for the following controller code
How to write the junit test code for the following controller code  //Controller Code package com.payoda.springs; import java.util.ArrayList... "Volumereturn"; } } // Mock test code package com.payoda.test
how to write code for this output?
how to write code for this output?   1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Advertisements
How to write jsp/servlet code to integrate LINKDIN?
How to write jsp/servlet code to integrate LINKDIN?  How integrate linkdin api's in java codding
How to write the code for date in swings - Struts
How to write the code for date in swings  Hi Friends, I want to code for display the calendar.......technologies use only swing and core java also how to display calendar like that popup window.....please write and send me
How to write a session code - Java Beginners
How to write a session code   Once Again Thanks Deepak...Thanks for continuing responce I want using session in my project plz help me how to write a session code plz write a session code and post answer my personal id
How to write a error.jsp code - Java Beginners
How to write a error.jsp code  Thanks once again I have a session code pls help me that how can i write a error.jsp page. why use error.jsp...let me know that hw can i write the error.jsp code. plz send me error.jsp code
how to write a programm in C for included code below
how to write a programm in C for included code below  Here is my question 'how to write a program in C that runs your sql-xml application session. In the session, you can run SQL queries interactively. The query results
source code for the following question
source code for the following question  source code for the fuzzy c-means
Expain the following code ?
Expain the following code ?  please explain the following line of code clearly? <html:link href="user.do?parameter=create">Create User</html:link>
How to update record to database? I have trouble with the following code
How to update record to database? I have trouble with the following code  I have written the update statement but it still not work to update the record,what is the missing of the following code ?I need help,thankyou <?php
Java code for following
Java code for following  Create a function that returns day difference between two dates (inclusive), without using any function provided by the platform or external library. The function must work for all dates in the range of 1
How to run ActionListenters in two different thread for the following code...
How to run ActionListenters in two different thread for the following code...  The ActionListeners here are not running concurrently... import java.io.*; import javax.swing.*; import java.awt.event.*; import
How to run ActionListenters in two different thread for the following code...
How to run ActionListenters in two different thread for the following code...  The ActionListeners here are not running concurrently... import java.io.*; import javax.swing.*; import java.awt.event.*; import
How to run ActionListenters in two different thread for the following code...
How to run ActionListenters in two different thread for the following code...  The ActionListeners here are not running concurrently... import java.io.*; import javax.swing.*; import java.awt.event.*; import
How to run ActionListenters in two different thread for the following code...
How to run ActionListenters in two different thread for the following code...  The ActionListeners here are not running concurrently... import java.io.*; import javax.swing.*; import java.awt.event.*; import
how to write weighted moving average code using 2d-array
how to write weighted moving average code using 2d-array  how to write weighted moving average code using 2d-array
help to write java code
help to write java code  write a full code to produce a system... of assignment 1,assignment 2, midterms-test and lave work to get total marks... 2: text-field /10 midterm-test: text-field /20 lab work: text-field /20
help to write java code
help to write java code  write a full code to produce a system... of assignment 1,assignment 2, midterms-test and lave work to get total marks... assignment 2: text-field /10 midterm-test: text-field /20 lab work: text-field /20
How to write Example code for comparing Strings in iPhone SDK?
How to write Example code for comparing Strings in iPhone SDK?  I... to compare the two string. Following code can be used: if([str1 isEqualToString... object. How to compare NSString object with another NSSTring object? Thanks  
core java code for following program
core java code for following program  We are hosting the Olympic games. Write a program that will track all the details of this hosting... should generate the following reports a. A medal tally b. For a given country
How to write Example code for CRUD application in hibernate annotation?
How to write Example code for CRUD application in hibernate annotation?  Learn Hibernate can be easy if anyone help me with the simple example code. How to write Example code for CRUD application in hibernate annotation? Share
write code in c#
write code in c#  sir I want to write a save and save as code in the c#(windosForm).Please help me
Need the Following MobileApplication Related Code
Need the Following MobileApplication Related Code  Hi, I need java coding for the following requirements in Collections.. Mobile Subscriber name... can provide code in switch cases,I feel more happy. Please reply me ASAP
Write JQUERY Code
Write JQUERY Code  Hi, Iam swathi.I created the table in the below format.can u please write the jquery code of this table..and my requirement... please send me the code through jquery..? Thank you Swathi
Write JQUERY Code
Write JQUERY Code  Hi, Iam swathi.I created the table in the below format.can u please write the jquery code of this table..and my requirement... please send me the code through jquery..? Thank you Swathi
how to write function of copy from one to another location in this code
how to write function of copy from one to another location in this code  I need to write a code to copy a file from one location to another using browse button in it. i have written code for the browse button. please help me
how to write a code for oracle database connection in asp.net for validation.
how to write a code for oracle database connection in asp.net for validation.  <script language="javascript" type="text/javascript"> function fill() { //<%@ import Namespace="System.Data" %> //<%@ import
how to code
how to code  how to create mysql coding in php
how to code this?
a toString() method that returns a String in the following form: Your total... at their choices. (Use your class method) Then following the menu, ask
how to code this?
in the following form: Your total for ENTEREE and DRINK is: ORDERCOST Example...) Then following the menu, ask the customer what entree they would like to order
Can i write JavaScript code in AjaxResponse Code ?
Can i write JavaScript code in AjaxResponse Code ?  Hai Every Dynamic's We can't write JavaScript code in Ajax Response Code.Why because it takes only html,json,xml response.I tried a lot to create js form in ajax response.It