【问题标题】:Unit testing Java class with Mockito mocks使用 Mockito 模拟单元测试 Java 类
【发布时间】:2015-04-08 14:18:42
【问题描述】:

我用 Java 写了 2 个类。它们是DeveloperGroupManager。开发人员有字段private GroupManager supervisor(带有getter 和setter)描述谁雇用了他。更重要的是,GroupManager 有以下方法:

public class GroupManager {

    //...

    private List<Developer> employees;

    public void hire(Developer e) {
        if(canHire()) {
            if(employees.contains(e)) {
                System.out.println(e.getName() + " is already hired");
            }
            else {
                e.setSupervisor(this);
                employees.add(e);
                System.out.println(getName() + " is now hiring " + e.getName());
            }
        }
        else {
            System.out.println(getName() + "cannot hire more employees.");
        }
    }

}

这个类公开了方法hire,它为给定的开发者设置了主管。 我想为Developer 类编写测试。 我是这样做的:

@Test
public void hiringWorksAsItShould() throws Exception {
    GroupManager gm = new GroupManager("John Doe", "manager", 1);
    gm.hire(developer);
    assertThat(developer.getSupervisor()).isInstanceOf(GroupManager.class);
}

我正在使用 FEST 测试框架,但由于它非常直观,我相信每个人都能理解它的含义。这个测试通过了,但是因为我希望它是单元测试,所以决定用= mock(GroupManager.class) 替换= new GroupManager。问题是现在这个测试失败了。我如何更正此 Mockito 功能以检查开发人员的主管是否为 GroupManager 的实例。

编辑 我得到的错误:

java.lang.AssertionError: expecting actual value not to be null
    at org.fest.assertions.Fail.failure(Fail.java:228)
    at org.fest.assertions.Fail.fail(Fail.java:167)
    at org.fest.assertions.Fail.failIfActualIsNull(Fail.java:100)
    at org.fest.assertions.GenericAssert.isNotNull(GenericAssert.java:238)
    at org.fest.assertions.ObjectAssert.isInstanceOf(ObjectAssert.java:52)
    at DeveloperTest.hiringWorksAsItShould(DeveloperTest.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

开发者类:

public class Developer {
    private final String name;
    private String role;
    private Manager supervisor;


    public Manager getSupervisor() {
        return supervisor;
    }

    public void setSupervisor(Manager supervisor) {
        this.supervisor = supervisor;
    }
}

DeveloperTest 类:

import org.junit.Test;

import static org.fest.assertions.Assertions.*;
import static org.mockito.Mockito.mock;

public class DeveloperTest {

    static final Developer developer = new Developer("John Doe", "developer");



    @Test
    public void hiringWorksAsItShould() throws Exception {
        GroupManager gm = mock(GroupManager.class);
        gm.hire(developer);
        assertThat(developer.getSupervisor()).isInstanceOf(GroupManager.class);

    }

提前致谢。

【问题讨论】:

  • 为什么模拟出来的时候测试会失败?
  • 我刚刚用它编辑了我的问题。
  • 你为什么要使用“isInstanceOf”?我希望在雇用后由 getSuperviser() 返回的 GroupManager 对象...应该等于您在其上调用的那个“hire()”。只是检查对象是否属于某个类;并没有真正给你买多少。
  • 是的,这正是我正在做的。但这是出于教育目的,如果我知道它失败的原因,我将非常感激。

标签: java unit-testing testing mocking mockito


【解决方案1】:

您确定要将 GroupManager 替换为 mock 而不是员工吗?

如果你有...

@Test
public void hiringWorksAsItShould() throws Exception {
    GroupManager gm = mock(GroupManager.class);

    gm.hire(developer); // YOU ARE CALLING THE MOCK HERE THEN!!
    assertThat(developer.getSupervisor()).isInstanceOf(GroupManager.class);
}

你不想要这样的东西吗?

@Test
public void hiringWorksAsItShould() throws Exception {
    // CLASS UNDER TEST   \/
    GroupManager gm = new GroupManager("John Doe", "manager", 1);

    // Mock "developer"...
    Developer developer = mock(Developer.class);

    gm.hire(developer); 

    verify(developer).setSupervisor(gm);        
}

因此,当您测试 GroupManager 时,这应该是一个可靠的实现,并且应该模拟与之反应的事物(即开发人员)。

【讨论】:

  • 但是我的代码正在测试 Developer 类和你的 GroupManager。
  • @onegrx - 你能发布你实际尝试测试的类/方法的源代码吗? (如果您按照第一篇文章中的建议用模拟替换 GroupManager,那么您实际上并没有测试任何东西!)
  • @onegrx 如果你在“gm.hire(developer)”方法上设置一个断点并进入它,你会看到你最终在 Mockito 的代码中的某个地方。您进行招聘的代码实际上永远不会被调用,这就是断言中值为“null”的原因。看看你的员工类,除了“员工应该存储它的主管”之外,真的没有什么要测试的。如果这是针对 Employee 的单元测试,那么我不确定“GroupManager”应该与什么有关
  • 所以你说这个功能连接到GroupManagerTest而不是DeveloperTest?
  • @onegrx 是的,这就是逻辑所在
猜你喜欢
  • 1970-01-01
  • 2011-05-13
  • 2018-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多