我使用Mockito的@Mock和@InjectMocks注释将依赖注入到Spring的@Autowired注释的私有字段中:
@RunWith(MockitoJUnitRunner.class)
public class DemoTest {
@Mock
private SomeService service;
@InjectMocks
private Demo demo;
/* ... */
}
and
public class Demo {
@Autowired
private SomeService service;
/* ... */
}
现在我还想将真实对象注入私有的@Autowired字段(没有setter)。这是可能的还是该机制仅限于注入mock ?
Mockito不是依赖注入框架,即使是依赖注入框架也鼓励构造函数注入而不是字段注入。
所以你只需要声明一个构造函数来设置被测类的依赖关系:
@Mock
private SomeService serviceMock;
private Demo demo;
/* ... */
@BeforeEach
public void beforeEach(){
demo = new Demo(serviceMock);
}
在一般情况下使用Mockito间谍是一个糟糕的建议。它使测试类变得脆弱,而不是直接且容易出错:真正被嘲笑的是什么?真正测试的是什么?
@InjectMocks和@Spy也损害了整体设计,因为它鼓励了臃肿的类和类中的混合责任。
请在盲目使用spy() javadoc之前阅读它(我不是强调):
Creates a spy of the real object. The spy calls real methods unless
they are stubbed. Real spies should be used carefully and
occasionally, for example when dealing with legacy code.
As usual you are going to read the partial mock warning: Object
oriented programming tackles complexity by dividing the complexity
into separate, specific, SRPy objects. How does partial mock fit into
this paradigm? Well, it just doesn't... Partial mock usually means
that the complexity has been moved to a different method on the same
object. In most cases, this is not the way you want to design your
application.
However, there are rare cases when partial mocks come handy: dealing
with code you cannot change easily (3rd party interfaces, interim
refactoring of legacy code etc.) However, I wouldn't use partial mocks
for new, test-driven & well-designed code.