简体   繁体   English

Mockito无法注入模拟

[英]Mockito can't inject mocks

I'm using Mockito and I'm trying to inject a Mock CustomFileHandler into my REjercicioDAO class for testing purposes. 我正在使用Mockito,并且试图将Mock CustomFileHandler注入我的REjercicioDAO类中以进行测试。 The thing is, my test throws no exceptions, but it doesn't inject my mock object, the original @Autowired CustomFileHandler is not being substituted. 事实是,我的测试没有引发异常,但是它没有注入我的模拟对象,原始的@Autowired CustomFileHandler没有被替换。 Here's my code: 这是我的代码:

@Repository
public class REjercicioDAO extends ARHibernateDAO < REjercicio > implements IREjercicioDAO {

    @Autowired
    public ICustomFileHandler customFileHandler;

    ...

}

And here's my test: 这是我的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = ATest.CONTEXT_CONFIGURATION)
public class REjercicioDAOTest extends ATest {

    @Mock private ICustomFileHandler customFileHandler;

    @Autowired
    @InjectMocks
    private IREjercicioDAO rEjercicioDAO;

    @Before
    public void before () {

        MockitoAnnotations.initMocks(this);

        ...

    }

Btw, entities work as expected and interfaces are correctly linked to the actual entities, I have tested that. 顺便说一句,实体已按预期工作,并且接口已正确链接到实际实体,我已经对此进行了测试。 How can I fix this? 我怎样才能解决这个问题?

Here is a no answer. 这是没有答案。 I could not give more because I am really sorry to see so many people use this awkward API relying on reflection while you could do things really clear for the readers of the class by explicitly setting the dependency. 我无法提供更多信息,因为我很抱歉看到如此多的人依赖于反射来使用这个笨拙的API,而您却可以通过显式设置依赖项来为类的读者做得很清楚。

The thing is, my test throws no exceptions, but it doesn't inject my mock object 事实是,我的测试没有引发异常,但是它没有注入我的模拟对象

Not surprising. 不奇怪。 This way of injecting the mock stays quiet even if no injection succeeds. 即使没有成功注入,这种注入模拟的方式也保持安静。 From the InjectMocks javadoc (emphasis is not mine!) : InjectMocks javadoc (强调不是我的!):

Mockito will try to inject mocks only either by constructor injection, setter injection, or property injection in order and as described below. Mockito将尝试仅通过构造函数注入,setter注入或属性注入按顺序注入模拟,如下所述。 If any of the following strategy fail, then Mockito won't report failure; 如果以下任何策略失败,则Mockito 将不会报告失败; ie you will have to provide dependencies yourself. 即,您将必须自己提供依赖项。

While Mockito does not report failure, I really discourage to use this API. 虽然Mockito不会报告失败,但我确实不鼓励使用此API。

About your actual issue, look at that : 关于您的实际问题,请看:

@Autowired
@InjectMocks
private IREjercicioDAO rEjercicioDAO;

You annotate the field with both Spring and Mockito annotation. 您可以使用Spring和Mockito注释对字段进行注释。 Do you feel confortable with the order of their processing ? 您对它们的处理顺序感到满意吗? These come from two distinct libraries. 这些来自两个不同的库。 I don't tell that it will never work (luck and random exists) but do you really think that it is robust ? 我没有说它永远不会工作(运气和随机性都存在),但是您真的认为它很健壮吗?

To achieve your requirement you could write something like that that does things in two explicit steps : 为了满足您的要求,您可以编写类似以下内容的代码,分两个明确的步骤:
- objects instantiation : mocking the dependency and inject the spring dependency -对象实例化:模拟依赖关系并注入spring依赖关系
- relationship set : between the mock dependency and the spring dependency -关系集:模拟依赖项和spring依赖项之间

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = ATest.CONTEXT_CONFIGURATION)
public class REjercicioDAOTest extends ATest {

    @Mock 
    private ICustomFileHandler customFileHandler;

    @Autowired        
    private IREjercicioDAO rEjercicioDAO;

    @Before
    public void before () {    
        MockitoAnnotations.initMocks(this);
        // Set explicitly the fileHandler dependency
        rEjercicioDAO.setFileHandler(customFileHandler);         
    }
 }

You are trying to mix IT with a unit test. 您正在尝试将IT与单元测试混为一谈。

1) If you are using Spring Boot: 1)如果您使用的是Spring Boot:

@MockBean
private ICustomFileHandler customFileHandler;

@Autowired
private IREjercicioDAO rEjercicioDAO;

Thats it.. 而已..

2) Not using Spring Boot: 2)不使用Spring Boot:

public class TestConfig{

    @Bean
    @Primary
    public ICustomFileHandler customFileHandler(){
       return Mockito.mock(ICustomFileHandler.class);
    }

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = ATest.CONTEXT_CONFIGURATION, classes=TestConfig.class)
public class REjercicioDAOTest extends ATest {

   @Autowired
   private ICustomFileHandler customFileHandlerMock; 

    @Autowired
    private IREjercicioDAO rEjercicioDAO;

The mock gets injected and you can set it up in your test as you like 模拟被注入,您可以根据需要在测试中进行设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM