sábado, diciembre 11, 2010

Today, thanks of Neal Ford (I have met him in Devoxx 2010[lovely presentation]), I have been reviewing its presentations and I have found interesting one called "Unit Testing That's Not So Bad: Small Things That Make a Big Difference". In this presentation are showed tools for testing like JUnit, InfiniTest, Jester, ... also shows some strategies for writting your tests, and finally a real revelation, you can also test your PRIVATE methods. More often than not, you create an interface that have a public method, and then a class that implements this interface (functionality), and because of code clean you create some private methods that help public method to acquire its porpoise. And then comes test part, you write testing code for your public method, you run tests, 100% coverage, and that's all, you think you have got a perfect code, but, ... that's not true, you could have a bug to a private method that is not reflected in final result (for example a divide by zero), and because you have not write tests for your private methods, you have not though about possible results and although your code have 100% coverage, it is not perfect.

Furthermore, your public method can be an aggregation of several private methods, and would not be funny and easier to test private methods alone? I am sure you are thinking yourself, "Yes would be perfect for that class I wrote some time ago where my private methods contains 60 lines of code each one".

Imagine next example:

We need to develop a software that calculates how many egg-cups shall be required for placing "n" eggs. We have a method that returns the number of eggs (imagine that this value is returned from a complex method like connecting to webservice, connecting to database, ...), also another complex method returns the number of positions in each egg-cup.

// Complex operation like accessing database for retrieving number of egg cup holes.

return this.numberOfHoles;

}

private int getNumberOfEggs() {

// Complex operation like accessing external resource

return this.numberOfEggs;

}

public void setNumberOfHoles(int numberOfHoles) {

this.numberOfHoles = numberOfHoles;

}

public void setNumberOfEggs(int numberOfEggs) {

this.numberOfEggs = numberOfEggs;

}

}

Where both class attributes has been used for simplyfing the example, and not requiring a mock.

And Test Class:

public class WhenNumberEggCupsIsCalculated {

@Test

public void twenty_Five_Eggs_Should_Require_Three_Egg_Cups(){

//Simple test with simple logic for understanding propose

EggCupCalculation eggCupCalculation = new EggCupCalculation();

//For avoiding a complicated test, this is for teaching propose, a mock will not be used.

eggCupCalculation.setNumberOfEggs(25);

eggCupCalculation.setNumberOfHoles(12);

assertThat(eggCupCalculation.calculateNumberOfEggCups(), is(3));

}

@Test

public void twenty_Four_Eggs_Should_Require_Two_Egg_Cups() {

//Simple test with simple logic for understanding propose

EggCupCalculation eggCupCalculation = new EggCupCalculation();

//For avoiding a complicated test, this is for teaching propose, a mock will not be used.

eggCupCalculation.setNumberOfEggs(24);

eggCupCalculation.setNumberOfHoles(12);

assertThat(eggCupCalculation.calculateNumberOfEggCups(), is(2));

}

}

And as you can see 100% coverage is acquired. And now you can think, well my code is perfect, cause I have thought about two possible cases, when the division have no rest or when have rest and one more egg cup is required.

Yeeeeeees, but what's happen if one day getNumberOfEggCupHoles() returns a 0, meaning that eggs cannot be into an egg cup? Then Booooom.

I am sure that if you had tested private method calculateNumberOfRequiredEggCups() you had seen that a divide by zero could occur.

This is a simple example, imagine with real business case.

So here are three possible solutions for testing private methods, every one is free to choose the best for him.

Solution 1:

Make all methods public (or package) scope, put your tests in same package (but in different source folder) as classes, and write your tests. It is an ugly possibility you modify your development, breaking OOP principles for test.

Solution 2:

Use reflection, that big omitted thing. How about using reflection for accessing to private methods? Good, let's look code:

Well now you can access private method, you still have the problem, because of divided by 0, but your code does not pass all tests. That's good, you can play with parameters of private methods and find possible errors and/or reproduce strange scenarios. Ok, it is a good idea, but maybe too complicated, you must use reflection and also a lot of possible exceptions (test fail), not because your business but because of reflection. So it is not a clean solution or at least simple to read. Of course it is a valid solution.

Solution 3:

Using Groovy. Expand your mind, there is some script languages (supported nativly by JDK), that can make your life easier, and this is one case. How about that groovy file:

Easy to understand, and easy to test, not catch sequence, not casts, not complicated expressions. If you had tested private methods (knowing that a division is performed), then you would see that a division by 0 can occurs and you modified the code for not crashing.

Now you have three valid options for testing private methods, choose the best for you. Of course this a simple example and everybody knows that this particular example is difficult that occurs in real examples, but this is only a pedagogic example.

