简体   繁体   English

如何在 Mockito 3 中用@InjectMocks 注释的 class 的构造函数中注入最终的 class,例如 String

[英]How to inject final class such as String into constructor of class annotated with @InjectMocks in Mockito 3

I would like to mock the following class:我想模拟以下 class:

public class MyRunner {
    private String name;
    private User user;//not final
    public MyRunner(String name, User user) {
      this.name = name;
      this.user = user
    }
    //...something complicated in the methods
}

Just like with JMockit就像使用JMockit

@ExtendWith(MockitoExtension.class)
public class MyRunnerTest {
    @Inject
    private String name = "sth";
    @Inject
    private User user;
    @Tested
    private MyRunner tested;
}

Similarly with Mockito 3,与 Mockito 3 类似,

@ExtendWith(MockitoExtension.class)
public class MyRunnerTest {
    @Mock
    private String name;
    @Mock
    private User user;
    @InjectMocks
    private MyRunner tested;
    @Test //from JUnit5
    public void testSth() {
      //...
    }
}

Problem : The injection of the name string into the MyRunner during construction would fail due to the fact that String is a final class.问题:由于String是最终的 class,在构造过程中将name字符串注入MyRunner会失败。 Got error message like:收到如下错误消息:

Cannot mock/spy class java.lang.String
Mockito cannot mock/spy because :
 - final class

It is possible to intialize the tested class with new keyword:可以使用新关键字初始化测试的 class:

@ExtendWith(MockitoExtension.class)
public class MyRunnerTest {
    private String name = "sth";
    @Mock
    private User user;
    private MyRunner tested = new MyRunner(name, user);
    @Test //from JUnit5
    public void testSth() {
      //...
    }
}

However, the solution above is not as fluent as merely using the annotation但是,上面的解决方案并不像仅仅使用注解那样流畅

Question : How to inject the final class into the object annotated with @InjectMocks instead of constructing it with new keyword explicitly?问题:如何将最终的 class注入到带有 @InjectMocks 注释的@InjectMocks中,而不是使用new关键字显式构造它?

There are many approaches to this.有很多方法可以解决这个问题。 Here are 3 conceptually different ones:这里有3个概念上不同的:

Option 1选项1

First off, if you're really talking about the String, than probably mocking it doesn't have much sense: Basically your example doesn't require mockito at all.首先,如果您真的在谈论字符串,那么可能 mocking 它没有多大意义:基本上您的示例根本不需要 mockito 。 You can simply:您可以简单地:

public class UserTest {
  private final String NAME = "sampleName;

  private User tested = new User(NAME);

 ...
}

Option 2选项 2

Now, assuming that you're not really talking about String class here, the best option is to program by Interfaces for which creating Mocks is fairly simple:现在,假设您在这里不是真的在谈论String class,最好的选择是通过创建 Mocks 相当简单的接口进行编程:


public class User {
    public User(Name name) {
      ...
    }

}

public interface Name {
   String getValue();
}

@ExtendsWith(MockitoExtension.class)
public class UserTest

   @Mock
   private Name name;

   @InjectMocks
   private User tested;

Option 3选项 3

If you're absolutely required to mock static class, you can do that in mockito but it requires some additional setup: basically you create a file org.mockito.plugins.MockMaker in the directory src/test/resources/mockito-extensions that will contain: If you're absolutely required to mock static class, you can do that in mockito but it requires some additional setup: basically you create a file org.mockito.plugins.MockMaker in the directory src/test/resources/mockito-extensions that will包含:

mock-maker-inline

See this tutorial for more information有关更多信息,请参阅本教程

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

相关问题 Mockito-使用@Spy和@InjectMocks时如何注入构造函数标记为@Mock的最终字段 - Mockito - how to inject final field marked as @Mock by constructor when using @Spy and @InjectMocks 如何存根使用@InjectMocks注释的类的方法? - How to stub a method of an class annotated with @InjectMocks? 由于构造函数中的静态类,Mockito无法使用@InjectMocks创建实例 - Mockito is unable to create instance using @InjectMocks because of Static class in constructor 如何在java中的injectmocks注释类下模拟私有方法 - how to mock a private method under a injectmocks annotated class in java 如果要注入的实例具有最终类,则如何使用InjectMocks - How to use InjectMocks if the instance to be injected has a final class 如何用 mockito 模拟最后一堂课 - How to mock a final class with mockito 具有新的初始化类变量的Mockito InjectMocks - Mockito InjectMocks with new Initialized Class Variables 模拟决赛 class 和 Mockito 2 - Mock final class with Mockito 2 应用程序如何通知Spring应用程序上下文在哪里找到用@Inject注释其构造函数的类? - How can an application inform the Spring application context where to find a class whose constructor is annotated with @Inject? Mockito:模拟一个类,但在构造函数受保护时设置final字段的值 - Mockito: Mock a class but set the value of the final field when the constructor is protected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM