Posts Tagged testing

About using Test Doubles in tests

I have just read an interesting chapter from the Software Engineering book at Google about using Test Doubles in tests.

The chapter discuss the various techniques of using test doubles in tests along with the pros and cons of using real implementations, fakes, stubbing with the support of mocking frameworks and interaction testing. It also refers to the benefits of running Contract Tests against the API’s public interface to catch contract changes prior to the production environment.

In my opinion, it is worth reading it to be aware of good practices when writing unit tests. Drop your comments here about what you think about it.

Tags: , , , , , , , , , ,

35 programming habits that make your code smell

Interesting article about some bad programming practices, including topics like code organization, teamwork, testing and maintenance.

Tags: , , , ,

When to use TestNG or JUnit

There is an interesting discussion between using TestNG or JUnit on Java projects at Javalobby. It’s very worth reading.

Tags: , , , ,

Using Hamcrest and JUnit

Lately I started using the core Hamcrest matchers bundled with the JUnit framework to create more readable unit tests.

Hamcrest matchers were created to improve the readability of unit testing code. It’s a framework which facilitates the creation of matcher objects to match rules specified in unit tests. Some examples will let it to be clearer:

   1 import static org.hamcrest.CoreMatchers.equalTo;
   2 import static org.hamcrest.CoreMatchers.is;
   3 import static org.junit.Assert.assertThat;
   4 
   5 @Test
   6 public void shouldBeTheSamePerson()
   7 {
   8     Person me = new Person( "Rafael" );
   9     Person theOther = new Person( "Rafael" );
  10     assertThat( me, is( theOther ) );
  11 }
  12 
  13 @Test
  14 public void shouldHaveFixedSizeNumbers()
  15 {
  16     List<Integer> numbers = Arrays.asList( 1, 2, 3, 4, 5 );
  17     assertThat( numbers.size(), is( equalTo( 5 ) ) );
  18 }

The first example checks if one Person object is equal to another using the Object equals method, which was overridden in the Person class. The is syntax defines a matcher which is a shorthand to is(equalTo(value)). The second one uses the is(equalTo(value)) matcher to check the size of an integer list of fixed size numbers. The assertThat method is used in conjunction with the is(equalTo(value)) matcher, which makes the test sentence very human readable.

An interesting thing is the possibility to create a custom matcher, like this one which tests if a given list only has even numbers:

   1 public class AreEvenNumbers extends TypeSafeMatcher<Collection<Integer>> {
   2 
   3     @Override
   4     public boolean matchesSafely(Collection<Integer> numbers) {
   5         for (Integer number : numbers) {
   6             if (number % 2 != 0) {
   7                 return false;
   8             }
   9         }
  10         return true;
  11     }
  12 
  13     @Override
  14     public void describeTo(Description description) {
  15         description.appendText("even numbers");
  16     }
  17 
  18     @Factory
  19     public static <T> Matcher<Collection<Integer>> evenNumbers() {
  20         return new AreEvenNumbers();
  21     }
  22 }

And below are two tests which uses the AreEvenNumbers custom matcher:

   1 import static org.hamcrest.CoreMatchers.is;
   2 import static org.junit.Assert.assertThat;
   3 import static br.com.rafael.hamcrest.AreEvenNumbers.evenNumbers;
   4 
   5 @Test
   6 public void shouldHaveOnlyEvenNumbers()
   7 {
   8     List<Integer> numbers = Arrays.asList( 2, 4, 6, 8, 10 );
   9     assertThat( numbers, is( evenNumbers() ) );
  10 }
  11 
  12 @Test
  13 public void shouldNotHaveOddNumbers()
  14 {
  15     List<Integer> numbers = Arrays.asList( 1, 2, 4, 6, 8, 10 );
  16     assertThat( numbers, not( evenNumbers() ) );
  17 }

These two tests use the static factory method evenNumbers to instantiate the matcher on the test code. Not the use of the not matcher on the shouldNotHaveOddNumbers test to assert that no odd numbers are present on the given list. All tests use the static import feature, which turns the test not clean and not cluttered with the class qualification.

I haven’t experienced the other common matchers on unit testing code, like the Beans, Collections and Number ones. I think they turn the tests more readable, clean and easy to change. And you? Have you ever used Hamcrest matcher? If you have other examples of using it, post them here!

Updated: Static imports were added to the testing code.

Tags: , , , , , ,

Effect propagation to code

Reading Michael Feathers’ ‘Working Effectively With LegacyCode’, I found quite interesting his heuristics to trace propagation of effects to code:

  1. Identify a method that will change.
  2. If the method has a return value, look at its callers.
  3. See if the method modifies any values. If it does, look at the method that use those values, and the methods that use those methods.
  4. Make sure you look for superclasses and subclasses that might be users of these instance variables and methods also.
  5. Look at parameters to the methods. See if they or any objects that their methods return are used by the code that you want to change.
  6. Look for global variables and static data that is modified in any of the methods you’ve identified.

Tags: , , , ,

Software testing purposes

James Bach has a interesting opinion about testing software systems. I think it’s a different opinion from any team project manager or developer. He not only emphasizes the importance of testers but says developers are trying to test software themselves, andfix every bug, before testers even got their first look at it. His software test heuristic is if it exists, I want to test it, even if the product is completely inoperable, with a lot of buggy code. A different opinion, even from mine. As a software developer, I would think to test it before delivering to testers 🙂 . And in your company, are there software testers? What do they think about it?

Tags: