ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 没有使用easymock注入模拟

2019-08-28 08:02:40  阅读:324  来源: 互联网

标签:easymock java 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();

}

}

第一个测试没问题,但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>

这是例外:

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)

谢谢.

解决方法:

在JUnit 5中,规则不能再使用了.您需要use an Extension并使用ExtendWith注释测试类或方法.此外,您必须使用@BeforeEach而不是@Before(See also the migration section in the user guide).

据我所知,还没有正式的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);
    }
}

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

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

    // ...

标签:easymock,java,maven,junit5
来源: https://codeday.me/bug/20190828/1749683.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有