【问题标题】:Unable to execute groovy test files in Springboot application无法在 Spring Boot 应用程序中执行 groovy 测试文件
【发布时间】:2020-03-26 22:22:33
【问题描述】:

我创建了一个示例SpringBoot 应用程序,用于使用Spock 框架编写单元测试。以下是文件。

Calculator.Java

public class Calculator {
   @Autowired
   Addition addition;

   public int calculate(int a, int b, String choice) {
      return addition.add(a, b);
   }
}

我已经使用 Junit 创建了测试类,如下所示,它工作正常

CalculatorTest.java

@SpringBootTest
public class CalculatorTest {

   @Mock
   Addition addition;

   @InjectMocks
   Calculator cal;

   @Test
   public void testCal() {
     when(addition.add(2, 3)).thenReturn(8);
     int value = cal.calculate(2, 3, "add");
     assertEquals(8, value);
   }
}

但是我需要使用Spock Framework编写单元测试,所以我创建了groovy文件并在pom.xml中添加了相应的dependenciesplugins。当我运行mvn clean install 来构建应用程序时。在TESTS 下我只能看到CalculatorTest.java 执行,但看不到CalculatorGroovyTest.groovy 文件的执行。

CalculatorGroovyTest.groovy

@SpringBootTest
class CalculatorGroovyTest extends Specification{

   @Mock
   Addition addition;

   @InjectMocks
   Calculator cal;

   def "Callator test method"(){
       given:
           addition.add(5, 8) --> 9;
       when:
           int val = cal.calculate(5, 8, "add");
       then:
           val == 9;
   }
}

以下是我在 pom 中添加的依赖项和插件。

依赖关系

<dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-core</artifactId>
        <version>1.3-groovy-2.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>2.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-guice</artifactId>
        <version>1.0-groovy-2.4</version>
        <scope>test</scope>
    </dependency>

插件

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <useFile>false</useFile>
                <includes>
                    <include>**/*Spec.groovy</include>
                    <include>**/*Test.groovy</include>
                    <include>**/*Test.java</include>
                </includes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <testSources>
                    <testSource>
                        <directory>${project.basedir}/src/test/java</directory>
                        <includes>
                            <include>**/*.groovy</include>
                        </includes>
                    </testSource>
                </testSources>
            </configuration>
        </plugin>

注意:在 maven 构建后我没有收到任何错误并获得构建成功

我不确定为什么不考虑使用 groovyfiles 进行单元测试

【问题讨论】:

  • 您介意将您的示例存储库推送到 GitHub 吗?我想克隆并检查它。
  • 你有什么理由把你的 groovy 文件放在 ${project.basedir}/src/test/java 而不是 ${project.basedir}/src/test/groovy 中?此外,为什么你在 spring 项目中有一个过时的 spock-guice 依赖项? spock-core 1.3-groovy-2.5 需要 groovy 2.5 而不是 2.4。

标签: spring-boot maven groovy spock spring-boot-test


【解决方案1】:

您的依赖项中似乎缺少spock-spring

<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-spring</artifactId>
    <version>1.2-groovy-2.5</version>
    <scope>test</scope>
</dependency>

我也没有在该列表中看到spring-boot-starter-test,请确保它在那里。

【讨论】:

  • 我还添加了 spock-spring 依赖项,但它不起作用。 spring-boot-starter-test 在那里,它是默认依赖,所以我没有明确提到它。
猜你喜欢
  • 2019-05-02
  • 2021-10-18
  • 2021-07-27
  • 2016-07-01
  • 1970-01-01
  • 2020-09-07
  • 2016-10-20
  • 2023-03-10
  • 1970-01-01
相关资源
最近更新 更多