Eclipse Yoxos Services Downloads Blogs About
Home > Blogs >

Archive for September, 2011

on Sep 29th, 2011Effective Mockito Part 2

As promised in the first part of the “Effective Mockito” blog series, I will concentrate on Mockito specifics in the followup posts. So, the main topic for Part 2 is Mockito’s @Mock Annotation.

When I write tests I try to follow an explicit pattern, called the build-operate-check pattern. This was described by Uncle Bob in his book “Clean Code” (Page 127, Chapter 9). The main idea behind this pattern is that you try to split your test method into three parts. The first part sets up the environment, the second executes the method you want to test and the third part verifies the expected. You can also apply this pattern with Mockito using the @Mock Annotation.

When writing tests we often reach a point where we need a second object to get an object under test to work. That’s where we can use mocks icon smile Effective Mockito Part 2 . The test code looks roughly like this:

public class SecondPartTest {
 
  @Test
  public void testSomething() {
    Strategy strategy = mock( Strategy.class );
    Something objectUnderTest = new Something( strategy );
 
    objectUnderTest.doSomething();
 
    verify( strategy ).doSomethingConcrete();
  }
 
}

As you can see we created an object of the type Something. This object needs another object of the type Strategy in its constructor and I decided to use a mock for this. After this we execute the method we want to test which is called doSomething. The doSomething method should invoke the method doSomethingConcrete on the object we passed in the constructor. As you’ve probably noticed, it’s the strategy pattern. I chose this one because it’s a perfect fit for the use of mocks. In the last step of the test we verify that the method doSomethingConcrete was invoked. Nothing special here.

It looks like a nice and clean little test to me. But, in most cases we have more than one test method in a test class. When a new test method is added, it could look like this:

public class SecondPartTest {
 
  @Test
  public void testSomething() {
    Strategy strategy = mock( Strategy.class );
    Something objectUnderTest = new Something( strategy );
 
    objectUnderTest.doSomething();
 
    verify( strategy ).doSomethingConcrete();
  }
 
  @Test
  public void testDelegateSomething() {
    Strategy strategy = mock( Strategy.class );
    Something objectUnderTest = new Something( strategy );
    when( strategy.doValidate() ).thenReturn( true );
 
    boolean isValid = objectUnderTest.validate();
 
    assertTrue( isValid );
  }
 
}

At this point our nice and clean little test is no longer as nice or clean. By adding one test method we introduced two redundant lines of code, the object instantiation and the mocking. This is a sign that we need to use fields for both the objectUnderTest and the mock, and create them in the @Before method as in this snippet:

public class SecondPartTest {
 
  Strategy strategy;
 
  Something objectUnderTest;
 
  @Before
  public void setUp() {
    strategy = mock( Strategy.class );
    objectUnderTest = new Something( strategy );
  }
 
  @Test
  public void testSomething() {
    objectUnderTest.doSomething();
 
    verify( strategy ).doSomethingConcrete();
  }
 
  @Test
  public void testDelegateSomething() {
    when( strategy.doValidate() ).thenReturn( true );
 
    boolean isValid = objectUnderTest.validate();
 
    assertTrue( isValid );
  }
 
}

This makes our test methods clean again, yippee icon smile Effective Mockito Part 2 . But I think we can do better. Mockito provides MockitoAnnotations and I’d like to use one of them in our test. It’s called @Mock (surprise).  Using this annotation we can tell a field that it is a mock.  It can look like this:

public class SecondPartTest {
 
  @Mock
  Strategy strategy;
 
  Something objectUnderTest;
 
  @Before
  public void setUp() {
    MockitoAnnotations.initMocks( this );
    objectUnderTest = new Something( strategy );
  }
 
  @Test
  public void testSomething() {
    objectUnderTest.doSomething();
 
    verify( strategy ).doSomethingConcrete();
  }
 
  @Test
  public void testDelegateSomething() {
    when( strategy.doValidate() ).thenReturn( true );
 
    boolean isValid = objectUnderTest.validate();
 
    assertTrue( isValid );
  }
 
}

This has at least one major benefit. You can see that a field is a mock at first glance without reading the setUp method. However, we didn’t save any lines in the setUp method because we need to tell Mockito to instantiate the @Mock annotated fields. This looks really ugly because we are mixing Mockito framework code with our test code. We need to find another way to instantiate the mocks. Luckily, Mockito helps out once again.

