[Spring] Junit : tiles.xml

종원유·2022년 2월 9일
0

Junit

목록 보기
2/2
post-thumbnail

Junit4 Tiles 에러

환경

  • Junit4
  • Spring-test
@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "classpath*:jf-core/spring/context-*.xml",
									"file:src/main/webapp/WEB-INF/config/jf-core/springmvc/dispatcher-servlet.xml"})

@WebAppConfiguration("file:src/main/webapp")
public class SpringTestSupport {
	
	@Autowired
	private CmmnService cmmnService;
	
	@Test
	public void TestBean() {
		assertNotNull(cmmnService);
	}
	
	@Test
	public void loginTest() {
		List<EgovMap> list = cmmnService.selectCommonCode(CommonCodeConstant.CA008);
	}
	
}

문제의 부분을 단위 테스트 하기 위해서 테스트 케이스를 만들던 도중

아래와 같은 에러를 만나게 되었다.

java.lang.IllegalStateException: Failed to load ApplicationContext
	at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)
	at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)
	at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:170)
	at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:110)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:252)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:254)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:217)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tilesConfigurer' defined in URL [file:src/main/webapp/WEB-INF/config/jf-core/springmvc/dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/JspFactory
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)
	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.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:133)
	at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:60)
	at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109)
	at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261)
	at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:68)
	at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86)
	... 25 more
Caused by: java.lang.NoClassDefFoundError: javax/servlet/jsp/JspFactory
	at org.springframework.web.servlet.view.tiles3.TilesConfigurer$SpringTilesContainerFactory.createAttributeEvaluatorFactory(TilesConfigurer.java:377)
	at org.apache.tiles.factory.BasicTilesContainerFactory.createContainer(BasicTilesContainerFactory.java:88)
	at org.springframework.web.servlet.view.tiles3.TilesConfigurer$SpringTilesContainerFactory.createContainer(TilesConfigurer.java:297)
	at org.apache.tiles.startup.AbstractTilesInitializer.createContainer(AbstractTilesInitializer.java:114)
	at org.apache.tiles.startup.AbstractTilesInitializer.initialize(AbstractTilesInitializer.java:64)
	at org.springframework.web.servlet.view.tiles3.TilesConfigurer.afterPropertiesSet(TilesConfigurer.java:271)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564)
	... 40 more
Caused by: java.lang.ClassNotFoundException: javax.servlet.jsp.JspFactory
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	... 48 more

아이러니하게도 단위 유닛 테스트인데도 불구하고 엉뚱하게 tiles에서 문제가 발생하였다.

위 에러에 대해서 찾아보니 Spring-Tiles는 웹을 기반으로 동작하다보니 Junit 실행 시 Context를 Load하는데 문제가 발생하였다는 오류였다.

수정

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:jf-core/spring/context-*.xml")=
@WebAppConfiguration("file:src/main/webapp")
public class SpringTestSupport {
	
	@Autowired
	private CmmnService cmmnService;
	
	@Test
	public void TestBean() {
		assertNotNull(cmmnService);
	}
	
	@Test
	public void loginTest() {
		List<EgovMap> list = cmmnService.selectCommonCode(CommonCodeConstant.CA008);
	}
	
}

위 코드로 수정하였다.
변경 부분은 @ContextConfiguration 부분의 dispatcher-servlet.xml을 location에서 제거하였다.

####변경 전

@ContextConfiguration(locations = { "classpath*:jf-core/spring/context-*.xml",
									"file:src/main/webapp/WEB-INF/config/jf-core/springmvc/dispatcher-servlet.xml"})

변경 후

@ContextConfiguration(locations = "classpath*:jf-core/spring/context-*.xml")

Dispatcher-servlet.xml을 location에서 제외하니 테스트가 정상적으로 작동한다.

profile
개발자 호소인

0개의 댓글