File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes InternalResourceViewResolver failing JUnit test -- Spring Demo Source Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "InternalResourceViewResolver failing JUnit test -- Spring Demo Source" Watch "InternalResourceViewResolver failing JUnit test -- Spring Demo Source" New topic
Author

InternalResourceViewResolver failing JUnit test -- Spring Demo Source

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
Hello,

In the last part of the Spring demo, page 2 (http://static.springsource.org/docs/Spring-MVC-step-by-step/part2.html), I added the InternalViewResolver to the springapp-servlet.xml as in:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

Then, per the demo, I changed the HelloController and associated test to just use the logical name, hello

-----------------------
HelloController.java
------------------------


-------------------------------
HelloControllerTests.java
-------------------------------


But, when I run the tests, I get errors like this:

Testcase: testHandleRequestView(springapp.web.HelloControllerTests): FAILED
null expected:<[WEB-INF/jsp/hello.jsp]> but was:<[hello]>
junit.framework.ComparisonFailure: null expected:<[WEB-INF/jsp/hello.jsp]> but was:<[hello]>

--------------

If I redeploy the app, it still works fine, so what's up with the test failing? I literally copied and pasted the code in the three places to implement the InternalResourceViewResolver.

Thanks in advance for any suggestions.

-- mike

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Well your Unit test isn't running Spring, it is a Unit Test not an Integration Test, so there is no InternalResourceViewResolver instantiated, You are just testing the Controller as a Plain Java Object.

I do recommend looking at some of the newer documentation and either return a String or a domain object or void from your Controller method instead of ModelAndView. To make your methods even more POJO like, including not passing in HttpServletRequest and HttpServletResponse.

But either way, your test should still work. Try just printing out to the console the contents of the ModelAndView object and see what is actually in there and that might expose something

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
Thanks Mark.

The problem turned out to be that I needed to recycle Tomcat.

Appreciate your reply.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Mike London wrote:Thanks Mark.

The problem turned out to be that I needed to recycle Tomcat.

Appreciate your reply.


Why, the Unit Test isn't running in or through Tomcat? Unless you rigged it to not be a JUnit Unit Test and have some manual intervention.

Or was it some Eclipse IDE in memory/classloader problem that even though the Unit Test has nothing to do with Tomcat directly, it was being affected?

Please let us know.

Thanks

Mark
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
Mark Spritzler wrote:
Mike London wrote:Thanks Mark.

The problem turned out to be that I needed to recycle Tomcat.

Appreciate your reply.


Why, the Unit Test isn't running in or through Tomcat? Unless you rigged it to not be a JUnit Unit Test and have some manual intervention.

Or was it some Eclipse IDE in memory/classloader problem that even though the Unit Test has nothing to do with Tomcat directly, it was being affected?

Please let us know.

Thanks

Mark


Hi Mark,

I think it was some type of memory/classloader issue with Tomcat, not JUnit. I had deleted and redeployed the application, but it wasn't until I recycled Tomcat that the changes were there.

Thanks very much for your follow up.

-m
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Mike London wrote:
Mark Spritzler wrote:
Mike London wrote:Thanks Mark.

The problem turned out to be that I needed to recycle Tomcat.

Appreciate your reply.


Why, the Unit Test isn't running in or through Tomcat? Unless you rigged it to not be a JUnit Unit Test and have some manual intervention.

Or was it some Eclipse IDE in memory/classloader problem that even though the Unit Test has nothing to do with Tomcat directly, it was being affected?

Please let us know.

Thanks

Mark


But that's what I mean, a Unit test doesn't run in Tomcat, so you shouldn't need anything in Tomcat to get a Unit test to run. What happens if you just stop Tomcat then run your unit test?

Thanks

Mark

Hi Mark,

I think it was some type of memory/classloader issue with Tomcat, not JUnit. I had deleted and redeployed the application, but it wasn't until I recycled Tomcat that the changes were there.

Thanks very much for your follow up.

-m
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
Understood, but the problem was that the Unit test was testing a view resolver mapping that was changed in the mvc-servlet.xml file. The JUnit wasn't testing Java code by itself.

-mike
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Mike London wrote:Understood, but the problem was that the Unit test was testing a view resolver mapping that was changed in the mvc-servlet.xml file. The JUnit wasn't testing Java code by itself.

-mike


But again, there view resolver wasn't even running in the Unit Test. If you made the unit test an integration test with Spring, then I would agree, but I didn't see any code extending AbstractDependencyInjectionSpringTestClass… or @ContextConfiguration and @RunWith on your Unit Test which would bootstrap Spring.

So, if you were testing a view resolver where and how would it use it? I am missing something here that I would like to know.

Mark
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
Hi Mark,

You're asking great questions, but ....

The problem is that I'm trying to _learn_ Spring by this MVC example, which is proving to be difficult due to the typos (page 6 - "springapp/src/springapp/service/SimpleProductManager.java"), and all the "XML-Hell"---everywhere. This tutorial is further complicated with all the Jars that don't match 2.5's Spring implementation so much detective work to get classpath correct.

--------------------

I had things working up to page 5, but now after implementing (but not totally understanding) page 6's new (sadly, more) XML files, I get the exception below when clicking submit to increase the price (I have HSQLDB running so the prices come up initially).

Any ideas?

Thanks.

-- M

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [update products set description = ?, price = ? where id = ?]; SQL state [25006]; error code [-3706]; invalid transaction state: read-only SQL-transaction; nested exception is java.sql.SQLException: invalid transaction state: read-only SQL-transaction
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [update products set description = ?, price = ? where id = ?]; SQL state [25006]; error code [-3706]; invalid transaction state: read-only SQL-transaction; nested exception is java.sql.SQLException: invalid transaction state: read-only SQL-transaction
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:83)
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602)
org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:786)
org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:808)
org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:233)
org.springframework.jdbc.core.simple.SimpleJdbcTemplate.update(SimpleJdbcTemplate.java:243)
springapp.repository.JdbcProductDao.saveProduct(JdbcProductDao.java:37)
springapp.service.SimpleProductManager.increasePrice(SimpleProductManager.java:27)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
$Proxy75.increasePrice(Unknown Source)
springapp.web.PriceIncreaseFormController.onSubmit(PriceIncreaseFormController.java:31)
org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:415)
org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:387)
org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:272)
org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:268)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.sql.SQLException: invalid transaction state: read-only SQL-transaction
org.hsqldb.jdbc.Util.sqlException(Util.java:215)
org.hsqldb.jdbc.JDBCPreparedStatement.<init>(JDBCPreparedStatement.java:3957)
org.hsqldb.jdbc.JDBCConnection.prepareStatement(JDBCConnection.java:637)
org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:281)
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:313)
org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:245)
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:580)
org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:786)
org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:808)
org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:233)
org.springframework.jdbc.core.simple.SimpleJdbcTemplate.update(SimpleJdbcTemplate.java:243)
springapp.repository.JdbcProductDao.saveProduct(JdbcProductDao.java:37)
springapp.service.SimpleProductManager.increasePrice(SimpleProductManager.java:27)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
$Proxy75.increasePrice(Unknown Source)
springapp.web.PriceIncreaseFormController.onSubmit(PriceIncreaseFormController.java:31)
org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:415)
org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:387)
org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:272)
org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:268)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