And now it is time to think about what private methods are candidate to be tested. Not all, but these private methods that contains important/critical business code should be tested.

In future posts, I will show you how to integrate maven into groovy test, so java native junit tests and groovy junt tests are runned.

sábado, noviembre 27, 2010

Have you ever started a war with java.util.Calendar class? I hope no, but unfortunately a lot of developed applications deals with Date and Time. Calendar class is a singleton class for manage time, but one never knows exactly how to work with it. You know you have methods for add and subtract time, but nothing more. Now I ask, How about periods of time, durations, intervals...? you need to develop a class for managing all these requirements. Let's take an example:

Imagine we want to compare if current date is Christmas Date.

Calendar compares all its fields, so year, month, day, minutes, seconds, ... are compared, but wait, Christmas day does not depend on year, every year has its Christmas Day. So a "Util" class is developed for treating with this situation. This class would look something like:

Boulangerie example. Our brother-in-law have a bakery, and wants to automatize oven, so a each bread stays only 15 minutes inside oven (of course not all breads gets into oven at same time). With Calendar/Date class this logic would look something like:

static long FIFTEEN_MINUTES = 15 * 60 * 1000;

long startOvenTimeBread1 = System.currentTimeInMillis();

//Thread Polling

....

long currentTime = System.currentTimeInMillis();

if(currentTime>=startOvenTimeBread1 + FIFTEEN_MINUTES) {

//bread ok

}

As you can see first example it is tedious because you must get all required attributes, and compare them with current time. In second example is not as hard as first but you lost the sensation of working with time, all are longs, and the only way to understand what is each long is with variable name.

So Yes working with Dates/Times in JDK looks like a suicide, but here comes JSR-310 (JDK-8?included).

Let's rewrite the same examples but with JSR-310 API.

Christmas Day example:

MonthDay christmasDay = MonthDay.of(12, 25);

MonthDay now = MonthDay.nowSystemClock();

boolean isChristmas = now.equals(christmasDay);

I think more readable than using Calendars, no problem with hours, minutes, seconds, years, ... MonthDay class manages all. These kind of classes in JSR-310 are known as Partials because they only represents partial time.

Now Boulangerie needs our help. As you have observed, the first problem in that case is that you only manages longs, and as a developer you don't have the feeling of working with time. Also an ugly comparison is required.

I. Using assertEquals (in our case with double) is not readable, what is the meaning of third parameter in previous example ?¿ What's happen if instead of 0 I pass a 1?¿.

II. When you speak yourself repeating a requirement, you say: "calculated value should be 1000", but it is rarely common to think "1000 should be my calculated value". Moreover, for example String assertEquals method contains 2 parameters, which of two are the expected value and current value?

III. Flexible notation in contrast to JUnit notation. For specifying that an element cannot be into a collection, in Junit assertFalse is used, and that's sounds strange using with a contains method of a list you can misunderstood exactly what are being test. Look Hamcrest expression, no possible error, we are asserting that "1+" not in validResults. Hamcrest has a module for treating collections expressions, like anyOf, isOneOf, hasItem, hasItems...

But wait, there is more, you can also build you own Hamcrest Matcher. A matcher is the responsible for validating if something is what expected. Hamcrest comes with a lot of built-in Matchers, but what's happen if we need one new matcher? Let's look an example:

Hamcrest does NOT comes with File matchers. We need to validate if after our business logic, a file is created in a directory.

domingo, noviembre 14, 2010

Usually one tend to think about testing as a residual part of development. Nothing further from reality. Testing, so JUnit classes, needs to be treated as "business code". Must follow naming conventions, code must be sectioned, and of course must be readable. For this reason, John Ferguson Smart has its own rules that I would like to summarize and comment:

As John Ferguson Smart told in his entries, there are 5 rules that all JUnit classes must follows, that rules are:

I. Don't say "test", say "should".

JUnit test should pass, but "should pass", maybe there is something wrong in classes that makes test fail, for this reason we are writing off tests. Also, test cases must be validated with requirements, because a requirement is specified as should/shall not with test, same nomenclature is used. Thanks of this improvement, it is easy validating requirements using tests.

Also as Neal Ford suggests each word of your test method name must be separated with '_' character. I find this suggestion so useful because you don't need to use your mind for parsing camel case methods. Of course this only applies to JUnit methods (Yes the only exception between delivery classes and test classes). With this small change your test department will be thankful how fast can validate requirements.

For example the method:testTransfer must be changed to transferShouldDeductSumFromSourceAccountBalance as John Smart suggests, but another change is better, and is changing to: transfer_Should_Deduct_Sum_From_Source_Account_Balance and that's more readable.

