在为Spring Boot项目设置Spock时,使用Maven运行测试编译目标时遇到了问题。 我有一个名为 WcyUrlsFactory 的Java类,它带有要使用Groovy / Spock测试的方法 createGroupPageUrl()。该类位于src / main / java /...
package pl.edu.wat.wcy.bali.sync.service.session.utils;
import lombok.extern.slf4j.Slf4j;
import pl.edu.wat.wcy.bali.sync.service.fetcher.GroupRequestParams;
import java.time.Clock;
import java.time.LocalDate;
import java.time.Month;
import static pl.edu.wat.wcy.bali.sync.service.session.utils.WcyUrlParams.*;
@Slf4j
public class WcyUrlsFactory {
private WcyUrlsFactory() {
}
public static String createGroupPageUrl(GroupRequestParams params) {
return createIndexPageUrl(params.getSessionId()) +
MID.value() +
iid(params.getSemester(), params.getClock()) +
exv(params.getGroupName());
}
...
}
经过测试的方法有一个参数,它是 GroupRequestParams.class 的实例。此类也位于src / main / java中,但位于不同的程序包中:
package pl.edu.wat.wcy.bali.sync.service.fetcher;
import lombok.Builder;
import lombok.Value;
import pl.edu.wat.wcy.bali.sync.service.session.utils.Semester;
import java.time.Clock;
@Value
@Builder
public class GroupRequestParams {
private final Clock clock;
private final String sessionId;
private final Semester semester;
private final String groupName;
}
我已经在Groovy中创建了规范类-该类与测试对象位于同一包中,位于src / test / groovy / ...目录中:
package pl.edu.wat.wcy.bali.sync.service.session.utils
import pl.edu.wat.wcy.bali.sync.service.fetcher.GroupRequestParams
import spock.lang.Specification
import java.time.Clock
import java.time.Instant
import java.time.ZoneId
class WcyUrlsFactorySpec extends Specification {
def "should create group page url for current date"() {
System.println("It works!")
expect:
def clock = clockSetTo(currentTime)
result == WcyUrlsFactory.createGroupPageUrl(GroupRequestParams.builder()
.semester(semester)
.groupName("I8B2S4")
.sessionId("4a899a8ebf2e7707db28b11419831b6b&t=6799928")
.clock(clock)
.build())
where:
semester | currentTime || result
...
}
}
在Idea中运行测试工作正常,但是当我尝试使用Maven构建工件时,出现错误:
> mvn clean test-compile -e
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building bali-sync 0.1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ bali-sync ---
[INFO]
[INFO] --- gmavenplus-plugin:1.6:compileTests (default) @ bali-sync ---
[INFO] Using Groovy 2.4.4 to perform compileTests.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.108 s
[INFO] Finished at: 2019-03-07T20:41:36+01:00
[INFO] Final Memory: 25M/309M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.gmavenplus:gmavenplus-plugin:1.6:compileTests (default) on project bali-sync: Error occurred while calling a method on a Groovy class from classpath.: InvocationTargetException: startup failed:
[ERROR] /home/mnowicki/IdeaProjects/bali/bali-sync/src/test/groovy/pl/edu/wat/wcy/bali/sync/service/session/utils/WcyUrlsFactorySpec.groovy: 3: unable to resolve class pl.edu.wat.wcy.bali.sync.service.fetcher.GroupRequestParams
[ERROR] @ line 3, column 1.
[ERROR] import pl.edu.wat.wcy.bali.sync.service.fetcher.GroupRequestParams
[ERROR] ^
[ERROR]
[ERROR] 1 error
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.gmavenplus:gmavenplus-plugin:1.6:compileTests (default) on project bali-sync: Error occurred while calling a method on a Groovy class from classpath.
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:309)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:955)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: Error occurred while calling a method on a Groovy class from classpath.
at org.codehaus.gmavenplus.mojo.CompileTestsMojo.execute (CompileTestsMojo.java:74)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:134)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:309)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:955)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.gmavenplus.util.ReflectionUtils.invokeMethod (ReflectionUtils.java:275)
at org.codehaus.gmavenplus.mojo.AbstractCompileMojo.doCompile (AbstractCompileMojo.java:197)
at org.codehaus.gmavenplus.mojo.CompileTestsMojo.execute (CompileTestsMojo.java:70)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:134)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:309)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:955)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/home/mnowicki/IdeaProjects/bali/bali-sync/src/test/groovy/pl/edu/wat/wcy/bali/sync/service/session/utils/WcyUrlsFactorySpec.groovy: 3: unable to resolve class pl.edu.wat.wcy.bali.sync.service.fetcher.GroupRequestParams
@ line 3, column 1.
import pl.edu.wat.wcy.bali.sync.service.fetcher.GroupRequestParams
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors (ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits (CompilationUnit.java:946)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation (CompilationUnit.java:593)
at org.codehaus.groovy.control.CompilationUnit.compile (CompilationUnit.java:542)
at org.codehaus.groovy.control.CompilationUnit.compile (CompilationUnit.java:525)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.gmavenplus.util.ReflectionUtils.invokeMethod (ReflectionUtils.java:275)
at org.codehaus.gmavenplus.mojo.AbstractCompileMojo.doCompile (AbstractCompileMojo.java:197)
at org.codehaus.gmavenplus.mojo.CompileTestsMojo.execute (CompileTestsMojo.java:70)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:134)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:309)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:194)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:107)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:955)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
[ERROR]
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
我的 pom.xml 包含gmavenplus:
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<goals>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
当我将GroupRequestParams.class移至与测试对象相同的类时,Maven测试通过-我如何在src / main / java中创建gmavenplus扫描源?