Java 为什么运行ung mvn clean install命令(在Spring引导项目上)如果我注释掉了这些方法以跳过测试,那么我也会获得单元测试失败?
从几年前开始,我就没有使用Spring\Spring Boot,我正在启动一个使用该技术的项目,因此我有一些疑问 我正在处理一个从GIT检索到的简单项目,并开始着手实现我的功能。它工作正常,但我有以下疑问 从命令行(进入项目的根目录),我使用以下命令编译它:Java 为什么运行ung mvn clean install命令(在Spring引导项目上)如果我注释掉了这些方法以跳过测试,那么我也会获得单元测试失败?,java,spring,spring-boot,maven,junit,Java,Spring,Spring Boot,Maven,Junit,从几年前开始,我就没有使用Spring\Spring Boot,我正在启动一个使用该技术的项目,因此我有一些疑问 我正在处理一个从GIT检索到的简单项目,并开始着手实现我的功能。它工作正常,但我有以下疑问 从命令行(进入项目的根目录),我使用以下命令编译它: mvn clean install 为了删除我的应用程序的早期版本,然后再次编译、测试和打包我的应用程序 然后,我通过以下命令启动应用程序: mvn spring-boot:run 并且应用程序正确启动: 一开始就没有问题。我的疑问是在
mvn clean install
为了删除我的应用程序的早期版本,然后再次编译、测试和打包我的应用程序
然后,我通过以下命令启动应用程序:
mvn spring-boot:run
并且应用程序正确启动:
一开始就没有问题。我的疑问是在执行mvn clean install命令的前一阶段。它还执行项目中定义的单元测试,但所有测试都失败。这里是奇怪的行为
我得到这样的例外:
Running com.springboot.excelapi.integration.ExcelResourceUnitTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.001 sec <<< FAILURE! - in com.springboot.excelapi.integration.ExcelResourceUnitTest
initializationError(com.springboot.excelapi.integration.ExcelResourceUnitTest) Time elapsed: 0 sec <<< ERROR!
java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:138)
at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
奇怪的是,我在JUnit类中注释了所有的测试方法。例如,前面的错误与此测试类相关:
package com.springboot.excelapi.integration;
import java.io.IOException;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.springboot.excelapi.Application;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { Application.class })
@WebAppConfiguration
@ActiveProfiles(profiles = { "no-liquibase" })
public class ExcelResourceUnitTest {
/*
@Test
public void processCompVibrAndTempoExcelTabTest()
{
final ExcelService excelService = new ExcelService();
try {
excelService.processCompVibrAndTempExcelTab();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
*/
}
正如您所看到的,这个类包含一个被注释掉的测试方法
那么,为什么我在执行mvn clean install命令时,它似乎在试图执行测试方法,而此时该方法被注释掉而被禁用呢?如果您不想运行测试,请对类或单个测试方法使用
@Ignore
似乎仍然有运行的测试(maven总结了4个测试)
因此,总而言之,您可能不应该总是运行mvn-DskipTests clean install
,或者至少不应该总是运行它,因为您仍然希望运行工作测试
现在,对于错误,您应该至少有一个公共方法用
@Test
注释,因此保留该方法,但注释掉它的内容(同样,我最好使用@Ignore
,正如我之前所说)至于失败的测试:即使注释掉它们,我也不确定测试是在什么时候执行的。你保存更改了吗?但测试一开始不应该失败。我建议联系开发商并征求意见。备注:您可以通过执行mvn-DskipTests clean install
@Turing85等方式停用测试。显然,我在重新运行mvn clean install之前保存了代码,但与注释不一致,请重试
package com.springboot.excelapi.integration;
import java.io.IOException;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.springboot.excelapi.Application;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { Application.class })
@WebAppConfiguration
@ActiveProfiles(profiles = { "no-liquibase" })
public class ExcelResourceUnitTest {
/*
@Test
public void processCompVibrAndTempoExcelTabTest()
{
final ExcelService excelService = new ExcelService();
try {
excelService.processCompVibrAndTempExcelTab();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
*/
}