简体   繁体   English

如何为手动依赖注入创建mockito测试

[英]How to create mockito test for Manual dependency injection

I am creating manual dependency injection using java. 我正在使用java创建手动依赖注入。 I am trying to create Mockito test for the same. 我正在尝试为此创建Mockito测试。

As I am new to Mockito and I have done only for framework based before. 因为我是Mockito的新手而且我之前只为基于框架做过。 So need your help on the below 所以需要你的帮助

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

So at last I need to achieve two things. 所以最后我需要做两件事。

  1. To Test MyFactory class and MyClass 测试MyFactory类和MyClass
  2. To Test MyProtoImpl class. 测试MyProtoImpl类。 So in this way I can get the entire codecoverage. 所以通过这种方式我可以得到整个代码覆盖。 So not only MyProtoImpl need to be covered with Junit Mockito MyFactory and MyClass also need to be covered 因此,不仅需要使用Junit Mockito MyFactory覆盖MyProtoImpl,还需要覆盖MyClass

Normally you want to create mocks of your dependencies. 通常,您希望创建依赖项的模拟。 From the comments it seems that you want to test the classes in separation, hence I would suggest "unit testing" the modules. 从评论看来,您似乎想要在分离中测试类,因此我建议对模块进行“单元测试”。 I will walk you through the testing of MyClass . 我将引导您完成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());
    }
}

If your classes contain void methods, you can test whether the methods have been called with the correct arguments: 如果您的类包含void方法,则可以测试是否使用正确的参数调用了这些方法:

     verify(a).someMethod(someParameter);

That's pretty much it for Mockito. 对于Mockito来说,这几乎就是这样。 You create mocks, you set the desired behavior, and finally you assert the result or verify that the methods have been called with the correct arguments. 您可以创建模拟,设置所需的行为,最后断言结果或验证是否使用正确的参数调用了方法。

However I don't think it makes that much sense to "Mockito-test" classes that are responsible for database connections and similar configuration. 但是,我认为对负责数据库连接和类似配置的“Mockito-test”类没有多大意义。 Mockito testing is IMHO more suited to test the service/logic layer of the application. Mockito测试是IMHO更适合测试应用程序的服务/逻辑层。 If you had a spring project I would simply test such configuration classes (ie database config etc.) in a scenario, where i establish a real connection to the database. 如果你有一个spring项目,我会在一个场景中测试这样的配置类(即数据库配置等),在那里我建立一个到数据库的真实连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM