
//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)
//@ContextConfiguration(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 ...
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.