Wednesday, January 26, 2011

Can You Wax Your Legs After Tanning?

agile defensive, or firing at the API

Following of an entry in @ sergioazo and the corresponding conversation, I remembered a technique I read somewhere to minimize problems in working with third-party libraries in your project.
can be used for a number of issues:
  • Understand how real a bookstore (not expected)
  • Defend against behavioral changes Bookseller future
  • document the use of a library, its API.
can say that we will generate unit tests, in which we can not write the name of the test method to the end. If you do TDD, the name of the test should indicate that we want to ensure performance, and then we schedule the test. In this case, we can put a name to really consistent method to test, when we assert running and checked the green. Maybe match expectations, or maybe not. I suggest you keep the two names, to understand the differences, which probably also disconcert the next programmer who take your code that uses this library.

If the tests we made our application uses a library, changing versions of the same, can be tested. Simply run our suite of integration, for us to corroborate whether the library is functioning as expected.

Here is an example. Suppose we have an API like this:

guardaDocumento public String (String docType, byte [] data);
public byte [] obtenerDocumento (String docCode)

And we have no information, it seems quite obvious what it does, but we want to make, and especially be careful with changes to the library that is being developed. We can do a test as follows:

deberiaDevolverElMismoDocumentoPorCodigo public void () {
 
Repository repository = new Repository ();
docCode = repositorio.guardaDocumento
String ("Type 1", " Hello World. "getBytes ());
assertArrayEquals (" Hello World ". getBytes (),
repositorio.obtenerDocumento (docCode));

} But this test, as simple as saving a document and get it again, it turns red. Why? Because the code is needed then is not the same code that returns
So finally after some tests (or a phone call to someone sick of your questions) we conclude that the code you need the "obtenerDocumento" is the concatenation of the type of document and the code returned to storage.
Therefore, we modify the test, and then modify the name of the test.

/ **
* was: deberiaDevolverElMismoDocumentoPorCodigo
 
* /
@ Test public void
deberiaDevolverElMismoDocumentoPorTipoDocumentalYCodigo () {
Repository repository = new Repository ();
docCode = repositorio.guardaDocumento
String ("Type 1", "Hello World." getBytes ());
assertArrayEquals ("Hello World". getBytes (),
repositorio.obtenerDocumento ("type1" + docCode));}
 
"get with it the initial goals?

(note: based on facts)