Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/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
AspectJ Maven插件破坏GMaven构建的Groovy类_Groovy_Junit_Aspectj_Aspectj Maven Plugin_Gmaven Plugin - Fatal编程技术网

AspectJ Maven插件破坏GMaven构建的Groovy类

AspectJ Maven插件破坏GMaven构建的Groovy类,groovy,junit,aspectj,aspectj-maven-plugin,gmaven-plugin,Groovy,Junit,Aspectj,Aspectj Maven Plugin,Gmaven Plugin,我试图在一个模块上运行Groovy和AspectJ,但当我添加AspectJ时,这些类返回的值似乎与我预期的非常不同: junit.framework.ComparisonFailure: null expected:<2011-04-03> but was:<null> testGetUnixDayFromDate(com.baselogic.chapter05.utils.DateUtilitiesTest) Time elapsed: 0.016 sec &l

我试图在一个模块上运行Groovy和AspectJ,但当我添加AspectJ时,这些类返回的值似乎与我预期的非常不同:

junit.framework.ComparisonFailure: null expected:<2011-04-03> but was:<null>

testGetUnixDayFromDate(com.baselogic.chapter05.utils.DateUtilitiesTest)  Time elapsed: 0.016 sec  <<< FAILURE!
junit.framework.AssertionFailedError: expected:<0> but was:<15037>

testGetDateDifferenceInDays(com.baselogic.chapter05.utils.DateUtilitiesTest)  Time elapsed: 0 sec  <<< FAILURE!
junit.framework.AssertionFailedError: expected:<0> but was:<7>
下面是我的AspectJ插件,它似乎导致了这个问题:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.3.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <complianceLevel>1.6</complianceLevel>
            </configuration>
        </plugin>
然后我还创建了一个Java单元测试用例:

    package com.baselogic.chapter05.utils;

    import org.junit.Test;

    import static org.junit.Assert.assertEquals;

    import java.text.SimpleDateFormat;
    import java.util.Calendar;

    public class DateUtilitiesJavaTest {

        @Test
        public void testGetYesterdayDate() {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String expected = format.format(calendar.getTime());

            String result = DateUtilities.getYesterdayDate();
            // Aspect DontWriteToTheConsole will complain:
            // System.out.println(result);
            assertEquals(expected, result);
        }

        @Test
        public void testGetYesterdayDateScala() {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String expected = format.format(calendar.getTime());

            String result = ScalaDateUtilities.getYesterdayDate();
            assertEquals(expected, result);
        }
    }

有人能帮我理解为什么aspectj maven插件会导致我的最终字节码出现编译错误吗?

我使用GMaven的经验很差。一旦我转到格拉德尔,许多“古怪”就消失了。这听起来有点奇怪,你不想在GMaven上浪费时间。

我认为切换到Gradle不是一个解决方案,而是一个昂贵的解决方案,尤其是对于大型项目。所以我不同意迈克·瓦佐夫斯基的观点。也许你应该修复你的Maven设置。您可以做以下几件事:

  • 首先,AspectJ Maven 1.3.1从2011年开始发布,并使用非常旧的AspectJ版本(1.6.10)。为什么不升级到默认使用AsepctJ 1.8.2的AspectJ Maven 1.7?您甚至可以将其升级到AspectJ 1.8.6,这是包含更多错误修复的最新版本

  • <>你也可以考虑从GMAVEN切换到GMAVEPLUS,因为前者不再是主要的,而后者是

  • 顺便说一句,您也可以从Java6切换到8或至少7,因为Java6不再维护

我的AspectJ Maven设置通常是这样的(我在这里省略了GMaven设置):


UTF-8
1.8
1.8.6
org.apache.maven.plugins
maven编译器插件
3.1
${java.source-target.version}
${java.source-target.version}
假的
org.codehaus.mojo
aspectj maven插件
1.7
真的
${java.source-target.version}
${java.source-target.version}
忽视
${java.source-target.version}
UTF-8
真的
过程源
编译
测试编译
org.aspectj
aspectjtools
${aspectj.version}
org.apache.maven.plugins
maven编译器插件
org.codehaus.mojo
aspectj maven插件
org.aspectj
aspectjrt
${aspectj.version}
运行时
org.aspectj
aspectjrt

