A robust starter web application to ease Java webapp development.

Home | Tutorials | Demos | Issues

 « Return to Thread: GenericManager's getAll returns empty list in controller test?

Re: GenericManager's getAll returns empty list in controller test?

by Ger-Jan te Dorsthorst :: Rate this Message:

Reply to Author | View in Thread

Oops; just noticed that I deleted too many lines from the code I sent
earlier. It's missing the debug lines I referred to. Here is what I
meant to send:

package nl.rivm.cda.webapp.controller;

import nl.rivm.cda.model.Download;
import nl.rivm.cda.webapp.controller.BaseControllerTestCase;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;

public class DownloadControllerTest extends BaseControllerTestCase {

        private DownloadController controller;

     public void setDownloadController(DownloadController controller) {
         this.controller = controller;
     }

        public void testHandleRequest() throws Exception {
      log.debug("controller = "+controller.getClass().getCanonicalName());
         ModelAndView mav = controller.handleRequest(null, null);
         ModelMap m = mav.getModelMap();
         List<Download> downloads = (List<Download>) m.get("downloadList");
         log.debug("string repres of download list (+ size) in model map
= "+downloads.toString()+" ("+downloads.size()+")");
         assertNotNull(m.get("downloadList"));
         assertTrue(((List) m.get("downloadList")).size() > 0);
     }

}

Ger-Jan te Dorsthorst wrote:

> Hello Matt, list,
>
> Matt Raible wrote:
>
>> Can you post the code for your DownloadControllerTest?
>
> Here's the code. It is what is generated by the AF maven plugin plus a
> couple of added debug lines, to verify that the controller is wired
> correctly and that the downloadList retrieved from the modelMap is in
> fact empty.
>
> package nl.rivm.cda.webapp.controller;
>
> import nl.rivm.cda.webapp.controller.BaseControllerTestCase;
> import org.springframework.ui.ModelMap;
> import org.springframework.web.servlet.ModelAndView;
> import java.util.List;
>
> public class DownloadControllerTest extends BaseControllerTestCase {
>
>     private DownloadController controller;
>
>     public void setDownloadController(DownloadController controller) {
>         this.controller = controller;
>     }
>
>     public void testHandleRequest() throws Exception {
>         ModelAndView mav = controller.handleRequest(null, null);
>         ModelMap m = mav.getModelMap();
>         assertNotNull(m.get("downloadList"));
>         assertTrue(((List) m.get("downloadList")).size() > 0);
>     }
>
> }
>
> Running this with the following command:
>
> gerjan@stammetje:~/workspace/cda$ mvn clean test -Ppostgresql
> -Dtest=DownloadControllerTest -Dsurefire.useFile=false
>
> Gives:
>
> Running nl.rivm.cda.webapp.controller.DownloadControllerTest
> INFO - BaseControllerTestCase.loadContextLocations(57) | Loading
> additional configuration from:
> classpath:/applicationContext-resources.xml,classpath:/applicationContext-dao.xml,classpath:/applicationContext-service.xml,classpath*:/applicationContext.xml,/WEB-INF/applicationContext*.xml,/WEB-INF/dispatcher-servlet.xml
>
> DEBUG - AbstractTransactionalSpringContextTests.startNewTransaction(392)
> | Began transaction (1): transaction manager
> [org.springframework.orm.hibernate3.HibernateTransactionManager@1ac13d7];
> rollback [true].
> DEBUG - DownloadControllerTest.testHandleRequest(18) | controller =
> nl.rivm.cda.webapp.controller.DownloadController
> DEBUG - DownloadControllerTest.testHandleRequest(22) | string repres of
> download list (+ size) in model map = [] (0)
> DEBUG - AbstractTransactionalSpringContextTests.endTransaction(360) |
> Rolled back transaction after execution of test [testHandleRequest].
> Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 2.326
> sec <<< FAILURE!
> testHandleRequest(nl.rivm.cda.webapp.controller.DownloadControllerTest)
>  Time elapsed: 2.306 sec  <<< FAILURE!
> junit.framework.AssertionFailedError: null
>     at junit.framework.Assert.fail(Assert.java:47)
>     at junit.framework.Assert.assertTrue(Assert.java:20)
>     at junit.framework.Assert.assertTrue(Assert.java:27)
>     at
> nl.rivm.cda.webapp.controller.DownloadControllerTest.testHandleRequest(DownloadControllerTest.java:24)
>
> [...]
>
>  > Are you certain
>> there's data for your "downloads" table in sample-data.xml?
>
> There is, and dbunit inserts it properly. If I subsequently run mvn
> jetty:run-war -Ppostgresql -Dmaven.test.skip=true, everything works as
> expected (= the downloadList inserted by the DownloadController in the
> ModelAndView's modelMap contains the three Downloads corresponding with
> the elements in sample-data.xml)
>
>>
>> On Wed, Jun 24, 2009 at 5:18 AM, <gerjan@...> wrote:
>>> Hi,
>>>
>>> I seem to have a problem retrieving objects from the database, but
>>> only when
>>> running a test. I added a model class 'Download' and generated CRUD
>>> using
>>> the appfuse maven plugin. All works fine when I run with
>>> maven.test.skip=true, but if I attempt to run with tests, or try to
>>> run the
>>> offending test in isolation (mvn test -Dtest=DownloadControllerTest),
>>> maven
>>> bails out with a test failure.
>>>
>>> The problem seems to be that when testing, the call to getAll() on
>>> downloadManager (an instance of GenericManager<Download, Long>) in the
>>> controller returns an empty List<Download>.
>>>
>>> What could be the cause of this happening only when testing?
>>>
>>> My DownloadController's handleRequest looks like this:
>>>
>>> ModelAndView mav = new ModelAndView();
>>> List<Download> downloads = downloadManager.getAll();
>>> log.debug("in downloadcontroller's handleRequest");
>>> log.debug("list of downloads retrieved from downloadmgr:
>>> "+downloads.toString()+"("+downloads.size()+")");
>>> mav.addObject("downloadList", downloads);
>>> return mav;
>>>
>>> When I run mvn jetty:run-war -Dmaven.skip.test=true I get this:
>>>
>>> [cda]DEBUG [btpool0-1] DownloadController.handleRequest(32) | in
>>> downloadcontroller's handleRequest
>>> [cda]DEBUG [btpool0-1] DownloadController.handleRequest(33) | list of
>>> downloads retrieved from dlmgr:
>>> [nl.rivm.cda.model.Download@89b953...,nl.rivm.cda.model.Download@abab54...,nl.rivm.cda.model.Download@7d32cf...](3)
>>>
>>>
>>> Whereas running mvn test -Dtest=DownloadControllerTest gives:
>>>
>>> DEBUG - DownloadController.handleRequest(32) | in downloadcontroller's
>>> handleRequest
>>> DEBUG - DownloadController.handleRequest(33) | list of downloads
>>> retrieved
>>> from dlmgr: [](0)
>>>
>>> Tia,
>>>
>>> Ger-Jan
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@...
>>> For additional commands, e-mail: users-help@...
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@...
>> For additional commands, e-mail: users-help@...
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@...
> For additional commands, e-mail: users-help@...
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...

 « Return to Thread: GenericManager's getAll returns empty list in controller test?