为什么@Mock和Mockito.mock(UriInfo.class)之间存在差异?

时间:2017-11-21 19:55:50

标签: java junit mockito junit4 java-ee-6

Mockito.mock和@Mock应该是do the same thing。奇怪的是,在模仿UriInfo时似乎并非如此。在使用JUnit4的单元测试中,下面的代码可以工作:

private UriInfo uriInfo = Mockito.mock(UriInfo.class);

而这引发了"参数uriInfo的错误未设置"在运行测试时:

@Mock
private UriInfo uriInfo;

1 个答案:

答案 0 :(得分:1)

你做过模拟初始化吗?

<cfscript>
    // Create array of random string assignments for ExamNo
    examNos = [] ;
    for(i=1; i lte 100;i++) {
        arrayAppend(examNos,right('00'& i,3)) ;
    }

    ///// Now do the work. ////
    //Create a struct to hold final results
    testAssignments = {} ;
    // Loop through the query and build random assignments
    for(row in testjd) {
        // Get random exam no.
        examNo = examNos[randrange(1,arrayLen(examNos))] ; 
        // Build struct of exam assignments
        structInsert(testAssignments, row.SoonerID, examNo) ; 
        // Delete that num from exam array. No reuse.
        arrayDelete(examNos,examNo) ; 
    }
</cfscript>

或者您可以在测试班上使用特殊的跑步者:

@Before
public void before() {
    MockitoAnnotations.initMocks(this);
}

你在哪里使用嘲笑的@RunWith(MockitoJUnitRunner.class) ?你有没有为它的方法指定一个模拟实现?例如,

urlInfo