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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:17:25  来源:igfitidea点击:

Mock services inside another spring service with mockito

javaspringjunit4mockitospring-test

提问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 ClientServiceinside 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 clientServiceinside productServiceis 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 injectionwhich 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 ProductServicewith @InjectMocks:

您需要标注ProductService@InjectMocks

@Autowired
@InjectMocks
private ProductService productService;

This will inject the ClientServicemock 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);
}