---
layout: default
title: Getting started
comments: true
locale: en
---

<div class="container">
  <h1>Getting Started</h1>

  <p>You have brand new class. Let's call it <code>ClassTested</code>.</p>

{% highlight java %}
public class ClassTested {

  private Collaborator listener;

  public void setListener(Collaborator listener) {
    this.listener = listener;
  }

  public void addDocument(String title, String document) {
    listener.documentAdded(title);
  }
}
{% endhighlight %}

  <p>Being a nice human being, you want to test your implementation. You might even be a disciple of TDD and haven't
    done your implementation yet. You want your test first! </p>

  <p>Your tested class will depend on others so you figured you need a mocking framework. Good for you. Add
    EasyMock dependency to your POM file.</p>

{% highlight xml %}
<dependency>
  <groupId>org.easymock</groupId>
  <artifactId>easymock</artifactId>
  <version>{{site.latest_version}}</version>
  <scope>test</scope>
</dependency>
{% endhighlight %}

  <p>Ok. Now <code>addDocument</code> should do stuff and then notify a dependency. Let's
    call it <code>Collaborator</code>.</p>

{% highlight java %}
public interface Collaborator {
    void documentAdded(String title);
{% endhighlight %}

  <p class="alert alert-danger">Now, a word of warning. I will mock an interface in this example. That doesn't mean
    you should only mock interfaces. I hate useless interfaces. And you want me to be happy. Please don't create
    an interface just for the pleasure of mocking it. Just mock the concrete class. Thank you.</p>

  <p>So, we want to make sure <code>addDocument</code> is notifying <code>Collaborator</code> by calling
    <code>documentAdded</code> with the right title in argument. Our todo list to do that:</p>

  <ol>
    <li>Create the mock</li>
    <li>Have it set to the tested class</li>
    <li>Record what we expect the mock to do</li>
    <li>Tell all mocks we are now doing the actual testing</li>
    <li>Test</li>
    <li>Make sure everything that was supposed to be called was called</li>
  </ol>

  <p>Then the code fulfilling it:</p>

{% highlight java %}
import static org.easymock.EasyMock.*;
import org.easymock.*;
import org.junit.*;

public class ExampleTest extends EasyMockSupport {

    @Rule
    public EasyMockRule rule = new EasyMockRule(this);

    @Mock
    private Collaborator collaborator; // 1

    @TestSubject
    private ClassTested classUnderTest = new ClassTested(); // 2

    @Test
    public void addDocument() {
        collaborator.documentAdded("New Document"); // 3
        replayAll(); // 4
        classUnderTest.addDocument("New Document", "content"); // 5
        verifyAll(); // 6
    }
}
{% endhighlight %}

  <p>And that's all you need to get you started. Some comments though:</p>
  <ul>
      <li>Extending (or delegating to) <code>EasyMockSupport</code> is useful but not mandatory.
        It allows to call <code>replayAll</code> instead of <code>replay(mock1, mock2, ...)</code> for instance</li>
      <li>Mock injection is done on fields (by <code>EasyMockRule</code>) so you won't need a setter only used for testing</li>
      <li>We are testing that <code>documentAdded</code> is called only once and receiving this exact parameter.
        Any other call to our mock is a test failure</li>
  </ul>

  <p>From there, I will highly suggest you have a look at the
      <a href="https://github.com/easymock/easymock/tree/easymock-{{site.latest_version}}/core/src/samples/java/org/easymock/samples">samples</a>
      and the <a href="user-guide.html">full documentation</a> to get a fair overview of EasyMock.
  </p>

  <div class="alert alert-success">
      <p>Happy mocking!</p>
  </div>

</div>
