Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no injection of a mock with easymock

I want to code a little example using easymock 3.5 and JUnit5, but I get an error (nullPointerException) when trying to inject the mock...

here is the test code:

package model;

import controler.BookEditor;
import org.easymock.EasyMockRule;
import org.easymock.EasyMockSupport;
import org.easymock.Mock;
import org.easymock.TestSubject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import view.BookWindow;

import static org.junit.jupiter.api.Assertions.assertEquals;

//@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class BookTest extends EasyMockSupport {

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


@Mock
public BookWindow bookWindow;

public BookList bookList;

@TestSubject
public BookEditor bookEditor;

@Before
public void setUp() {
  bookList = new BookList();
  bookEditor = new BookEditor(bookList, bookWindow);
}

@Test
public void testBookCreation() {

  Book le_livre_de_la_jungle = new Book("Le livre de la jungle", "Rudyard Kipling",
        "Flammarion",
        "978-2081263246");
  assertEquals(le_livre_de_la_jungle.getTitle(), "Le livre de la jungle");
  assertEquals(le_livre_de_la_jungle.getAuthor(), "Rudyard Kipling");
  assertEquals(le_livre_de_la_jungle.getEditor(), "Flammarion");
  assertEquals(le_livre_de_la_jungle.getISBN(), "978-2081263246");

}


@Test
public void testDisplayBook() {
  bookWindow.setTitle("Le livre de la jungle"); //here is line 53
  bookWindow.setAuthor("Rudyard Kipling");
  bookWindow.setEditor("Flammarion");
  bookWindow.setISBN("978-2081263246");
  replayAll();

  bookEditor.setActiveBook(new Book("Le livre de la jungle",
        "Rudyard Kipling", "Flammarion", "978-2081263246"));
  verifyAll();

}

}

the first test is ok, but testDisplayBook fails because bookWindow is null.

in my POM, I have this:

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>4.12.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>3.5</version>
        <scope>test</scope>
    </dependency>

</dependencies>

here is the exception:

java.lang.NullPointerException at model.BookTest.testDisplayBook(BookTest.java:53) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:389) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:167) at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:163) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:110) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:57) at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:83) at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66) at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77) at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null$2(HierarchicalTestExecutor.java:92)

thank you.

like image 801
lolveley Avatar asked Oct 27 '25 01:10

lolveley


2 Answers

In JUnit 5 Rules can't be used any more. You have to use an Extension and annotate the test class or method with ExtendWith. Furthermore you have to use @BeforeEach instead of @Before (See also the migration section in the user guide).

Update: Since EasyMock 4.1, EasyMock ships with a JUnit 5 extension out of the box.

As far as I know there is no official EasyMock extension yet. Luckily the EasyMockRule can be ported quite easily:

import org.easymock.EasyMockSupport;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestInstancePostProcessor;

public class EasyMockExtension implements TestInstancePostProcessor {

    @Override
    public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {
        EasyMockSupport.injectMocks(testInstance);
    }
}

Now you can annotate your test class:

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;

// ...

@ExtendWith(EasyMockExtension.class)
public class BookTest extends EasyMockSupport {

    @Mock
    public BookWindow bookWindow;

    public BookList bookList;

    @TestSubject
    public BookEditor bookEditor;

    @BeforeEach
    public void setUp() {
        bookList = new BookList();
        bookEditor = new BookEditor(bookList, bookWindow);
    }

    // ...
like image 125
eee Avatar answered Oct 29 '25 19:10

eee


Approach with JUnit Jupiter

I think one of the problem with your code is that it uses a mix of JUnit 4 and JUnit 5 annotations. Based on your stack trace, I believe you use a JUnit 5 test engine, therefore you should replace the annotation @Before with @BeforeEach.

I reproduced your problem with Eclipse Oxygen with JUnit support. I got past the NullPointerException by replacing annotation-based injections with explicit instantiations:

public class BookTest extends EasyMockSupport {
    public BookWindow bookWindow;
    public BookList bookList;
    public BookEditor bookEditor;

    @BeforeEach
    public void setUp() {
        bookWindow = EasyMock.mock(BookWindow.class);
        bookList = new BookList();
        bookEditor = new BookEditor(bookList, bookWindow);
    }
    ...
}

I don't have the implementation of Book, BookList, BookEditor and BookWindow so I could not verify if the test passed, but at least it runs and bookWindow could be defined.

Approach with JUnit 4

This being said, I tried to execute your code with JUnit 4 (replacing JUnit Jupiter @Test annotation with @org.junit.Test. This yielded the following exception:

java.lang.NullPointerException: Have you forgotten to instantiate bookEditor?
at org.easymock.internal.Injector.injectMocks(Injector.java:81)
at org.easymock.EasyMockSupport.injectMocks(EasyMockSupport.java:561)
at org.easymock.internal.EasyMockStatement.evaluate(EasyMockStatement.java:42)
...

EasyMock checks @TestSubject is instantiated before injecting the mocks. I believe you wouldn't want to instantiate the BookEditor before injection of the mock since you would end-up with a null bookWindow anyway:

public class BookTest extends EasyMockSupport {
    ...
    @Mock
    public BookWindow bookWindow;

    @TestSubject // BookWindow would be null below as not yet injected.
    public BookEditor bookEditor = new BookEditor(bookList, bookWindow);

One possibility could be to remove the @TestSubject annotation from the bookEditor instance variable, and instantiate the BookEditor in the @Before method (as you currently do):

public class BookTest extends EasyMockSupport {
    @Rule
    public EasyMockRule rule = new EasyMockRule(this);

    @Mock
    public BookWindow bookWindow;

    public BookList bookList;

    public BookEditor bookEditor;

    @Before
    public void setUp() {
        bookList = new BookList();
        bookEditor = new BookEditor(bookList, bookWindow);
    }
    ...

But you may need the @TestSubject, in which case this solution is not feasible.

Yet another approach is to avoid the @Mock annotation and instantiate the mock explicitly:

public class BookTest extends EasyMockSupport {
    ...
    public BookWindow bookWindow = EasyMock.mock(BookWindow.class);

    @TestSubject
    public BookEditor bookEditor = new BookEditor(bookList, bookWindow); 
    ...
like image 21
Alexandre Dupriez Avatar answered Oct 29 '25 17:10

Alexandre Dupriez