Since JUnit4 you can choose specific test runners for test classes. You probably know this because you use it in your TestSuite classes all the time. Anyway, Mockito provides a runner called MockitoJUnitRunner. Using the runner looks like this:

@RunWith( MockitoJUnitRunner.class )
public class SecondPartTest {
 
  @Mock
  Strategy strategy;
 
  Something objectUnderTest;
 
  @Before
  public void setUp() {
    objectUnderTest = new Something( strategy );
  }
 
  @Test
  public void testSomething() {
    objectUnderTest.doSomething();
 
    verify( strategy ).doSomethingConcrete();
  }
 
  @Test
  public void testDelegateSomething() {
    when( strategy.doValidate() ).thenReturn( true );
 
    boolean isValid = objectUnderTest.validate();
 
    assertTrue( isValid );
  }
 
}

As you can see we could remove the initMocks call in the setUp method and we are nice and clean again. And, that’s how you can get to a clean test using the @Mock annotation. In the next Effective Mockito installment I’ll show you how spies can be used to get to a clean test when you depend on third party libraries icon wink Effective Mockito Part 2 .

followme Effective Mockito Part 2

Read the other Effective Mockito posts:

on Sep 29th, 20115 days until the Eclipse Stammtisch Munich

In Munich, we are currently looking forward to two events: The last week-end at the Oktober Fest and even more important the Eclipse Stammtisch on upcoming Tuesday 6pm. We have already over 20 registrations including the legendary Mr. Eclipse Foundation Europe, Ralph Müller.
Furthermore, we will have interesting demonstrations. Arno will present the Loggifier, a tool that inserts code into Java class files for logging, and the CVSTools. Maximilian and Edgar will present new features of the EMFStore including GIT Integration. Ekke will demonstrate the development of LocationBased Services with the BlackBerry Eclipse PlugIn. And finally I will try to get the KINECT running at Sappralot icon smile 5 days until the Eclipse Stammtisch Munich
However, if you are not in the mood to look at any demonstrations, just drop by for a chat and a drink.
If you have not registered for the Stammtisch yet, please do so: http://eclipsestammtisch.eventbrite.com/
I will update our reservation on Friday.
Looking forward to meet you there!

on Sep 20th, 2011Eclipse Juno Milestone 2, available for download

With summer behind us and autumn almost here (in the northern hemisphere), you can feel the change in the air.

fall Eclipse Juno Milestone 2, available for download

The Eclipse and Equinox teams have made Juno Milestone 2 available, however, these milestones are no longer based on the Eclipse 3.x stream. Starting with this milestone, we will be encouraging users to actively test the 4.2 stream. The Eclipse Juno release (coming June 2012) will be based on the 4.x stream.

There are a few goodies in this release including Quick-Fixes for loop refactoring:
convert to for loop Eclipse Juno Milestone 2, available for download

An enhanced Ant editor:

ant extension assist Eclipse Juno Milestone 2, available for download

And some fancy new transitions:

Checkout the entire New and Noteworthy. Or better yet, download the milestone and take it for a spin.

on Sep 19th, 2011Effective Mockito Part 1

Last week I talked to a fellow developer, Frank Appel, about Mockito. We’ve been using this mocking library for over a year. We both agreed that of all the innovations we’ve tried in the last year or so, Mockito has boosted our coding productivity the most. With this blog series we want to share our experiences with Mockito. You see that I used the word “effective” in the title, and, in this context I want to define “effective” as arriving at clean test and production code as fast as possible.

To understand these posts you need to have a basic understanding of what mocking is. I recommend reading Martin Fowlers article,”Mocks aren’t Stubs“. You’ll also need some practice with Mockito. But, since you found this blog, you probably searched for the term Mockito icon wink Effective Mockito Part 1 , so this should be a given. Anyway, there is a third thing, that only affects this first post. You need to use Eclipse as your IDE. This post shows you how to setup Mockito in your IDE for the daily work. If you are using another IDE and you know how to achieve what I have described here, please share your tips in a comment. The follow up posts will not require Eclipse because they will be more Mockito specific. Enough prerequisites, let’s start with with the first thing that will boost your Mockito experience.

I always write tests first. My goal is to come up with a first test method really quickly to be able to create all the classes that I need by resolving the compiler errors using Eclipse’s quick fix functionality (CTRL/CMD+1 is your friend). What would be ideal, is to create a test class and just write the test without importing all the other stuff.  The Mockito and Junit stuff should automatically appear in my content assist. When using Mockito, I want to create a test method, write “mo”, open the content assist and it should show me all the “mock” methods.

