Chapter 3: Testing J2EE Applications
Summary
总结
Important
重要
Testing should occur throughout the software lifecycle. Testing should be a core activity of software development.
测试必须发生在软件开发的整个生命周期。测试必须是软件开发的一个核心任务。
Test cases should usually be written before code. Instead of considering application code as the sole deliverable, test cases,
documentation (Javadoc and other), and application code should be kept in sync, with the test cases being written first. If you can't
write a test for a piece of code, you don't know enough to write the code itself. A test that isn't up-to-date is useless.
Unless tests are easy to run, they won't be run. It's vital that unit tests can be run automatically, and that there is no need to interpret the results - there should be a single indication of success or failure. Ant can be used to automate tests written with JUnit.
It's important to invest a little time early in the project lifecycle in establishing testing conventions and streamlining the testing process so that all developers can efficiently write and execute tests.
测试用例必须在编码之前。不是考虑应用程序代码作为唯一的交付成果,而是测试用例,文档(javadoc 和其他的),和应用代码应该是同步的,并且测试用例是最开始写的。如果你不能为一段代码写测试用例,你不能足够的了解来编写代码本身。测试最新的才有用。除非测试很容易去运行,否者他们将不运行。那是重要的测试必须被自动的运行,并且不需要解释结果--这里必须有一个单一的标志成功或失败。用junit编写的ant通常被用于自动化测试。那是重要的去花费少量的时间去建立测试惯例和简化测试过程以至于所有的开发者能有效的写和执行代码在项目的生命周期中。
Especially in the case of web applications, stress testing is vital. Unsatisfactory performance is a serious risk in enterprise software development, and should be tackled early, through a proof of concept, while it isn't prohibitively expensive to modify the application's design.
特别是在web应用中,。压力测试也是很重要的。令人不满的性能是一个严重的问题在企业软件开发中,它应该在早期解决,通过一个概念验证,虽然修改应用的设计不是一个很昂贵的花费。
Testing J2EE applications involves many different tests. We've considered the following test strategies:
测试j2ee应用涉及许多方面的测试。我们考虑了下面的测试策略:
-
Unit testing of classes (from all architectural tiers) with JUnit. As we've seen, the simple, easy-to-use, JUnit testing framework can be used as the basis of our testing strategy. Unit testing is usually the most important type of testing.
-
junit单元测试类(从所有的架构层)。正如我们看见的,简单的,易用的,junit测试框架被用于我们基本的测试策略。单元测试通常是测试最重要的部分。
-
Testing EJBs with remote interfaces using JUnit test suites running in a client JVM.
-
测试远程接口ejbs用junit测试套件运行在一个客户jvm上。
-
Testing EJBs with local interfaces using Cactus (an open source framework built on JUnit that allows tests to be run within the application server).
-
Unit testing of web-tier classes using JUnit and test implementations of Servlet API interfaces such as HttpServletRequest and HttpServletResponse. This enables web-tier classes to be tested outside the application server, making testing more convenient.
-
web层的单元测试用junit测试实现了servlet api 接口(例如httpservletRequest 和httpservletResponse)的类。这是的web层在应用服务器的外部被测试,是测试更方便。
-
Acceptance testing of the web interface via "screen scraping" web tools such as HttpUnit.
-
web层接受的测试通过“屏幕抓取”web工具如httpunit。
-
Load and stress testing of database operations.
-
对于数据库操作的负载和压力测试。
-
Load and stress testing of EJB component interfaces.
-
对于ejb组件接口的负载和压力测试。
-
Load and stress testing of the web interface.
-
对于web接口的负载和压力测试。
We've also discussed the desirability of designing applications so that central functionality can be tested against ordinary Java classes, rather than harder-to-test J2EE components.
我们也讨论了设计应用程序的合理性,以至于中央功能能被测试以防范普通java类,而不是难以测试j2ee组件。
Testing alone is not enough to ensure that code is bug free. Other useful strategies include using assertions in application code and introducing the practice of regular code reviews. Sound coding and design practices can do much to reduce the likelihood of bugs.
单独测试并不能确保代码bug没有。其他可用的策略包含在代码中用断言和引进常规代码审查。良好的编码和设计实践能更多的减少bug的可能性。
While the concepts discussed in this chapter apply whatever tools we use, I've concentrated on free, simple tools. These are ubiquitous and widely understood, but there are more sophisticated commercial testing tools available, especially for web applications, that are beyond the scope of this chapter.
在本章中讨论的概念适用任何我们用的工具,我只是集中在免费简单的工具上。这些都是非常普遍的,但是有更复杂的商业测试工具可用,特别是为web应用,他们不属于这章的讨论范围。