[英]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.