Junit 调用另一个方法Mockito的方法中出现空指针异常

Junit 调用另一个方法Mockito的方法中出现空指针异常,junit,nullpointerexception,mockito,powermockito,Junit,Nullpointerexception,Mockito,Powermockito,我对Mockito和Powermockito都是新手。我有一个类要测试,它与数据库交互,以便通过不同的公共方法查找和删除数据库中的数据。该应用程序是典型的JavaEE应用程序,被测试的类属于Businesslogic中的服务包。我要测试的方法如下所示: public List<QuestionDtoWrapper> searchInQuestions(final Integer ID, final Integer catID, final St

我对Mockito和Powermockito都是新手。我有一个类要测试,它与数据库交互,以便通过不同的公共方法查找和删除数据库中的数据。该应用程序是典型的JavaEE应用程序,被测试的类属于Businesslogic中的服务包。我要测试的方法如下所示:

    public List<QuestionDtoWrapper> searchInQuestions(final Integer ID, final Integer catID,
                final String searchString, final String language) {
            final List<QuestionDtoWrapper> result = new ArrayList<>();

//In line below I get null pointer exception although I have stubbed this method            
final List<QuestionDtoInt> questions = facade.findQuestionsByCatTemplate(ID, catID,
                    searchString, language);
            for (final QuestionDtoInt question : questions) {
                result.add(new QuestionDtoWrapper(question));
            }
            Collections.sort(result, new QuestionComparator(new Locale("de")));

            return result;
        }
公共列表搜索问题(最终整数ID、最终整数catID、,
最终字符串搜索字符串,最终字符串语言){
最终列表结果=新建ArrayList();
//在下面的一行中,我得到了空指针异常,尽管我已经将此方法存根
最终列表问题=facade.findQuestionsByCatemplate(ID、catID、,
搜索字符串(语言);
对于(最后一个问题点问题:问题){
结果.添加(新问题Towrapper(问题));
}
排序(结果,新的QuestionComparator(新的区域设置(“de”));
返回结果;
}
以下是我在Junit测试中尝试测试该方法的方式:

@RunWith(MockitoJUnitRunner.class)
@PrepareForTest(QuesService.class)

public class QuesServiceTest {  
        @Mock
        QuesFacade mockFbFacade;

        @Mock
        List<QuesDtoInt> questions;

        @Spy
        QuesService myService = new QuesService();

        @Before
         public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
        }   

        @Test
            public void testSearchInQuestions() throws ParseException {
                PowerMockito.doReturn(questions).when(mockFbFacade).findQuestionsByCatTemplate(anyInt(), anyInt(), anyString(), anyString());
                List<QuestionDtoWrapper> res = null ;
                res = myService.searchInQuestions(anyInt(), anyInt(), anyString(), anyString());
                assertNotNull(res);
            } 
@RunWith(MockitoJUnitRunner.class)
@PrepareForTest(查询服务类)
公共类QuesService测试{
@嘲弄
QuesFacade mockFbFacade;
@嘲弄
列出问题;
@间谍
QuesService myService=新QuesService();
@以前
public void setUp()引发异常{
initMocks(this);
}   
@试验
public void testSearchInQuestions()引发异常{
doReturn(questions).when(mockFbFacade).findQuestionsByCatTemplate(anyInt(),anyInt(),anyString(),anyString());
列表res=null;
res=myService.searchInQuestions(anyInt(),anyInt(),anyString(),anyString());
资产不为空(res);
} 
我在该方法调用另一个方法的行中遇到空指针异常。请参阅源代码中的注释。是否有人可以告诉我:

1) 我是否使用mockito作为正确的主题?我是否应该使用真实的测试数据?但是数据库连接呢?我尝试了这种方法,最终只使用了mockito

2) 为什么我会得到空指针异常,尽管我已经用Powermockito中断了该方法

3) 请提供您有价值的建议,以正确测试给定的方法


注意:-我不允许在代码中进行任何重构。

您创建了一些模拟,但没有将它们注入到您的
myService
中。您不应该模拟列表(
questions
),而应该使用真实的列表。此外,您可能不想在这里使用
spy
(至少我在您的代码片段中没有看到这样做的原因)。根据您的
QuesService
类的外观,您希望在此处使用
@InjectMocks
。您可能希望添加有关您的类的更多信息。为什么在这种特定情况下尝试使用
PowerMockito