So do you want to use 2.5 or the latest in that line of 2.x which is 2.5.6

Or do you want to learn the latest Spring which is Spring 3.x.

Basically you need to match the jar versions with the Documentation version to make sure that the tutorial will work.

For instance, in today's Spring MVC you shouldn't implement the Controller interface because now that class is coupled to Spring. Spring would rather you use the Annotation approach where a Controller class is a POJO and you can many methods in it to handle many Requests.

for instance

I could re-write the controller as


For the second method, the internalViewResolver would look for a jsp file called listOrders.jsp

Not to add confusion to you. ;)

Mark
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
Interesting, thanks.

Hopefully, there will be an updated tutorial for Spring 3 soon.

Wouldn't you be learning Spring 3 if you were staring now? Not sure, but it sounds like 3 is the way to go.

Thanks.

- M

P.S. Have you looked at MyEclipse for Spring? Supposedly, it will set up much of the app for you, but I'm not sure how useful it is when you need to create new forms and business logic...
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Yes, I highly recommend learning 3.0 if starting out. There are many improvements to decouple your code from anything Spring related to truly have decoupled, highly cohesive classes and easier simpler configurations.

I use IntelliJ for my IDE of choice. And today, one of the best ways to start a Spring application is using Spring Roo to do it. I don't like relying on an IDE to set up that app because it usually ties your build to the actual IDE, Eclipse does this alot. I like to be in control of my build so I know what is actually happened so that when it doesn't deploy correctly it is because of a mistake I made and not a mistake from the IDE. I have spent many hours trying to fix IDE mistakes instead of working on my code and my own mistakes. ;)

Mark
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
Thanks again Mark for all your great replies!!!

- M

Shahar Muky
Greenhorn

Joined: May 19, 2011
Posts: 7
I also encountered the same issue when working on this tutorial, Is there a solution for this issue?

Second, can anyone send me a reference to a good tutorial for spring 3?

Thanks
Muky.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
Shahar Muky wrote:I also encountered the same issue when working on this tutorial, Is there a solution for this issue?

Second, can anyone send me a reference to a good tutorial for spring 3?

Thanks
Muky.


As I recall, I never got that working. So, I just skipped implementing the tests.

For Spring 3, I haven't really found any tutorials.

Check out MyEclipse for Spring ($99/yr. currently). It is AMAZING! It will literally scaffold an entire working application from the database (with working JSPs) in a couple minutes (including the layers like DAO, Service, etc.). And, it works! Then you can concentrate on learning the various pieces. The other IDEs expect you to write a lot more code or use Roo, which kept crashing when I tried it in another popular IDE. For me, ROO was painful and something I'd rather not use.

(of course, these are just my personal opinions)

For books, check out Spring Recipes 3 and the soon-to-be-released "Spring in Action" (June 28, 2011).

Also, consider taking the Core Spring class. The Sheriff here, Mark, is a SpringSource instructor so he can jump in and let you know more about that class.

HTH.

mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: InternalResourceViewResolver failing JUnit test -- Spring Demo Source