You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中Mockito 2.2如何返回对象?返回对象为NULL问题求助

我懂你现在的困扰——用Mockito mock了notificationService之后,调用它的方法居然返回null,而你明明需要它返回一个带指定属性的SystemAccountResponse对象对吧?别着急,咱们一步步搞定这个问题。

解决Mockito Mock对象返回Null的问题

核心原因是你还没告诉Mockito:当调用mock对象的某个方法时,应该返回什么内容。默认情况下,Mockito的mock对象会返回null(针对引用类型),所以咱们需要手动指定返回值。

步骤1:确保测试类的基础配置正确

首先确认你的测试类已经正确配置了MockitoJUnitRunner,并且用注解创建了mock对象:

@RunWith(MockitoJUnitRunner.class)
public class YourTestClass {
    // 用@Mock创建你的notificationService mock实例
    @Mock
    private NotificationService notificationService; // 替换成你实际的类名
    
    // 用@InjectMocks把mock对象注入到你要测试的业务类中
    @InjectMocks
    private YourBusinessService serviceUnderTest; // 替换成你要测试的类名
}

这里要注意:@Mock负责创建mock对象,@InjectMocks会自动把mock对象注入到测试类的实例里,这样业务类就能使用mock的notificationService了。

步骤2:构建带有指定属性的SystemAccountResponse对象

先按照你的需求,创建一个符合预期的响应实例:

// 构建你需要的响应对象
SystemAccountResponse mockResponse = new SystemAccountResponse();
// 设置你需要的属性,比如假设你要设置solnbr或者其他字段
mockResponse.setSolnbr("AWARD");
// 如果有嵌套属性或者其他字段,继续添加设置逻辑
// mockResponse.setOtherField("your value");

步骤3:指定mock方法的返回值

接下来告诉Mockito:当调用notificationService的某个特定方法时,返回你刚才构建好的mockResponse。比如假设你调用的方法是getSystemAccount(Request request):

// 先构建你给出的Request对象
Request request = new Request();
Element element = new Element("element");
element.addContent(new Element("solnbr").setText("AWARD"));
request.setData(element);

// 核心:指定mock方法的返回值
when(notificationService.getSystemAccount(request)).thenReturn(mockResponse);

// 现在调用业务类的方法,它会使用mock的返回值,而不是null
serviceUnderTest.yourMethodToTest(request);

额外注意事项

  • 参数匹配问题:如果你的Request对象每次创建的实例不一样(引用不相等),Mockito默认的精确匹配会失效,这时候可以用模糊匹配:
    // 匹配任意Request类型的参数
    when(notificationService.getSystemAccount(any(Request.class))).thenReturn(mockResponse);
    
    // 或者用eq()匹配特定属性的对象(需要重写equals方法,或者用ArgumentMatcher自定义匹配)
    when(notificationService.getSystemAccount(eq(request))).thenReturn(mockResponse);
    
  • 方法签名一致:确保你指定的方法名、参数类型和业务类中实际调用的完全一致,否则Mockito还是会返回null。
  • 手动注入替代方案:如果没用@InjectMocks,记得手动把mock的notificationService赋值给业务类的对应字段,不然业务类用的还是原始对象,不是mock实例。

完整示例代码

@RunWith(MockitoJUnitRunner.class)
public class AccountServiceTest {

    @Mock
    private NotificationService notificationService;

    @InjectMocks
    private AccountService accountService; // 假设这是你要测试的业务类

    @Test
    public void testSystemAccountResponse() {
        // 1. 构建请求对象
        Request request = new Request();
        Element element = new Element("element");
        element.addContent(new Element("solnbr").setText("AWARD"));
        request.setData(element);

        // 2. 构建预期的响应对象
        SystemAccountResponse mockResponse = new SystemAccountResponse();
        mockResponse.setSolnbr("AWARD"); // 设置你需要的属性

        // 3. 指定mock方法的返回值
        when(notificationService.getSystemAccount(any(Request.class))).thenReturn(mockResponse);

        // 4. 执行测试方法
        SystemAccountResponse actualResponse = accountService.processRequest(request);

        // 5. 验证结果
        assertNotNull(actualResponse);
        assertEquals("AWARD", actualResponse.getSolnbr());
    }
}

这样设置之后,当你的测试代码调用notificationService的对应方法时,就会返回你预先构建好的带有指定属性的对象,再也不会返回null啦!

内容的提问来源于stack exchange,提问作者lahiruhashan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:39:41