II. Don't test classes, test their behavior.

Of course because test cases are related to use cases, tests must validate behavior. This also affects on avoiding testing your classes for the only porpouse of reaching 100% of coverage. 100% of coverage is important, but guarantees nothing, if you don't keep in mind that you must test behavior.

III. Test class names are important too.

Of course when you want to locate where some behavior is tested, it helps the name of your test. Class names must answer the question: When is this behavior applicable? as method names should answer the question: What behavior are we testing?.

What you think is better test class name: TransactionTest or WhenMoneyTransactionIsCommited. Both test names are valid but second one is descriptive about what behavior are you testing.

IV. Structure your tests well.

Test classes are also business classes and for that reason they also have the right to be refactored, kept clean and readable. For that reason is so important to write tests consistently. A really good suggestion is divide each test in three main sections, Given-When-Then. Only with tabs and CR can work perfectly, but you can also use frameworks like JBehave or Easyb.

V. Test are deliverables too.

Tests must be treated as commercial code, not because your client does not see test classes, you can ignore them from static analysis.

This sequence will create a project for creating a simple addon, with all required classes.

Let's examine what have created and how a modification will be applied for creating our addon.

In directory src/main/resources/META-INF/spring two files are present, log4j.properties and applicationContext.xml. applicationContext file is a Spring configuration file that configure Spring for work with annotations (@Configurable, @Component, ....).First class to take a look:

src/main/java/com/test/roo/addon/PropertyName.java

This class represents attributes of options of our command. For example, logger addon, the command is logging setup and then --level is the option, and that option has many attributes (FATAL, ERROR ...), these attributes are what are defined in this class. Must implements Comparable.

Let's take a look of important points:

private String propertyName; [1]

public static final PropertyName USERNAME = new PropertyName("Username"); [2] public static final PropertyName HOME_DIRECTORY = new PropertyName("Home Directory");

[1]: Property name. This value will be used in our Command class, for now, imagine as a key that is unique.[2]: A list of public static final attributes must be defined. Their type must be the same of class we are implementing, and their name will be used for autocompletation. In case of logger addon (FATAL, ERROR, INFO, ...).[3]: Good practice to generate constructor as private in this kind of classes. [4]: Class must implements compareTo. Because our key (propertyName) is unique, this attribute is used, and as usually equals is also implemented using compareTo.[5]: Method that must returns the key (propertyName). Must be called getKey()

Next important class:

src/main/java/com/test/roo/addon/Commands.java

This class represents the command, and what options accept. This class must implement CommandMarker.

[1]: Annotation for registering Command with automatic classpath scan.[2]: Can be called the Business Object of our Command object. Contains logic of command, options and attributes.[3]: Constructor, with two arguments, first of all StaticFieldConverter, which will be used for registering our previous, attributes class. (PropertyName.class). The second one is the business object class.[4]: Annotates a method that can indicate whether a particular command is presently available or not. The string represents the name of the command or commands that this availability indicator represents.[5]: Annotates a method indicating which method must be called when the command have to execute. value attribute is the name of the command (what user must enter), in case of logging is logging setup, and help attribute that shows the message when help is required.[6]: Each option is annotated with CliOption. This method can contain as many CliOption annotations as required. Of all annotation attributes, one is so important, key. The attribute value must be the same as returned in getKey() function of PropertyName.class. Another important thing, look the METHOD attribute, it is our attribute class defined before. Spring Roo will inject the static final instance chosen by user.[7]: Calling business object.Last important class is: src/main/java/com/test/roo/addon/Operations.java

This plain annotated class, is responsible of implementing command logic. Because of annotation, spring-roo can use constructor inject for providing some specific type instances. Those classes can be FileManager, PathResolver, MetadataService, ProjectOperations, ... Zero or more of these classes can be used in constructor. Let's take generated class as example:

[1]: Annotation used by Spring-Roo.[2]: Constructor only using two spring-roo classes. FileManager and MetadataService, both of them will be injected by spring-roo.[3]: Business Method called by Command class.

Another example could be LoggingOperations. That's the class used by logging addon.

LoggingOperations(FileManager fileManager, PathResolver pathResolver, MetadataService metadataService) where other class rather than used in our exmaple, is passed, PathResolver.

Now I am going to explain what is the purpose of most used injected classes in Operations class.

- FileManager, used for modifying the underlying disk storage. Can be used for scanning directories, create files, directories, delete, ...- PathResolver, is primary mechanism to resolve paths.- MetadataService is used for returnig metadata information. Usually used as ProjectMetadata projectMetadata = (ProjectMetadata) metadataService.get(ProjectMetadata.getProjectIdentifier()); and ProjectMetadata offers convenience methods for acquiring the project name, top level project package name, registered dependencies and path name resolution services.- ProjectOperations provides common project operations, such as adding dependencies, update project type, ...

