0

I am trying to create Mock for google spanner but not getting error.

Class method is

 public List<T> test(Emp emp)
    {

        String sql = "SELECT * FROM EMP where VAL= @val;

-- few logics are mention and then below line which need mocking.

Statement.Builder buildStatement = Statement.newBuilder(sql);
        buildStatement.bind("val").to(11);

        List<Emp> testList= this.spannerTemplate.query(Emp.class,
                buildStatement.build(), null);

Test class is

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Statement.class })
public class Test
{
---------
---------
---------
            Statement statement = PowerMockito.mock(Statement.class);
            PowerMockito.whenNew(Statement.class).withAnyArguments().thenReturn(statement);
            Mockito.doReturn(builder).when(statement).newBuilder(any(String.class));

            when(this.spannerTemplate.query(Emp.class, builder.build(), null)).thenReturn(list);
--------
}

Error as of now is below, java.lang.IllegalStateException: Failed to transform class with name com.google.cloud.spanner.Statement. Reason: null

at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:296)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:204)

If i change the way of Mocking then get Null Pointer exception or similar kind of error. I am not able to understand how to mock the above 3 line.

This is from google.spanner

  • Which version of JDK are you using? Are you using by any change 8? – Stefan Neacsu Feb 06 '20 at 11:49
  • Yes. it's jdk 8 – Rahul Verma Feb 07 '20 at 06:19
  • Does [Powermock - java.lang.IllegalStateException: Failed to transform class](https://stackoverflow.com/questions/32854688/powermock-java-lang-illegalstateexception-failed-to-transform-class) answer your question? There is an [example](https://stackoverflow.com/a/40990838/7725879) which may come in useful for you. Let me know how it goes. – sllopis Feb 10 '20 at 15:40

0 Answers0