对 Java Servlet 进行单元测试 [英] Unit testing a Java Servlet

查看:43
本文介绍了对 Java Servlet 进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道对 servlet 进行单元测试的最佳方法是什么.

I would like to know what would be the best way to do unit testing of a servlet.

测试内部方法只要不引用servlet上下文就不是问题,但是测试doGet/doPost方法以及引用上下文或使用会话参数的内部方法呢?

Testing internal methods is not a problem as long as they don't refer to the servlet context, but what about testing the doGet/doPost methods as well as the internal method that refer to the context or make use of session parameters?

有没有办法简单地使用经典工具(例如 JUnit 或最好是 TestNG)来做到这一点?我需要嵌入一个 tomcat 服务器或类似的东西吗?

Is there a way to do this simply using classical tools such as JUnit, or preferrably TestNG? Did I need to embed a tomcat server or something like that?

推荐答案

尝试 HttpUnit,虽然你很可能最终编写的自动化测试更像是集成测试"(模块的)而不是单元测试"(单个类的).

Try HttpUnit, although you are likely to end up writing automated tests that are more 'integration tests' (of a module) than 'unit tests' (of a single class).

这篇关于对 Java Servlet 进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