GoogleCloudPlatform / spring-cloud-gcp

New home for Spring Cloud GCP development starting with version 2.0.
Apache License 2.0
410 stars 304 forks source link

Address native image runtime failure for spring-cloud-gcp-data-spanner-repository-sample #2343

Open mpeddada1 opened 9 months ago

mpeddada1 commented 9 months ago

Command: mvn clean test --activate-profiles native-sample-config,nativeTest --define notAllModules=true -Dmaven.javadoc.skip=true -Dcheckstyle.skip -Dtests=com.example.SpannerRepositoryIntegrationTests#testRestEndpointPut

Results in:

JUnit Jupiter:SpannerRepositoryIntegrationTests:testRestEndpoint()
    MethodSource [className = 'com.example.SpannerRepositoryIntegrationTests', methodName = 'testRestEndpoint', methodParameterTypes = '']
    => java.lang.IllegalStateException: Failed to load ApplicationContext for [AotMergedContextConfiguration@4cc91e70 testClass = com.example.SpannerRepositoryIntegrationTests,
...
  [...]
     Caused by: org.graalvm.nativeimage.MissingReflectionRegistrationError: The program tried to reflectively invoke method public synchronized int org.apache.coyote.AbstractProtocol.getNameIndex() without it being registered for runtime reflection. Add it to the reflection metadata to solve this problem. See https://www.graalvm.org/latest/reference-manual/native-image/metadata/#reflection for help.
       org.graalvm.nativeimage.builder/com.oracle.svm.core.reflect.MissingReflectionRegistrationUtils.forQueriedOnlyExecutable(MissingReflectionRegistrationUtils.java:97)
       java.base@17.0.7/java.lang.reflect.Method.acquireMethodAccessor(Method.java:77)
       java.base@17.0.7/java.lang.reflect.Method.invoke(Method.java:566)
       org.apache.tomcat.util.IntrospectionUtils.getProperty(IntrospectionUtils.java:250)
       org.apache.catalina.connector.Connector.getProperty(Connector.java:298)
       [...]

Adding the following reflection configs as suggested resolves the previous error but results in an NPE at https://github.com/GoogleCloudPlatform/spring-cloud-gcp/blob/28ab0c29e5763df42e986e8c31a9fe5a22fa0416/spring-cloud-gcp-samples/spring-cloud-gcp-data-spanner-repository-sample/src/test/java/com/example/SpannerRepositoryIntegrationTests.java#L87

[
  {
    "name":"org.apache.coyote.AbstractProtocol",
    "queryAllPublicMethods":true,
    "queryAllDeclaredMethods":true,
    "methods":[{"name": "getName", "parameterTypes":[]},{"name": "getNameIndex", "parameterTypes":[]},{"name":"getAddress","parameterTypes":[] }, {"name":"getLocalPort","parameterTypes":[] }, {"name":"setPort","parameterTypes":["int"] }, {"name":"setProperty","parameterTypes":["java.lang.String","java.lang.String"] }]
  }
]

Stacktrace:

com.example.SpannerRepositoryIntegrationTests > testRestEndpoint() FAILED

com.example.SpannerRepositoryIntegrationTests > testLoadsCorrectData() SUCCESSFUL

com.example.SpannerRepositoryIntegrationTests > testRestEndpointPut() FAILED

com.example.SpannerRepositoryIntegrationTests > testJsonAndArrayJsonFieldReadWrite() SUCCESSFUL

com.example.SpannerRepositoryMultiDatabaseIntegrationTests > testLoadsCorrectData() SUCCESSFUL

Failures (2):
  JUnit Jupiter:SpannerRepositoryIntegrationTests:testRestEndpoint()
    MethodSource [className = 'com.example.SpannerRepositoryIntegrationTests', methodName = 'testRestEndpoint', methodParameterTypes = '']
    => java.lang.NullPointerException
       com.example.SpannerRepositoryIntegrationTests.testRestEndpoint(SpannerRepositoryIntegrationTests.java:87)
       java.base@17.0.7/java.lang.reflect.Method.invoke(Method.java:568)
       org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727)
       org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
       org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
       org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156)
       org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147)
       org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86)
       org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(InterceptingExecutableInvoker.java:103)
       org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.lambda$invoke$0(InterceptingExecutableInvoker.java:93)
       [...]
  JUnit Jupiter:SpannerRepositoryIntegrationTests:testRestEndpointPut()
    MethodSource [className = 'com.example.SpannerRepositoryIntegrationTests', methodName = 'testRestEndpointPut', methodParameterTypes = '']
    => java.lang.AssertionError: 
expected: Trader{traderId='t123', firstName='John', lastName='Smith', createdOn=2000-01-02 03:04:05.0, modifiedOn=[2000-01-02 03:04:05.0]}
 but was: null
       com.example.SpannerRepositoryIntegrationTests.testRestEndpointPut(SpannerRepositoryIntegrationTests.java:117)
       java.base@17.0.7/java.lang.reflect.Method.invoke(Method.java:568)
       org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727)
       org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
       org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
       org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156)
       org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147)
       org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86)
       org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(InterceptingExecutableInvoker.java:103)
       org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.lambda$invoke$0(InterceptingExecutableInvoker.java:93)
       [...]
diegomarquezp commented 7 months ago

Hi @mpeddada1, is this issue still occuring?

mpeddada1 commented 7 months ago

Thanks for checking in @diegomarquezp. Yes, I believe this issue still exists with the spanner-repository-sample and native image compilation.