使用 easymock 无需注入模拟

lol*_*ley 4 java easymock maven junit5

我想使用 easymock 3.5 和 JUnit5 编写一个小示例,但在尝试注入模拟时出现错误 (nullPointerException)...

这是测试代码:

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();

}
Run Code Online (Sandbox Code Playgroud)

}

第一个测试没问题,但 testDisplayBook 失败,因为 bookWindow 为 null。

在我的 POM 中,我有这个:

<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>
Run Code Online (Sandbox Code Playgroud)

这是一个例外:

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

谢谢。

eee*_*eee 5

在JUnit 5中Rules不能再使用了。您必须使用Extension并用 注释测试类或方法ExtendWith。此外,您必须使用@BeforeEach而不是@Before另请参阅用户指南中的迁移部分)。

更新:从 EasyMock 4.1 开始,EasyMock 附带了开箱即用的JUnit 5 扩展。

据我所知,目前还没有官方的 EasyMock 扩展。幸运的是,EasyMockRule可以很容易地移植:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在您可以注释您的测试类:

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);
    }

    // ...
Run Code Online (Sandbox Code Playgroud)