篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java做单元测试的时候报错,网上的教程都看了,感觉没啥问题,但是就是调不通,求大神指教一下?大谢!相关的知识,希望对你有一定的参考价值。
org.junit.runners.model.InvalidTestClassError: Invalid test class 'com.zyzy.hairdressers.HairdressersApplicationTests': 1. The class com.zyzy.hairdressers.HairdressersApplicationTests is not public. 2. Test class should have exactly one public constructor 3. No runnable methods
配置@SpringBootTest的classes↓:
@SpringBootTest(classes = SpringConfig.class)
SpringConfig.java↓:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com")
public class SpringConfig
pom.xml↓:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
下次请附带pom.xml。单就几行代码,和报错信息看不出什么。
?Java 单元测试报错:Test class should have exactly one public zero-argument constructor?
public class Test{
private String a;
private int b;
}
public Test(String a, int b){
this.a = a;
this.b = b;
}
public boolean t1(String s){
if(s.length()){
return true;
}else{
return false;
}
}
@Test
public void test1(){
Test t = new Test("aaa", 5);
t.t1("123");
}
运行程序出现:
java.lang.Exception:
Test class should have exactly one public zero-argument constructor
原因:该类中包含了有参构造函数。解决办法:将有参构造函数的类独立出来即可。