Chapter 9. Unit testing

 

This chapter covers

  • Unit testing
  • Overview of EmbeddedChannel
  • Testing ChannelHandlers with EmbeddedChannel

ChannelHandlers are the critical elements of a Netty application, so testing them thoroughly should be a standard part of your development process. Best practices dictate that you test not only to prove that your implementation is correct, but also to make it easy to isolate problems that crop up as code is modified. This type of testing is called unit testing.

Although there’s no universal definition of unit testing, most practitioners agree on the fundamentals. The basic idea is to test your code in the smallest possible chunks, isolated as much as possible from other code modules and from runtime dependencies such as databases and networks. If you can verify through testing that each unit works correctly by itself, it will be much easier to find the culprit when something goes awry.

In this chapter we’ll study a special Channel implementation, EmbeddedChannel, that Netty provides specifically to facilitate unit testing of ChannelHandlers.

Because the code module or unit being tested is going to be executed outside its normal runtime environment, you need a framework or harness within which to run it. In our examples we’ll use JUnit 4 as our testing framework, so you’ll need a basic understanding of its use. If it’s new to you, have no fear; though powerful it’s simple, and you’ll find all the information you need on the JUnit website (www.junit.org).

9.1. Overview of EmbeddedChannel

9.2. Testing ChannelHandlers with EmbeddedChannel

9.3. Testing exception handling

9.4. Summary

sitemap