2
votes

UPDATED

I seem to have done something uniquely wrong as I cannot find reference to this problem.

I am trying to integrate tiles into an extant spring thymeleaf/webflow/hibernate app.

Everything works fine until I add the tilesConfigurer to my applicationContext:

<!-- Configures the Tiles layout system-->
	


<bean id="tilesConfigurer" class="org.thymeleaf.extras.tiles2.spring4.web.configurer.ThymeleafTilesConfigurer">
	<property name="definitions">
		<list>
			<value>/WEB-INF/**/views.xml</value>
		</list>
	</property>
</bean>	

After adding the configurer, my test fail, but the app still runs in tomcat8.

Here is a typical test class:

package jake.prototype2.test.service;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import jake.prototype2.model.business.Business;
import jake.prototype2.service.user.BusinessService;
import jake.prototype2.test.testrunner.TestSS;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:AssessmentAdminTL-servlet.xml")
public class BusinessServiceTest
{
@Autowired
private BusinessService businessService;

@Test
public void testGetBusinesses() {
    TestSS.getLogger().info("testGetBusinesses");
    List <Business> blist=businessService.getBusinesses();
    assertTrue(blist.size()>0);
}

@Test
public void testGetBusiness() {
    TestSS.getLogger().info("testGetBusinesses");
    Business b=null;
    try
    {
        b=businessService.getBusiness(TestSS.getBusiness().getId());
    }
    catch(Exception e)
    {
        TestSS.getLogger().error(e.getMessage(),e);
        fail();
    }
     assertFalse(b==null);
}
}

The resource is also specified in the applicationContext:

<context:component-scan base-package="jake.prototype2" />
	<mvc:resources mapping="/style/**" location="/style/" />
	<mvc:resources mapping="/images/**" location="/images/" />
	<mvc:resources mapping="/views/**" location="/WEB-INF/views/" />
	<mvc:annotation-driven />

web.xml specifes default servlet path:

 
   <servlet>
      <servlet-name>AssessmentAdminTL</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
 
   <servlet-mapping>
    <servlet-name>AssessmentAdminTL</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

I get the following exception on app initialization:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tilesConfigurer' defined in URL [file:/C:/Users/jake/workspace/_ pt2/WebContent/WEB-INF/config/views_applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Cannot resolve S ervletContextResource without ServletContext at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:125) at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(Abs

Again, I note, everything works fine until the tilesconfigurer code is inserted. Any help would be much appreciated.

1
The exception is pretty clear imho. You are trying to use web classes in a non web environment (your unit test). You are only testing your service I suggest splitting your web and business related configurations and only load what is needed. If you want to test your controllers and web related stuff add @WebApplicationContext. - M. Deinum
thanks. I'll test and if it works, you should post this as answer. - Jake

1 Answers

2
votes

with a lot f help from @M.Deinum the answer is that I was calling the context incorrectly.

My test class annotation should have been as follows:

@WebAppConfiguration
@ContextHierarchy({
@ContextConfiguration(locations = { "classpath:AssessmentAdminTL-servlet.xml" })
})

However, while this solved the problem specified here, I still have other path related problems with tilesconfigurer, (probably due to my lack of a mock context?)

In short, this serves as a very specific answer to a very specific problem. It is not the whole story.