本文介绍了使用Mockito注入模拟不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mockito来测试我的Spring项目,但是 @InjectMocks 似乎无法将模拟服务注入到另一个Spring服务(bean)中.

I'm using Mockito to test my Spring project, but the @InjectMocks seems not working in injecting a mocked service into another Spring service(bean).

这是我要测试的Spring服务:

Here is my Spring service that I want to test:

@Service
public class CreateMailboxService {
    @Autowired UserInfoService mUserInfoService; // this should be mocked
    @Autowired LogicService mLogicService;  // this should be autowired by Spring

    public void createMailbox() {
        // do mething
        System.out.println("test 2: " + mUserInfoService.getData());
    }

}

下面是我要模拟的服务:

And below is the service that I want to mock:

@Service
public class UserInfoService {
    public String getData() {
        return "original text";
    }
}

我的测试代码在这里:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/root-context.xml" })
public class CreateMailboxServiceMockTest {

    @Mock
    UserInfoService mUserInfoService;

    @InjectMocks
    @Autowired
    CreateMailboxService mCreateMailboxService;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void deleteWithPermission() {
        when(mUserInfoService.getData()).thenReturn("mocked text");

        System.out.println("test 1: " + mUserInfoService.getData());

        mCreateMailboxService.createMailbox();
    }
}

但结果想要

test 1: mocked text
test 2: original text  // I want this be "mocked text", too

似乎 CreateMailboxService 并没有获得模拟的 UserInfoService ,而是使用了Spring的自动装配的bean.为什么我的 @InjectMocks 无法正常工作?

it seems that the CreateMailboxService didn't get the mocked UserInfoService but using Spring's autowired bean.Why is my @InjectMocks not working?

推荐答案

您可以在 CreateMailboxService 类中为 mUserInfoService 创建 package 级别设置程序.

You can create package level setter for mUserInfoService in CreateMailboxService class.

@Service
public class CreateMailboxService {
    @Autowired UserInfoService mUserInfoService; // this should be mocked
    @Autowired LogicService mLogicService;  // this should be autowired by Spring

    public void createMailbox() {
        // do mething
        System.out.println("test 2: " + mUserInfoService.getData());
    }

    void setUserInfoService(UserInfoService mUserInfoService) {
        this.mUserInfoService = mUserInfoService;
    }
}

然后,您可以使用设置器将模拟程序注入测试中.

Then, you can inject that mock in the test using the setter.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/root-context.xml" })
public class CreateMailboxServiceMockTest {

    @Mock
    UserInfoService mUserInfoService;

    CreateMailboxService mCreateMailboxService;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        mCreateMailboxService = new CreateMailboxService();
        mCreateMailboxService.setUserInfoService(mUserInfoService);
    }

    ...
}

这样,您可以避免 @InjectMocks 和Spring注释出现问题.

This way you can avoid problems with @InjectMocks and Spring annotations.

这篇关于使用Mockito注入模拟不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:55
查看更多