I’ll give you an example to make this clearer and to show how to achieve this. If I write a test method and open the content assist (which I use all the time), the standard selection options are very basic.

Screen Shot 2011 09 12 at 8.37.40 AM Effective Mockito Part 1As you can see there are no “mock” methods but at least the Mockito classes. The reason is that the content assist only shows methods that are imported in the current class. So, by static importing Mockito this can be fixed.

Screen Shot 2011 09 12 at 8.38.31 AM Effective Mockito Part 1But this is not what I want. This assumes that I have to import all Mockito stuff manually, or that I need to write “Mockito” and organize my imports automatically. I really want to be able to just write “mo” and get a pre-filled content assist. Luckily there is an option that lets us achieve this. In Eclpse it’s called “content assist favorites”. It can be configured within your preferences. Let’s take a look at our favorites.

Screen Shot 2011 09 12 at 8.41.16 AM Effective Mockito Part 1As you can see, I added Mockito.*, Matchers.* and Assert.* to my favorites. This enables my content assist to show the available methods and import them when I’m using one. You may think, “hey, this will fill my content assist with unnecessary stuff when I’m writing production code.” But this is not the case. These imports are only available when Mockito and JUnit is on your build class path. So, by separating test and production code you only have these imports available in your test projects. Let’s take a look at the result.

Screen Shot 2011 09 12 at 8.41.29 AM Effective Mockito Part 1As you can see I haven’t imported any Mockito stuff yet but I can select all the mock methods from my content assist. This enables me to write tests really quickly because I don’t have to think about setting up a test, I can just write it.

This is just a simple trick to come up to speed as quickly as possible when writing tests using Mockito. In the next posts I will focus more on the Mockito specifics. I hope you liked it and will stay tuned icon wink Effective Mockito Part 1

followme Effective Mockito Part 1

Read the other Effective Mockito posts:

on Sep 9th, 2011My Eclipse Testing Day

At September 7th I attended the Eclipse Testing Day 2011.

In the morning we heard several talks about various testing strategies in different commercial products. Alexander Klein from BeOne held an inspiring talk about testing the users experience for a product. Among other ideas he recommended to watch users while they are confronted with the product to get ideas for the next iteration.

In the afternoon things became more hands-on. Michaela Greiler presented her Eclipse Plug-in Test Suite. This is a test runner that allows static and dynamic analysis to report on executed extension points and OSGi services. By itself it is merely interesting, but imho not interesting enough to be a standalone project. I think this technology should go as additional metrics in something like EclEmma. Maybe JaCoCo will provide a home? Hear me, Marc?

Jubula was a major point of talk. Felix Ziesel from BREDEX showed how he uses the Eclipse for Testers download to define an automated smoke test suite to test the completeness of the Eclipse for RCP and RAP developers. I hope he will repeat this talk at an EclipseCon where other packagers are present. This just looked as it ought to be, but obviously it wasn’t feasible before Jubula.

The last talk was mine. I presented an example of a full-featured PDE build from a git repository with test automation. Even though it was the last one of a full day of talks the discussion afterwards was interesting and showed I had hit a sore spot in many projects.

At the side where a few booths from various companies. I was excited to see Xored advertising Q7 there. This is a UI testing tool that only supports Eclipse. Because of this exclusiveness the scripting language gets right to the point. Capture and Replay functionality is also available. Why was I excited about that? – Coming as complete outsiders, they did a talk at last years Eclipse Testing Day about this side product they developed to make testing their application easier. It was extremely well perceived and for a while they where the focus of attention with this thing that’s now Q7. I’d really like to see them more involved with the Eclipse community. Specifically I think their tool might be in a good position to allow UI testing for a RAP workbench.

As a conclusion I have mixed feelings about the Eclipse Testing Day. It shows that things are moving and keeps a handful of people talking. I think it could be a real benefit for the community, if it would show up. But while last year all talks where held in english, now only 3 out of 10 did so, making the Testing Day too much of a german and too less an international event. Many key players where missing. Nobody mentioned SWTBot or JaCoCo. WindowTester didn’t even seem to exist. Is FrogLogic still in business (Their website says, yes)?
It seemed to me that compared to last year there was almost an entirely new audience. I recognized only a handful people and 2 of them where speakers.
It think that the talks and the audience are a bit too diverse. The name “Testing Day” brings together developers people who drive test processes in larger companies, but did’t lead them to talk to one another. Instead of accepting just mixing them in a single branch of talks, the Testing Day could improve by cutting down the program for the whole audience to maybe half a day. Fill the other half day with special interest topics, i.e. bring together the developers to talk about TDD, JUnit, issues about test automation with their test runners and hard times with AspectJ. Bring the people together who want to discuss test processes in big projects, but separate these two groups for a while at least.

