Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Mockito对象mock更改为null_Java_Unit Testing_Mockito - Fatal编程技术网

Java Mockito对象mock更改为null

Java Mockito对象mock更改为null,java,unit-testing,mockito,Java,Unit Testing,Mockito,我试图模拟以下函数: // Class ObjClass public void updateColumn(long id, MyObj myObj, String errorLog) throws Exception { try { MyObj2 obj2 = entityManager.find(MyObj2.class, id); obj2.setStatus(myObj); obj2.setTime(

我试图模拟以下函数:

// Class ObjClass
public void updateColumn(long id, MyObj myObj, String errorLog) throws Exception {
        try {
            MyObj2 obj2 = entityManager.find(MyObj2.class, id);
            obj2.setStatus(myObj);
            obj2.setTime(System.currentTimeMillis());
            obj2.setErrorLog(errorLog);
            entityManager.persist(obj2);
        }
        catch (Exception e) {
            //some stuff...
        }
    }
我的测试类代码如下所示:

@Mock

private EntityManager entityManager;

@InjectMocks
private ObjClass objClass;

@Mock
private MyObj2 myObj2;

Mockito.when(entityManager.find(MyObj2.class, id)).thenReturn(myObj2);
    ObjClass.updateColumn(id, myObj.SUCCESS, null);
这里,当测试代码运行并到达行
MyObj2 obj2=entityManager.find(MyObj2.class,id)时,我得到一个null

然而,在调试时,在我的测试方法到达这个类之前,obj2在这里是空的,obj2具有来自测试类code
@Mock private MyObj2 MyObj2的模拟值

有人能提出可能的问题吗?使用Mockito


此外,我还想模拟updateColumn方法中的setter并验证它们。有人能帮忙吗?

id的值是多少?它变了吗?如果你能重写你的例子,让它包含最小的可执行程序来重现你的问题,那将是可能的。因此,我必须同意updateColumn中id的值很可能与用于mock函数的值不同。也许现在,将mock函数更改为Mockito.when(entityManager.find(eq(MyObj2.class),anyLong()),看看这是否会导致测试工作。