2

Wondering if anyone could help or has done something similar. I am trying to mock returning an Oracle CLOB from a custom ClobFactory. I am using the following code to create the CLOB

CLOB clob = CLOB.createTemporary(connection, false, CLOB.DURATION_SESSION);
try (final Writer clobWriter = clob.setCharacterStream(1)) {
    clobWriter.write(profile);
} catch (IOException e) {
    throw new SQLException(e);
}
return clob;

And my test I have set up the following mocks

@Mock OracleDataSource        dataSource;
@Mock CreateProfileParameters parameters;
@Mock ClobDBAccess            clobDbAccess;
@Mock OracleConnection        connection;

@Mock ClobFactory clobFactory;
@Mock CLOB        clob;
@Mock Writer      clobWriter;

....

when(dataSource.getConnection()).thenReturn(connection);
when(CLOB.getDBAccess(connection)).thenReturn(clobDbAccess);
when(clobDbAccess.createTemporaryClob(connection, false, CLOB.DURATION_SESSION, (short) 0)).thenReturn(clob);
when(CLOB.createTemporary(connection, false, CLOB.DURATION_SESSION)).thenReturn(clob);
when(ClobFactory.getClob(connection, dataString)).thenReturn(clob);

But I keep getting a NullPointerException each time the test is run, pointing to the second line -

Stacktrace was: java.lang.NullPointerException
at oracle.sql.CLOB.getDBAccess(CLOB.java:1525) ...

Has anyone tried something like this before? Or am I missing out another object that I should be mocking?

4

2 回答 2

1

该方法CLOB.getDBAccess(connection)CLOB类的静态方法,因此不能用 Mockito 模拟。

您可以使用Powermock模拟静态。

于 2013-09-27T10:56:29.797 回答
0

之前没见过类的getDBAccess方法CLOB。但是看上面的内容,在我看来它可能是final,这意味着你不能用 Mockito 存根它。我建议使用真实的CLOB,而不是模拟的。模拟值对象通常是个坏主意,而 aCLOB绝对是一个值对象。

于 2013-09-27T10:53:29.957 回答