【问题标题】:How to create mockito test for Manual dependency injection如何为手动依赖注入创建模拟测试
【发布时间】:2019-04-23 08:46:13
【问题描述】:

我正在使用 java 创建手动依赖注入。我正在尝试为此创建 Mockito 测试。

因为我是 Mockito 的新手,而且我以前只为基于框架的人做过。所以在下面需要你的帮助

//All Business logic holder class. It does not have any new operator. Even, it does not have any knowledge about the concrete implementations
class MyClass {

private MyProto a;
private B b;
private C c;

MyClass(MyProtoImpl a, B b, C c) {
 //Only Assignment
 this.a = a;
 this.b = b;
 this.c = c;
}

//Application Logic
public void doSomething() {
 a.startClient(c);
 //Do B specific thing
 //Do C specific thing
 }
}

//The Factory. All the new operators should be placed in this factory class and wiring related objects here.
class MyFactory {
 public MyClass createMyClass() {
   return new MyClass(new AImpl(), new BImpl(), new CImpl());
  }
}

class Main {
 public static void main(String args[]) {
  MyClass mc = new MyFactory().createMyClass();
  mc.doSomething();
 }
}

所以最后我需要实现两件事。

  1. 测试 MyFactory 类和 MyClass
  2. 测试 MyProtoImpl 类。所以通过这种方式我可以获得整个代码覆盖率。所以不仅 MyProtoImpl 需要被 Junit Mockito MyFactory 覆盖,MyClass 也需要被覆盖

【问题讨论】:

  • 您到底想测试什么?我只看到一个带有包私有构造函数的普通类和一个用于实例化对象的工厂类。但我不明白你的意思
  • //Application Logic public void doSomething() { //a.Method1ReadFile() //b.printSomething(); //c.myBusinessLogic(); }
  • 你想写一个单元测试吗?在哪里连接依赖项?
  • @Ph03n1x 刚刚编辑了问题。请检查一次
  • 所以最后我需要实现两件事。 1.测试MyFactory类和MyClass。 2. 测试 MyProtoImpl 类。所以通过这种方式我可以获得整个代码覆盖率。所以不仅 MyProtoImpl 需要被 Junit Mockito MyFactory 覆盖,MyClass 也需要被覆盖

标签: java unit-testing junit mockito powermock


【解决方案1】:

通常您希望创建依赖项的模拟。从 cmets 看来,您似乎想单独测试这些类,因此我建议对模块进行“单元测试”。我将带你完成MyClass的测试。

class MyTestClass {

    // First you want to create mocks of your dependencies
    @Mock
    MyProto a;

    @Mock
    B b;

    @Mock
    C c;

    // At this point Mockito creates mocks of your classes.
    // Calling any method on the mocks (c.doSomething()) would return null if the
    // method had a return type.

    @Test
    void myFirstTest(){
     // 1. Configure mocks for the tests
     // here you configure the mock's returned values (if there are any).
     given(a.someMethod()).willReturn(false);

     // 2. Create the SUT (Software Under Test) Here you manually create the class
     // using the mocks.
     MyClass myClass = new MyClass(a, b, c);

     // 3. Do the test on the service
     // I have assumed that myClass's doSomething simply returns a.someMethod()'s returned value
     boolean result = myClass.doSomething();

     // 4. Assert
     assertTrue(result);
     // Alternatively you can simply do:
     assertTrue(myClass.doSomething());
    }
}

如果您的类包含void 方法,您可以测试是否使用正确的参数调用了这些方法:

     verify(a).someMethod(someParameter);

对于 Mockito 来说就差不多了。您创建模拟,设置所需的行为,最后断言结果或验证已使用正确的参数调用方法。

但是,我认为负责数据库连接和类似配置的“Mockito-test”类没有多大意义。恕我直言,Mockito 测试更适合测试应用程序的服务/逻辑层。如果你有一个 spring 项目,我会在一个场景中简单地测试这样的配置类(即数据库配置等),在那里我建立一个与数据库的真实连接。

【讨论】:

    猜你喜欢
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 2019-11-27
    相关资源
    最近更新 更多