PDA

View Full Version : TDD newbie


kantorn
Oct 5th, 2004, 07:28 AM
Hi all

I'm currently exploring the recommended "test first" approach in Spring development, and I must admit I'm fascinated by all the possibilities theis method has.

But I'm also a newbie in this way of thinking, and have run into problems

I wonder why

private MockControl control = null;
private LogicFacade mockLogic = null;
private DateSearchFormController datefc = null;
private MockHttpServletRequest mockreq = null;


protected void setUp() throws Exception {
control = MockControl.createControl(LogicFacade.class);
mockLogic = (LogicFacade) control.getMock();
datefc = new DateSearchFormController();
datefc.setLogic(mockLogic);
mockreq = new MockHttpServletRequest();
}

protected void tearDown() throws Exception {
control = null;
mockLogic = null;
datefc = null;
mockreq = null;
}

public void testLoginCheck() throws Exception {
MockHttpSession mockSession = new MockHttpSession();
MockControl sessionControl = MockControl.createControl(UserSession.class);
UserSession session = (UserSession) sessionControl.getMock();

AvikUser user = new AvikUser();
user.setRole(AvikRole.POWERUSER);
session.setUser(user);

mockSession.setAttribute("usersession", session);
mockreq.setAttribute("something", new Object());
mockreq.setSession(mockSession);
datefc.handleRequest( mockreq , new MockHttpServletResponse() );

}

generates the following exception

[junit] Testcase: testLoginCheck(ks.rah.avik2.test.TestSearchControl ler): Caused an ERROR
[junit] Request method 'null' not supported
[junit] org.springframework.web.servlet.support.RequestMet hodNotSupportedException: Request method 'null' not suppor
ted
[junit] at org.springframework.web.servlet.support.WebContent Generator.checkAndPrepare(WebContentGenerator.java :
199)
[junit] at org.springframework.web.servlet.support.WebContent Generator.checkAndPrepare(WebContentGenerator.java :
178)
[junit] at org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(AbstractController.java:109)
[junit] at ks.rah.avik2.test.TestSearchController.testLoginCh eck(TestSearchController.java:61)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)

testing this code


protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors) throws Exception {
DateSearchForm form = (DateSearchForm) command;
AvikUser user = AvikController.checkLoggedIn(request, AvikRole.POWERUSER, false);
.
.
.

Seems like the MockHttpServletRequest/Response is null somehow.....

Anybody have an idea?

Colin Sampaleanu
Oct 5th, 2004, 11:25 AM
You are using the constructor for MockHttpServletRequest which does not specify any 'method' string ('GET' or 'POST'), so you need to set this yourself manually as a property.

kantorn
Oct 5th, 2004, 11:55 AM
That did it.

Thanks!