The organizers are aware of this, too, so I’m looking forward for a strong and interesting Eclipse Testing Day 2012.

on Sep 8th, 2011Announcing a full featured PDE Build example from a Git repository

I set up a githup repository that gives a working example for a PDE product build from a git repository. It is meant to ease the pain of setting up new builds by having a working template that just needs to be adjusted to the new project.

I’m planning a series of blog entries with and around that example. Some features are:

  • Works system independent
  • Executes tests
  • Builds from map files
  • Separates compile step and assemble step (Improves build time)
  • Mostly self-contained (Required: An Eclipse SDK and Git)

How to get it running in short:

  • Fetch from git://github.com/mkempka/hyperbola-pde-build-demo.git
  • Adjust values in org.eclipsercp.hyperbola.releng/custom.properties
  • Execute org.eclipsercp.hyperbola.releng/build.xml with ant

Here are my slides from my talk at the Eclipse Testing Day 2011 that include some documentation. I’ll do more documentation in separate blog entries the next weeks.

on Sep 7th, 2011Eclipse Stammtisch Europe

Sorry for the eye catcher, I meant “München” icon smile Eclipse Stammtisch Europe

To shorten the time until EclipseCon Europe, we cordially invite you to the Eclipse Stammtisch München on October 4th. The Stammtisch is always a good opportunity to meet the community in your region and talk about Eclipse and of course, non-Eclipse topics.

If you would like to attend, please register for this event, as we will make a reservation based on the number of registered people. Currently, we plan to meet at Sappralott. But, as the location might change, please provide a valid mail address so that we can notify you about the details.

Based on your feedback from the last Stammtisch, we would like to give people a forum to present interesting things. However, since this is not a democamp, there will be no stage or agenda. People who want to show something can bring their laptop. We will distribute the presenters around the tables and people can walk around to see what they are showing. That means you can present something, you can watch something, but if you are not in the mood, you can just sit there, have a conversation and drink beer icon smile Eclipse Stammtisch Europe

If you want to present, please register as a presenter and send me a short description of what you are planning: jhelming@eclipsesource.com

stammtisch Eclipse Stammtisch Europe

 

on Sep 1st, 2011Eclipse turning 10, let’s go to Europe!

Eclipse is turning 10 this fall! My first experience with Eclipse came 8 1/2 years ago when I started building visualization tools on top of GEF. I was an IBM CAS Student while doing my PhD at the University of Victoria. Over the years I’ve attended several demo camps, presented Eclipse content at the local Java User Group, delivered presentations and tutorials at numerous EclipseCons and even attended the occasional Eclipse stammtisch. However, I’ve never attended an Eclipse conference in Europe icon sad Eclipse turning 10, lets go to Europe! .

2011 09 01%25252010.22.09 Eclipse turning 10, lets go to Europe!

I’m hoping to change that this year so that I can join in the Eclipse 10th anniversary celebrations.  I’ve submitted 3 talks to the conference:

  • p2, your savior or your achilles heel? Everything an Eclipse team needs to know about p2: This is re-hash of the talk Pascal and I gave an EclipseCon 2011 (the one in North America). We highlight 10 things that all Eclipse developers need to know about p2, or at least the types of things that will make your life a lot easier.
  • It’s Raining Bytes: Scaling p2 Using the Cloud: In this talk I will discuss how we use p2 to build something as complex as the Yoxos distribution. Yoxos contains over 30,000 artifacts, split across dozens of self contained slices and all delivered using the Amazon Cloud services.   Each one of these components has been validated to ensure that it installs and that all dependencies are available. This talk is intended for those looking for practical advice on how to scale p2 (the eclipse provisioning platform).
  • Moving from CVS to Git, 10 things I’ve learned: This talk was inspired by a blog post a wrote a while ago, when I was first learning Git. I often find that once someone starts using Git heavily, they forget about the growing pains they went through.  With these pains still fresh in my mind, I hope to use this opportunity to present 10 things I learned along the way.  This talk is geared towards those just starting off with Git.

I hope to see you in Germany this fall.

© EclipseSource 2008 - 2011