And as an overview, that's all. Finally perform assembly command is executed and we have our addon ready.

Final step move generated jar to $ROO_HOME/dist and restart roo.

In next entries I will write how to modify applicationContext, generate specific files, ITD, …

jueves, marzo 04, 2010

Drink a Mojito and leave EasyMock. In some conferences I have been recently, I have discovered that most people in unitary tests, use EasyMock for stubbing and mocking. And I am wondering, why? Mockito offers, from my point of view, the same features that EasyMock, but better readability and simplified.

First of all, I use Mockito for stubbing classes (Business Objects). In always use Design By Interfaces approach. This approach in conjunction with Spring Framework (IoC), makes my software so modular and reusable, and as you will see soon, testable too.

Imagine we are creating a piece of software that uploads an image in FTP server. We would have an interface called FTPService with a method called int upload(String serverDirectoryPath, String filenamePath) throws NotEnoughSpaceException.

This method would upload a file to given server directory, returning the number of written bytes, and if server is full, NotEnoughSpaceException is thrown; and its implementation called FTPServiceImpl.

Moreover we would have a business object, which will act as a controller of all procedures related with uploading a file (check if uploading file exists, establishing connection, closing connection ...). This class would have a FTPService setter for injecting the implementation (in our case FTPServiceImpl).

Ok, we have got the scenario, now we start playing with Mockito.

As you can see, first problem for testing our business object is that we require a ftp server and/or network connection, but, wait, we are writing unit tests, not integration tests, so a strategy for testing controller without a server is required. And here comes stubbing and Mockito. A stub may simulate the behavior of existing code (such as a procedure on a remote machine) or be a temporary substitute for yet-to-be-developed code. And creating a stub with mockito is as simple as:

(test class)

import static org.mockito.Mockito.*;

....

ControllerBOImpl controller = new ControllerBOImpl();

//Create Mock
FTPService mockedFtp = mock(FTPService.class);

//stubbing. When upload method with "serverPath" as first argument and "filePath" as
//second argument, 1234 integer will be returned.

Of course more steps would be required in previous example like stubbing more methods of FTPService or writing asserts for ControllerBOImpl. But I think that would be information that distracts reader from what is really important. Stubbing with Mockito is so easy.

As you have noted, when you stub something that has attributes, you must provide hardcoded values, and that values must be used in method calling. It is not the same calls controller.uploadFile("serverPath", "filePath") than controller.uploadFile("directory",”filePath") because attribute values are different.

Stubbing voids requires different approach from when(Object) because the compiler does not like void methods inside brackets.

There are four possible solutions, but I only use two of them. doThrow(Throwable) when I want to test a void method throwing an exception, and doNothing() [default behavior], when no interaction is required.

FTPService would also have a method for connecting to server, maybe called connect()throws IOException() and would throw an exception when could not establish a connection.

Two tests are required for testing that these functionalities:

First Test:

doNothing().when(mockedFtp).connect();

And Second one:

doThrow(new IOException()).when(mockedFtp).connect();

And finally, and this reason applies only people who write tests using BDD approach. In summary, means write test with follow structure, //given //when //then comments as fundamental parts of test methods.

Remember that Mockito uses when method for “record” behavior? Well, in BDD approach, when paragraph is used for calling the business method. Note that when does not fit nicely into BDD, because given paragraph is what is called when in Mockito. For that reason Mockito provides a BDDmockito class. This class changes when method name for given method name. Thanks of that, our test is more readable and follows BDD standard.

jueves, febrero 25, 2010

This library is useful when our application must deal with holidays and what to do when a calculated day falls on a holiday or weekend.

Imagine next use case: - Employees have to say how many days of vacation will enjoy from one date. System must notify when employees are at office.

Assume that employee says that he will start his vacations on 22/02/2010 and will be 6 days out of office. So in theory our employee will be at office on 28th day of same month (22 + 6). But looking at calendar, one will see that he won't be there on 28 because it is Sunday (assuming that weekends are Saturday and Sunday). In fact he will be at office on 1/03/2010 (Monday).

This use case can be focused with two approaches:

1) Using java.util.Calendar class, and then the holidays and weekends problem must be controlled by our program. Calculating “what day our employee returns to office” is not only a matter of calling Calendar.add function.

2) Using DateCalc library which watch out for us.

Not only think about weekends and summer holidays, but national holidays like 4th of July in case of USA.

Take a look to its website, there you can find examples and algorithms for handling holiday problem