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

<div class="container">
  <h1>Getting Started</h1>
  <p>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>Then, you need a class to test and a collaborator used by this class. The collaborator can be a class or and interface. It doesn't matter. Please don't create an interface just for the pleasure of mocking it.</p>

{% highlight java %}
public interface Collaborator {
    void documentAdded(String title);
    void documentChanged(String title);
    void documentRemoved(String title);
    byte voteForRemoval(String title);
    byte[] voteForRemovals(String[] title);
}

public class ClassUnderTest {

    private Collaborator listener;
    // ...
    public void setListener(Collaborator listener) {
        this.listener = listener;
    }
    public void addDocument(String title, byte[] document) { 
        // ... 
    }
    public boolean removeDocument(String title) {
        // ... 
    }
    public boolean removeDocuments(String[] titles) {
        // ... 
    }
}
{% endhighlight %}

    <p>In our case, we are expecting the collaborator to be notified when a document is added to the class under test. So lets do that. First our todo list:</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.Test;
import org.junit.runner.RunWith;

@RunWith(EasyMockRunner.class)
public class ExampleTest extends EasyMockSupport {

    @Mock
    private Collaborator collaborator; // 1

    @TestSubject
    private final 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) `EasyMockSupport` is useful but not mandatory</li>
        <li>Mock injection is done on fields so you won't need a setter only used for testing</li>
        <li>We are testing here that `documentAdded` will be called once and only once using this exact parameter.
        That's what we expect. Any other result means </li>
    </ul>

    <p>From there, I will highly suggest you to have a look at the
        <a href="https://github.com/easymock/easymock/tree/master/easymock/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>
