简体   繁体   English

Mockito存根方法以返回类的所有实例的值

[英]Mockito stubbing a method to return value for all instances of class

Scenario: 场景:

I am testing a Spring MVC controller using a standalone setup. 我正在使用独立设置测试Spring MVC控制器。

The controller takes parameters from the JSON object in the request.It calls a service after converting the JSON object to java object.The service saves it to DB using JPA and it then updates the Id field of entity and returns back to controller.The controller returns the JSON response to the caller after converting the entity to JSON. 控制器从请求中的JSON对象获取参数,将JSON对象转换为java对象后调用服务,该服务使用JPA将其保存到DB,然后更新实体的Id字段并返回给控制器。将实体转换为JSON后,将JSON响应返回给调用方。

I have mocked the service in my test class and injected to the controller using @InjectMocks 我已经在测试类中模拟了服务,并使用@InjectMocks注入了控制器

The mocked service has no access to the private setId method of the JPA entity as it is populated by hibernate in the real scenario. 模拟的服务无法访问JPA实体的私有setId方法,因为它是由真实情况下的休眠填充的。

Now when I mock the service, how can i stub the getId method of the created entity? 现在,当我模拟服务时,如何对创建的实体的getId方法存根?

Code to test mockservice: 测试模拟服务的代码:

@Test
public void thatAccountCreationRendersAsJson() throws Exception {

    doAnswer(new Answer<Boolean>() {
        @Override
    public Boolean answer(InvocationOnMock invocationOnMock) throws Throwable {
    Account account = (Account) invocationOnMock.getArguments()[0];
    account.setEnabled(true);
    account.setFirstName("bca123");
        return Boolean.TRUE;
    }   
    }).when(mockAccountService).registerAccount(any(Account.class),
            anyString(), any(BindingResult.class));

Now how to stub the getId method of Account class so that controller gives the Id in the JSON response. 现在,如何对Account类的getId方法存根,以便控制器在JSON响应中提供Id。

I cannot create a mock account object as it is created by the controller from JSON and sent to the mocked service. 我无法创建模拟帐户对象,因为它是由控制器从JSON创建并发送到模拟服务的。 my mock account object is not used. 我的模拟帐户对象未使用。

I tried to use 我尝试使用

Account spier = spy(account);
doReturn(new Long(22)).when(spier).getId();
spier.setFirstName("cba123");

in the above doAnswer method but it is not effective. 在上面的doAnswer方法中,但这是无效的。

Is there any way we can return a constant value like 22 for all invocations of getId method of Account.class for any instance ? 有什么方法可以为任何实例的Account.class的getId方法的所有调用返回像22这样的常量值?

http://code.google.com/p/mockito-python/wiki/Stubbing gives a section Instance, Class-level and Static Methods http://code.google.com/p/mockito-python/wiki/存根一节提供了实例,类级别和静态方法
which exactly fits my needs where they stub a instance method for all instances but it is on python. 这完全满足了我的需求,他们在所有实例中都为一个实例方法存根,但是它在python上。

Thanks for answering my question. 谢谢回答我的问题。

Make your setId() method public, and add account.setId(whatever) . 将您的setId()方法公开,并添加account.setId(whatever) The setId() method has no real reason to be private. setId()方法没有真正的理由要私有。 Just because Hibernate can set private fields doesn't mean that you shouldn't have a public setter for it, especially if it's needed for your tests. 仅仅因为Hibernate可以设置私有字段并不意味着您不应该为此设置公共设置器,尤其是在测试需要它的情况下。 If you really want to keep it private, then do the same thing as Hibernate, and set it using reflection. 如果您真的想将其设为私有,请执行与Hibernate相同的操作,并使用反射进行设置。 But I wouldn't do that. 但是我不会那样做。

I have moved this test to integration testing as i had the full spring container along with a in memory database to set the id by hibernate. 我已经将此测试移至集成测试,因为我拥有完整的spring容器以及一个内存数据库,用于通过休眠设置id。

Vam VAM

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

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