如果我们看不到使用Groovy和Java测试用例更新的测试代码,那么显示测试输出就没有什么意义了。您没有编译,只有测试失败。如果您认为它们是由AspectJ Maven引起的,您是否使用了任何方面?如果是的话,你能给我们看一下方面代码吗?请提供一份报告,以使问题重现。我下面的答案只是一个建议,我只能确定,如果我看到你的代码,它会有所帮助。
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Chapter 05: Extending 1.0.2
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ ch05 ---
    [INFO] Deleting C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target
    [INFO]
    [INFO] --- gmaven-plugin:1.3:generateStubs (default) @ ch05 ---
    [INFO] Generated 2 Java stubs
    [INFO]
    [INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ ch05 ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 0 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ ch05 ---
    [INFO] Compiling 2 source files to C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\classes
    [INFO]
    [INFO] --- maven-scala-plugin:2.15.2:compile (default) @ ch05 ---
    [INFO] Checking for multiple versions of scala
    [INFO] includes = [**/*.scala,**/*.java,]
    [INFO] excludes = []
    [INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\main\java:-1: info: compiling
    [INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\generated-sources\groovy-stubs\main:-1: info: compiling
    [INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\main\groovy:-1: info: compiling
    [INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\main\scala:-1: info: compiling
    [INFO] Compiling 3 source files to C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\classes at 1301955015855
    [INFO] prepare-compile in 0 s
    [INFO] compile in 4 s
    [INFO]
    [INFO] --- gmaven-plugin:1.3:compile (default) @ ch05 ---
    [INFO] Compiled 3 Groovy classes
    [INFO]
    [INFO] --- clojure-maven-plugin:1.3.7:compile (compile-clojure) @ ch05 ---
    [INFO]
    [INFO] --- aspectj-maven-plugin:1.3.1:compile (default) @ ch05 ---
    [INFO]
    [INFO] --- gmaven-plugin:1.3:generateTestStubs (default) @ ch05 ---
    [INFO] Generated 2 Java stubs
    [INFO]
    [INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ ch05 ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 0 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ ch05 ---
    [INFO] Compiling 3 source files to C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\test-classes
    [INFO]
    [INFO] --- maven-scala-plugin:2.15.2:testCompile (default) @ ch05 ---
    [INFO] Checking for multiple versions of scala
    [INFO] includes = [**/*.scala,**/*.java,]
    [INFO] excludes = []
    [INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\test\java:-1: info: compiling
    [INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\generated-sources\groovy-stubs\test:-1: info: compiling
    [INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\test\groovy:-1: info: compiling
    [INFO] C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\src\test\scala:-1: info: compiling
    [INFO] Compiling 3 source files to C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\test-classes at 1301955027323
    [INFO] prepare-compile in 0 s
    [INFO] compile in 3 s
    [INFO]
    [INFO] --- gmaven-plugin:1.3:testCompile (default) @ ch05 ---
    [INFO] Compiled 2 Groovy classes
    [INFO]
    [INFO] --- aspectj-maven-plugin:1.3.1:test-compile (default) @ ch05 ---
    [WARNING] No sources found skipping aspectJ compile
    [INFO]
    [INFO] --- maven-surefire-plugin:2.7.1:test (default-test) @ ch05 ---
    [INFO] Surefire report directory: C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\surefire-reports

    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running com.baselogic.chapter05.utils.DateUtilitiesJavaTest
    Apr 4, 2011 6:10:31 PM com.baselogic.chapter05.utils.DateUtilities getYesterdayDate_aroundBody11$advice
    INFO: --------------------
    Apr 4, 2011 6:10:31 PM com.baselogic.chapter05.utils.DateUtilities getYesterdayDate_aroundBody11$advice
    ...
    Tests run: 26, Failures: 21, Errors: 0, Skipped: 0, Time elapsed: 0.093 sec <<< FAILURE!

    Results :

    Failed tests:
      testGetYesterdayDate(com.baselogic.chapter05.utils.DateUtilitiesJavaTest)
    ...

    Tests run: 31, Failures: 26, Errors: 0, Skipped: 0

    [ERROR] There are test failures.

    Please refer to C:\usr\SYNCH\PACKT\3166\Chapters_Code\ch05\target\surefire-reports for the individual test results.
    package com.baselogic.chapter05.utils

    import groovy.util.GroovyTestCase
    import java.util.Calendar
    import java.text.SimpleDateFormat

    class DateUtilitiesTest extends GroovyTestCase {
        protected void setUp() {
            super.setUp()
        }

        protected void tearDown() {
            super.tearDown()
        }

        public void testGetYesterdayDate() {
            Calendar calendar = Calendar.getInstance()
            calendar.add(Calendar.DATE, -1)
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd")
            String expected = format.format(calendar.getTime());

            String result = DateUtilities.getYesterdayDate()
            println(result)
            assertEquals(expected, result)
        }

        void testGetUnixDayFromDate() {
            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            java.util.Date date = (java.util.Date) formatter.parse("2011-03-03 23:59:59");
            assertEquals(DateUtilities.getUnixDayFromDate(date), 15037);
        }

        void testGetDateDifferenceInDays() {
            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            java.util.Date date1 = (java.util.Date) formatter.parse("2011-03-03 23:59:59");
            java.util.Date date2 = (java.util.Date) formatter.parse("2011-03-10 23:59:59");
            assertEquals(DateUtilities.getDateDifferenceInDays(date1, date2), 7);
        }
    }
    package com.baselogic.chapter05.utils;

    import org.junit.Test;

    import static org.junit.Assert.assertEquals;

    import java.text.SimpleDateFormat;
    import java.util.Calendar;

    public class DateUtilitiesJavaTest {

        @Test
        public void testGetYesterdayDate() {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String expected = format.format(calendar.getTime());

            String result = DateUtilities.getYesterdayDate();
            // Aspect DontWriteToTheConsole will complain:
            // System.out.println(result);
            assertEquals(expected, result);
        }

        @Test
        public void testGetYesterdayDateScala() {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String expected = format.format(calendar.getTime());

            String result = ScalaDateUtilities.getYesterdayDate();
            assertEquals(expected, result);
        }
    }