2011-04-28 54 views
4

有一個用於測試Struts2動作的struts2-junit插件,但它似乎需要測試以JUnit 3風格編寫,也就是說我不能使用@BeforeClass,@Test等註釋,並且不能使用BDD-樣式測試名稱,如「shouldDoThisAndThat()」。有沒有辦法將struts2-junit-plugin和JUnit 4-style測試結合起來?如何在Junit 4中測試Struts2的行爲?

回答

1

我有同樣的問題,這裏是我如何解決它,它是一個非常簡單的基類。

在我的情況下,我使用休眠,所以如果不使用它,請忽略會話/事務處理。 這讓我們繼續使用我發現的所有模擬struts對象。一個簡單的測試案例如下。

我仍然需要弄清楚如何注入資源包(i18n字符串)......我將添加到基類。

package com.accelarad.unittest; 

import org.apache.struts2.StrutsTestCase; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.junit.AfterClass; 
import org.junit.Before; 
import org.junit.BeforeClass; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.ApplicationContextAware; 
import org.springframework.orm.hibernate3.SessionFactoryUtils; 
import org.springframework.orm.hibernate3.SessionHolder; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.transaction.support.TransactionSynchronizationManager; 
import org.springframework.web.context.WebApplicationContext; 

/** 
* Bring Struts JUnit3 into the JUnit4 world 
* 
* @author kcostilow 
* 
*/ 
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration({ 
    "classpath:spring-datasource-unittest.xml", 
    "classpath:spring-test-hibernate.xml", 
    "classpath:spring-context.xml", 
    "classpath:spring-security.xml", 
    "classpath:spring-server.xml" 
    } 
) 
public abstract class AbstractSpringStruts2JUnit4 extends StrutsTestCase implements ApplicationContextAware { 

    protected ApplicationContext applicationContext; 

    @Autowired 
    protected SessionFactory sessionFactory; 

    // implements ApplicationContextAware 
    public void setApplicationContext(ApplicationContext applicationContext) { 
     this.applicationContext = applicationContext; 
    } 

    @BeforeClass 
    public static void initialize() { 
     // in my case, I create a mock JNDI here, 
     // including a mailSession using a Wiser mock SMTP server 
    } 

    @Before 
    public void setUp() throws Exception { 
     super.setUp(); // use JUnit3 setUp chain. In this case setupBeforeInitDispatcher() will be called 
     if (! TransactionSynchronizationManager.hasResource(sessionFactory)) { 
      Session session = SessionFactoryUtils.getSession(sessionFactory, true); // first @Before only 
      TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); 
     } 
    } 

    @Override 
    protected void setupBeforeInitDispatcher() throws Exception { 
     servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext); 
     // inject hibernate sessionFactory into Transaction Management, simulating OpenSessionInView 
    } 

    @AfterClass 
    public static void shutdown() { 
     // in my case, I shutdown the mock mail server here 
    } 
} 

樣品測試案例:

public class MyActionTest extends AbstractSpringStruts2JUnit4 { 
    @Test 
    public void testOneStrutsAction() throws Exception { 
     ActionProxy proxy = super.getActionProxy("/packageName/actionName"); 
     TestCase.assertEquals("actionName result", "success", proxy.execute()); 

     MyAction myAction = (MyAction) proxy.getAction(); 
     TestCase.assertNotNull(myAction.getSomething()); 
    } 
} 

你可以把它從那裏我想。

+0

重新國際化:從Struts文件已經在我的類路徑中,上面的結構已經引入了我的字符串屬性文件,所以我不必爲getText()工作額外做任何事情。 – kcostilow 2011-10-19 15:21:19

0

對於其他人誰降落在這裏尋找如何測試使用JUnit Struts2的行動,這是一個使用Struts2.3.12 & JUnit 4中

public class TestIoOptimizationAction extends StrutsJUnit4TestCase<IoOptimizationAction> { 

    @Test 
    public void login() throws Exception { 
     request.setParameter("email", "[email protected]"); 
     request.setParameter("password", "22"); 

     ActionProxy proxy = getActionProxy("login"); 
     Map<String, Object> sessionMap = new HashMap<String, Object>(); 
     proxy.getInvocation().getInvocationContext().setSession(sessionMap); 
     String result = proxy.execute(); 
//  String result = executeAction("/login"); 
     assertEquals("success", result); 
     System.out.println(response.getContentAsString()); 
    } 
} 

我必須單獨嵌入會話映射一些示例代碼,因爲我使用getActionProxy()和這裏的reason

0

你的測試類應該擴展StrutsSpringJUnit4TestCase。

0

我偷kcostilow一點點,coding_idiot的解答和StrutsJUnit4TestCase創建一個抽象類:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration({ 
    "classpath:spring-datasource-unittest.xml", 
    "classpath:spring-test-hibernate.xml", 
    "classpath:spring-context.xml", 
    "classpath:spring-security.xml", 
    "classpath:spring-server.xml" 
    } 
) 
@Transactional 
@TransactionConfiguration(transactionManager = "dataSourceTransactionManager") 
public abstract class AbstractSpringStruts2JUnit4<T extends ActionSupport> extends StrutsJUnit4TestCase<T> { 

    @Autowired 
    @Qualifier("dataSource") 
    public void setDataSource(DataSource dataSource) { 
     this.dataSource= dataSource; 
    } 

    @Before 
    public void onSetUp() throws Exception { 
     super.setUp(); 
     setupAction(); 
    } 

    protected abstract void setupAction(); 

} 

然後擴展的類來創建測試:

public class SomeActionIntegrationTest extends AbstractSpringStruts2JUnit4<SomeAction> { 
    private SomeAction someAction; // SomeAction must extend ActionSupport. 

    @Override 
    protected void setupAction() { 
     ActionProxy proxy = super.getActionProxy("/someAction"); 
     // You might have* to add ".do" to the end of the URI. 
     So, super.getActionProxy("/someAction.do") was the only way I was able to get the proxy. 

     someAction = (ActionSupport) proxy.getAction(); 
     assertNotNull(someAction); 
    } 

    @Test 
    public void testExecute() throws Exception { 
     String result = someAction.execute(); 
     assertEquals("input", result); 
    } 
} 
相關問題