Java Mockito 空指针异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24072579/
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-14 09:59:56  来源:igfitidea点击:

Mockito NullPointerException

javaspringjunitmockito

提问by Anuj

I followed what @hoaz suggested. However, I am getting nullpointer exception

我遵循了@hoaz 的建议。但是,我收到空指针异常

@RunWith(MockitoJUnitRunner.class)
public class GeneralConfigServiceImplTest  {

@InjectMocks private GeneralConfigService generalConfigService;
@Mock private SomeDao someDao;
@Mock private ExternalDependencyClass externalDependencyObject 

@Test
public void testAddGeneralConfigCallDAOSuccess() {
    when(someDao.findMe(any(String.Class))).thenReturn(new ArrayList<String>(Arrays.asList("1234")));

    //println works here, I am able to get collection from my mocked DAO

    // Calling the actual service function
    generalConfigService.process(externalDependencyObject)
    }
}

In my Code it's like this:

在我的代码中是这样的:

import com.xyz.ExternalDependencyClass;

public class  GeneralConfigService{

private SomeDao someDao;

public void process(ExternalDependencyClass externalDependencyObject){

// function using Mockito 
Collection<String> result = someDao.findMe(externalDependencyObject.getId.toString())
    }
}

I also notice that DAO was null so I did this(Just to mention, I did the below step to try, I know the difference between springUnit and Mockito or xyz):

我还注意到 DAO 为空,所以我这样做了(只是提一下,我做了以下步骤来尝试,我知道 springUnit 和 Mockito 或 xyz 之间的区别):

@Autowired
private SomeDao someDao;



@John B 解决方案解决了我的问题。但是,我想提一下对我不起作用的内容。这是我更新的单元测试

@Test
public void testAddGeneralConfigCallDAOSuccess() {
    /*
    This does not work
    externalDependencyObject.setId(new ExternalKey("pk_1"));
    // verify statement works and I thought that the class in test when call the getId 
    // it will be able to get the ExternalKey object
    //verify(externalDependencyObject.setId(new ExternalKey("pk_1")));
    */

    // This works
    when(externalDependencyObject.getId()).thenReturn(new ExternalKey("pk_1"));
    when(someDao.findMe(any(String.Class))).thenReturn(new ArrayList<String>(Arrays.asList("1234")));

    ....
    // Calling the actual service function
    generalConfigService.process(externalDependencyObject)
    }



参考了这个问题:

How do I mock external method call with Mockito

如何使用 Mockito 模拟外部方法调用

How do I set a property on a mocked object using Mockito?

如何使用 Mockito 在模拟对象上设置属性?

回答by John B

You haven't mocked the behavior of getIdin externalDependencyObjecttherefore it is returning nulland giving you the NPE when toString()is called on that null.

您还没有嘲笑getIdin的行为,externalDependencyObject因此它正在返回null并在toString()被调用时为您提供 NPE null

You need a when(externalDependencyObject.getId()).then...

你需要一个 when(externalDependencyObject.getId()).then...