groovy / groovy-eclipse

Eclipse Groovy Development Tools
657 stars 192 forks source link

Test Errors in groovy-ecliplse-compiler with OpenJDK11 #1078

Closed ochstobi closed 4 years ago

ochstobi commented 4 years ago

Hello,

I try to migrate from Java 8 to 11 and get an error in my groovy test classes that seems to be related to the groovy-eclipse-compiler. Executing the Tests in Java8 is fine. But when I execute them with Java11 I get the following error:

[ERROR] testSingletonFail  Time elapsed: 0.009 s  <<< FAILURE!
org.opentest4j.AssertionFailedError: Unexpected exception type thrown
==> expected: <java.lang.IllegalStateException> but was: <java.lang.AbstractMethodError>
         at TestSpringBeanScopeChecker.testSingletonFail(TestSpringBeanScopeChecker.groovy:22)
 Caused by: java.lang.AbstractMethodError: Receiver class
 TestSpringBeanScopeChecker does not define or inherit an
 implementation of the resolved method 'abstract java.lang.Object
 getProperty(java.lang.String)' of interface groovy.lang.GroovyObject.
         at TestSpringBeanScopeChecker.testSingletonFail(TestSpringBeanScopeChecker.groovy:22)

When executing the tests in Intellij it passes. This is due I don't have configured groovy-eclipse-compiler in Intellij. When executing maven from command line the error appers because I use the groovy-eclipse-compiler.

Here is the test class and the scrope checker method:

@SpringJUnitConfig
class TestSpringBeanScopeChecker {
    @Autowired
    ApplicationContext ctx

    @Test
    void testSingletonFail() {
        Assertions.assertThrows(IllegalStateException.class) {
            SpringBeanScopeChecker.check(ctx, DummyPrototype.class, BeanDefinition.SCOPE_SINGLETON)
        }
    }
}
public static void check(ApplicationContext ctx, Class<?> type, String scope)
            throws IllegalStateException {

        AbstractApplicationContext actx = (ctx instanceof AbstractApplicationContext) ? 
                ((AbstractApplicationContext) ctx) : 
                new StaticApplicationContext(ctx);

        ConfigurableListableBeanFactory factory = actx.getBeanFactory();

        for (String key : ctx.getBeanNamesForType(type)) {
            BeanDefinition definition = factory.getMergedBeanDefinition(key);

            if (!scope.equals(definition.getScope())) {
                throw new IllegalStateException(
                        "Every spring bean "
                                + "must be request scoped in the bean configuration. The current scope is: "
                                + definition.getScope());
            }
        }
    }
eric-milles commented 4 years ago

Can you include your POM as well? Groovy 3 gave some groovy.lang.GroovyObject methods a default implementation, which would prevent Groovy 3 compiled code from running on a previous runtime version. This is probably the cause of your AbstractMethodError.

Groovy 2.5's GroovyObject vs. Groovy 3+'s GroovyObject

ochstobi commented 4 years ago

It is a bit unsorted because I copied it from different parent POMs, but I think this is all the relevant groovy stuff:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <compilerVersion>${jdkVersion}</compilerVersion> <!-- 11 -->
        <source>${jdkVersion}</source>
        <target>${jdkVersion}</target>
        <compilerId>${mavenCompilerId}</compilerId> <!-- groovy-eclipse-compiler -->
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-compiler</artifactId>
            <version>3.6.0-03</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-eclipse-batch</artifactId>
            <version>3.0.1-01</version>
        </dependency>
    </dependencies>
</plugin>

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>groovy-maven-plugin</artifactId>
    <version>2.1.1</version>
</plugin>

<pluginManagement>
    <plugin>
        <groupId>org.codehaus.gmaven</groupId>
        <artifactId>groovy-maven-plugin</artifactId>
        <executions>
            <!-- commented out -->
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-all</artifactId>
                <version>${groovy.version}</version> <!-- 2.5.10 inherited from spring -->
                <type>pom</type>
            </dependency>
        </dependencies>
    </plugin>
</pluginManagement>

<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>${groovy.version}</version> <!-- 2.5.10 inherited from spring -->
    <type>pom</type>
    <exclusions>
        <exclusion>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-testng</artifactId>
        </exclusion>
    </exclusions>
</dependency>
eric-milles commented 4 years ago

You are compiling with groovy-eclipse-batch 3.0.1-01 and running with groovy runtime 2.5.10. These should be matched up, so either batch 2.5.10-0x and runtime 2.5.10 or batch 3.0.1-0x and runtime 3.0.x.

vitalii-c commented 3 years ago

Thank you very much for pointing that "groovy-eclipse-batch" should correspond to groovy-all version. I have spent hours until found this post.

My current working setup for mixing both java and groovy code is below, before i've always end up with

00:48 $ tree -L 4 src/
src/
└── main
    ├── groovy
    │   └── com
    │       └── example
    └── java
        └── com
            └── example

7 directories, 0 files
Exception in thread "main" java.lang.AbstractMethodError: com.example.fieldsgroovy.entity.Entity.getProperty(Ljava/lang/String;)Ljava/lang/Object;
    at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:309)
    at com.example.fieldsgroovy.service.AppGroovy.main(AppGroovy.groovy:7)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example.mix</groupId>
    <artifactId>mix-java-groovy</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <jdk.version>8</jdk.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <groovy.cps.version>1.31</groovy.cps.version>
        <!--
                for shared jenkins shared library testing
                <groovy.version>2.4.12</groovy.version>
        -->
        <groovy.version>2.5.14</groovy.version>
        <!--
                https://github.com/groovy/groovy-eclipse/issues/1078
                https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-eclipse-batch
        -->
        <batch.version>2.5.14-02</batch.version>
        <!--           <batch.version>3.0.8-01</batch.version>-->

        <version.org.jenkins-ci.plugins.credentials-binding>1.17</version.org.jenkins-ci.plugins.credentials-binding>
        <version.lombok>1.18.20</version.lombok>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jenkins-ci.plugins</groupId>
            <artifactId>credentials-binding</artifactId>
            <version>${version.org.jenkins-ci.plugins.credentials-binding}</version>
        </dependency>
        <dependency>
            <groupId>com.cloudbees</groupId>
            <artifactId>groovy-cps</artifactId>
            <version>${groovy.cps.version}</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>${groovy.version}</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${version.lombok}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <!--
                                https://www.baeldung.com/groovy-java-applications
                                https://github.com/groovy/groovy-eclipse/wiki/Groovy-Eclipse-Maven-plugin&ndash;&gt;
                                https://stackoverflow.com/questions/8524891/maven-groovy-and-java-lombok&ndash;&gt;
                -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <!--                    https://www.baeldung.com/maven-compiler-plugin-->
                <version>3.8.1</version>
                <configuration>
                    <compilerId>groovy-eclipse-compiler</compilerId>
                    <verbose>true</verbose>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <!--                    <compilerArguments>-->
                    <!--                        <javaAgentClass>lombok.launch.Agent</javaAgentClass>-->
                    <!--                    </compilerArguments>-->
                    <!--                    <fork>true</fork>-->
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.20</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-compiler</artifactId>
                        <version>3.7.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-eclipse-batch</artifactId>
                        <version>${batch.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${version.lombok}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>