Beans(POJOs)를 XML에 기술하는데신 Spring Framework 2.x 에서 제공하는 Annotation을 이용.
솔직히 코딩하는데 있어서는 Annotation이 편할 것 같은데 XML에 기술하는게 나중에 관리면에서 좋을 것 같다는 생각이 조금 든다.
* 출처 - http://jjaeko.tistory.com에 원문을 참고해서 작성함.
* 이클립스에서 소스 작성시 발생할 수 있는 예외 정리
* Springframework2.5 이용시 발생할 수 있는 예외 정리
*/
package com.test;
// AbstractDependencyInjectionSpringContextTests를 확장하지 않을 경우 아래 두줄
// 주석 해제 필요.
// import static org.junit.Assert.assertNotNull;
// import static org.junit.Assert.assertEquals;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/com/test/Beans.xml" })
public class ScanTest extends AbstractDependencyInjectionSpringContextTests {
/**
* Logger for this class
*/
private static final Log logger = LogFactory.getLog(ScanTest.class);
@Autowired
private Foo foo;
/**
* junit으로 메소드를 테스트 할 경우 test메소드명이지만 @Test annotation을 붙이
* 면 test메소드가 됨.
*/
@Test
public void fooTest() {
if (logger.isDebugEnabled()) {
logger.debug("fooTest() - start");
}
assertNotNull(foo.getBar());
assertEquals("Hello World", foo.greeting());
if (logger.isDebugEnabled()) {
logger.debug("fooTest() - end");
}
}
}
예외 정리
2. @ContextConfiguration에서 정의한 ScanTest-context.xml의 classpath 경로가 틀리거나 다른 이름일 경우(예-Beans.xml) java.lang.NoSuchMethodError: org.junit.internal.runners.MethodValidator.<init>(Lorg/junit/internal/runners/TestClass;) 발생함.
3. Eclipse3.3에 포함되어있는 JUnit의 경우 버전이 4.1이고 이를 사용하면 "@RunWith(SpringJUnit4ClassRunner.class)"부분에서 컴파일 에러가 발생함.
에러 내용 : Type mismatch: cannot convert from Class<SpringJUnit4ClassRunner> to Class<? extends Runner>
출처 : http://kekedie.tistory.com
0 Responses to [Spring Framework] Annotation Test(@Component, @Autowired, @ContextConfiguration)
Something to say?