Java 使用 mockito 在另一个 spring 服务中模拟服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19003931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mock services inside another spring service with mockito
提问by br4zuca
I'm facing problems mocking services injected inside of other services within the Spring framework. Here is my code:
我在模拟 Spring 框架内其他服务中注入的服务时遇到了问题。这是我的代码:
@Service("productService")
public class ProductServiceImpl implements ProductService {
@Autowired
private ClientService clientService;
public void doSomething(Long clientId) {
Client client = clientService.getById(clientId);
// do something
}
}
I want to mock the ClientService
inside my test, so I tried the following:
我想模拟ClientService
我的测试内部,所以我尝试了以下方法:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring-config.xml" })
public class ProductServiceTest {
@Autowired
private ProductService productService;
@Mock
private ClientService clientService;
@Test
public void testDoSomething() throws Exception {
when(clientService.getById(anyLong()))
.thenReturn(this.generateClient());
/* when I call this method, I want the clientService
* inside productService to be the mock that one I mocked
* in this test, but instead, it is injecting the Spring
* proxy version of clientService, not my mock.. :(
*/
productService.doSomething(new Long(1));
}
@Before
public void beforeTests() throws Exception {
MockitoAnnotations.initMocks(this);
}
private Client generateClient() {
Client client = new Client();
client.setName("Foo");
return client;
}
}
The clientService
inside productService
is the Spring proxy version, not the mock that I want. Is it possible to do what I want with Mockito?
该clientService
内部productService
是Spring代理版本,而不是我想要的模拟。可以用 Mockito 做我想做的事吗?
回答by Jaiwo99
There are more ways to achieve this, the most easy way to do this will be don't use field injection, but setter injection
which means you should have:
有更多方法可以实现这一点,最简单的方法是don't use field injection, but setter injection
这意味着您应该拥有:
@Autowired
public void setClientService(ClientService clientService){...}
in your service class, then you can inject your mock to the service in test class:
在您的服务类中,然后您可以将模拟注入测试类中的服务:
@Before
public void setUp() throws Exception {
productService.setClientService(mock);
}
important:
If this is only a unit test, please consider don't use SpringJUnit4ClassRunner.class
, but MockitoJunitRunner.class
, so that you can also use field inject for your fields.
important:
如果这只是一个单元测试,请考虑不要使用SpringJUnit4ClassRunner.class
, but MockitoJunitRunner.class
,以便您也可以为您的字段使用字段注入。
回答by Debojit Saikia
You need to annotate ProductService
with @InjectMocks
:
您需要标注ProductService
有@InjectMocks
:
@Autowired
@InjectMocks
private ProductService productService;
This will inject the ClientService
mock into your ProductService
.
这会将ClientService
模拟注入您的ProductService
.
回答by Hymany1205
In addition to
此外
@Autowired
@InjectMocks
private ProductService productService;
Add the following method
添加以下方法
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}