|
|
||
Tom White's BlogTesting ArchivesjMock 2 and my Java Unit Testing ToolkitPosted by tomwhite on April 11, 2007 at 06:16 AM | Permalink | Comments (2)The long-awaited final version of jMock 2 was released today. There are some big changes since version one. For example, you can now write
and then set expectations on the returned
(This means that the This change means that you can refactor your method names without breaking your tests, unlike jMock 1 where the method names were strings:
You may have noticed that the new syntax is a little weird (check out the double braces, and how e.g. will modifiers need new statements), and it may take a little getting used to, but I think it's worth it for the IDE completion and refactoring support. For example, in test driven mode I can write the name of the method that doesn't yet exist then get my IDE to create the method for me. The other nice thing is the integration with Hamcrest (a library of matchers that I've written about before). My toolkit for writing new unit tests now includes:
Testing for errant network connectionsPosted by tomwhite on February 08, 2007 at 01:37 AM | Permalink | Comments (2)We kept breaking our XML catalog resolution in the course of developing an application. We would refactor the parser code, or we would upgrade a schema and forget to upgrade the catalog. The application wouldn't break, but it took longer to run since resources were being retrieved over the network rather than using the local catalog. Because we didn't time our test runs, and because we had lots of non-network dependent tests in the suite, this regression would go unnoticed for a while. When it was noticed we'd fix the symptom, then move on. Until it happened again... After the sixth or so occurrence in few years, I wrote a class to detect the problem. It's a simple implementation of SecurityManager that throws an exception when an attempt is made to connect to a site that is not on the list of approved hosts. The code appears below.
To use it we set the It's not just for applications that use XML catalogs. It's useful for almost any code, as a way to audit - and regression test - the network resources your application depends on. (Like a database you thought wasn't being used any more.) It's also a simple way to discover when third-party libraries connect to the internet unannounced. The code is rough and ready - it's good enough for testing, but not really suitable for anything else, since it is totally permissive except for the checks on outbound connections. Feel free to use it, and suggest improvements or ways you've tackled the same problem in the comments section.
HamcrestPosted by tomwhite on December 22, 2006 at 12:27 PM | Permalink | Comments (5)In Literate Programming with jMock I enthused about jMock's idea of constraints and flexible assertions. Now the jMock team has released version 1.0 of Hamcrest, the constraints part of jMock. Hamcrest matchers (what were called constraints in jMock) are actually useful for more than just writing unit tests, but it is their application in writing assertions where they really shine and will probably see most use. So now I can write
or even
rather than JUnit's
Apart from being more readable,
And since the matchers (and the So why not read the tutorial and give Hamcrest a go? Lift OffPosted by tomwhite on October 30, 2006 at 03:50 AM | Permalink | Comments (3)In a previous blog entry I mentioned a literate functional testing framework that we had developed at our company, Kizoom. The framework was initially developed by my colleague Robert Chatley for testing a rather obscure digital TV XML application. After that project we produced a new version of the framework for testing HTML applications, which we used internally for a number of projects with great success. Last month we spend a bit of time cleaning up the API and released LiFT, short for Literate Functional Testing, as an open source project on java.net. Here I'd like to give a flavour of LiFT and why you might want to use it.
Hmm, not too clear what it's testing. I'm not trying to single out HttpUnit here - it's a great tool for interacting with web applications, but it's pretty low-level. Of course, I could abstract some of the operations into helper methods to make the test clearer. For instance, the bit at the end where I'm looking for an image is ripe for extracting as a method. In fact, this is the approach we took for years - building libraries of helper functions that made using HttpUnit a bit less of an effort. However, it's not a structured approach. We can do better.
This is much more concise. But more importantly it is much more readable. Each line can be read as a sentence, either as a jMock-style flexible assertion starting with "assert that", or as a command, such as "click on".
Page navigation is handled for us - we are provided with a
Perhaps the most powerful idea is in the use of matchers. Matchers allow us to refer to bits of the page - so we can assert something about them or do something to them. For example,
Matchers allow for extensibility - it is easy to write your own if you need to - and help avoid the creation of ad hoc helper method libraries. Often you don't even need to go this far, you can refine an existing matcher using any jMock constraint. This is what is done in the last line. The LiFT still has some rough edges - for example, its error messages are not always as clear as they could be - but it is definitely ready for testing real web applications. There's much more I could say, but I won't here. If you want to learn more try LiFT out by following the introduction, or read the slides from the talk Robert and I gave at the Google London Test Automation Conference in September, or even watch the video. More Literate Programming with jMock: AnaphoraPosted by tomwhite on May 14, 2006 at 02:30 AM | Permalink | Comments (5)According to the dictionary, an anaphor is a word used to avoid repetition. It refers back to something in the conversation. The word "it" in the previous sentence refers back to the word "anaphor" in the first sentence, so "it" is an anaphor for "anaphor". Natural language is often ambiguous, and one reason for this is that it may not be clear which word an anaphor such as "it" is referring to. But ambiguity and programming languages don't go very well together - so why would anyone want to mix the two? Actually, there are circumstances in programming languages where there is no real ambiguity, and an anaphor can have a use in eliminating repetition. Think of it as applying the DRY (Don't Repeat Yourself) principle at the syntax level. Literate Programming with jMockPosted by tomwhite on May 11, 2006 at 11:10 AM | Permalink | Comments (7)
We've been using jMock at our company for some time now. We've found it great for test driven development
and isolating our unit tests from the rest of the system more effectively. One aspect of jMock that stands out for me
is its idea of constraints. In fact, we've found this idea so useful that we always use the | ||
|
|