HelloWorldAction을 JUnit으로 테스트 하기.
Action 테스트 하기
서블릿과 Struts1의
Action은 서블릿 API에 의존적이므로 Servlet Container내에서만 테스트가 가능하다. 하지만 struts2의 액션은 일반
POJO 클래스를 테스트 하는 것처럼 하면 된다. 이전에 작성한 HelloWorldAction을 JUnit으로 단위 테스트를 해 보자.
아래 예제를 실행하기 위해서는 http://www.junit.org에서 junit 최신 버전(junit-4.5.jar)을 다운받아
library path에 추가해야 한다.
package hello;
import static
org.junit.Assert.assertTrue;
import org.junit.Test;
import
com.opensymphony.xwork2.Action;
public class HelloWorldTest {
@Test
public void testHelloWorldAction() throws Exception {
HelloWorldAction h = new HelloWorldAction();
h.setName("JCLEE");
String result =
h.execute();
String msg = h.getMsg();
//Action을 execute 했을 때 정상적이라면 SUCCESS가
리턴됨
assertTrue(Action.SUCCESS.equals(result));
//name에 "JCLEE"라는 값을 주고 Action을 execute 했을 때
//정상적이라면 msg는 "Hello,JCLEE"임
assertTrue("Hello,JCLEE".equals(msg));
}
}
댓글 없음:
댓글 쓰기