我有一組寫入JUnit
的測試用例。由於這些測試用例依賴於servlet容器,因此我想從servlet
運行它們。例如,如果我將Test類的完全限定類名稱傳遞給servlet,它應該能夠運行該測試用例。 我在Web應用程序的類路徑中有這些測試用例。JUnit - 從Web應用程序運行測試用例
1
A
回答
0
/**
* The various JUNIT classes should be passed as the <code>class</code> request paramter in the URL.
* @param request
* @param response
*/
private void executeJUNITTestCases(HttpServletRequest request, HttpServletResponse response){
response.setContentType("text/plain");
//Pass the class names of the Test cases in the URL parameter "class"
String[] className = request.getParameterValues("class");
try{
if(className = null || className.length == 0){
PrintWriter writer = response.getWriter();
JSONObject obj = new JSONObject();
obj.put("error", "Class names should be provided as parameter values of key [class]");
writer.write(obj.toString());
return;
}
OutputStream out = response.getOutputStream();
final PrintStream pout = new PrintStream(out);
new JUnitCore().runMain(new JUnitSystem(){
public PrintStream out(){
return pout;
}
public void exit(int arg0){}
}, className);
out.close();
}
catch(IOException e){
e.printStackTrace();
}
上面的代碼可以幫助我們調用Web應用程序上下文中的JUnit測試用例。
2
不知道爲什麼你需要一個servlet容器,但通常最好是模擬環境,以便控制並可以模擬不同的情況。
在以下文檔Unit testing of servlet using mock framework (MOCKITO)內解釋瞭如何使用mockito對servlet進行單元測試,並且此問題也可能與How to test my servlet using JUnit相關。
相關問題
- 1. 從java web應用程序運行JUnit測試
- 2. Spring的web應用程序中運行JUnit測試時
- 3. 運行JUNIT測試用例時出錯
- 4. 運行Junit測試用例時OutofMemoryError
- 5. 同時運行JUnit測試用例
- 6. 的build.xml不運行JUnit測試用例
- 7. 運行JUnit測試用例時加載應用程序競爭錯誤失敗
- 8. 在Spring應用程序中運行junit測試時訪問h2 web控制檯
- 9. 使用JUnit測試Swing應用程序
- 10. 如何從命令行在JUnit中運行測試用例?
- 11. 如何獲取測試類實例JUnit從ClassRunner內部運行測試用例?
- 12. Junit測試用例
- 13. 使用jUnit或其他api測試web應用程序?
- 14. 使用spring和junit測試Web應用程序羣集
- 15. 執行web應用程序的所有測試用例
- 16. 測試Web應用程序
- 17. Web應用程序測試
- 18. 測試Web應用程序?
- 19. Spring MVC應用程序Junit測試用例失敗
- 20. 如何使JUnit測試用例按順序運行?
- 21. JUnit應用程序套件測試
- 22. Android JUnit測試應用程序類
- 23. Spring應用程序中的JUnit測試
- 24. 如何在運行框架中運行JUnit測試用例2.3.2
- 25. 從多個類文件運行junit測試用例(Robotium)
- 26. 在參數化junit測試用例中運行特定測試用例
- 27. junit測試用例運行後,我應該刪除與此測試用例有關的測試數據嗎?
- 28. 從Junit測試運行時,Spring Boot應用程序拋出異常
- 29. 如何從我的Java應用程序內部運行JUnit測試?
- 30. 與junit的開源Web應用程序測試?
如果我們必須測試涉及多個資源的交易,那麼嘲笑環境可能還不夠。 – Apps