【JUnit 报错】 method initializationerror not found:JUnit4单元测试报错问题

用JUnit测试一段代码,出现报错method initializationerror not found;出现如下问题:

双击这个就显示出现如下的错误:

method \’initializationError\’ not found.

解决办法:

除了引入junit-4.12.jar之外,还要引入2个依赖jar包:hamcrest-core-1.3.rc2.jar,hamcrest-library-1.3.rc2.jar

引入jar包的方法:

点击 https://mvnrepository.com/(首先你得是maven项目可以这么引用)

搜索hamcrest-core,hamcrest-library,分别粘贴对应的标签到你的pom.xml

如粘贴

<!– https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core –>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>

到pom.xml中,如下:

<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
<modelVersion>4.0.0</modelVersion>
<groupId>Zip</groupId>
<artifactId>Zip</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!– https://mvnrepository.com/artifact/org.slf4j/slf4j-api –>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!– https://mvnrepository.com/artifact/com.github.livesense/org.liveSense.fragment.sun.misc –>
<dependency>
<groupId>com.github.livesense</groupId>
<artifactId>org.liveSense.fragment.sun.misc</artifactId>
<version>1.0.5</version>
</dependency>
<!– https://mvnrepository.com/artifact/junit/junit –>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!– https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core –>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<!– https://mvnrepository.com/artifact/org.hamcrest/hamcrest-library –>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<!– https://mvnrepository.com/artifact/com.github.digital-wonderland.sling-metrics/cq-metrics-fragment –>
<dependency>
<groupId>com.github.digital-wonderland.sling-metrics</groupId>
<artifactId>cq-metrics-fragment</artifactId>
<version>0.1.0</version>
</dependency>
<!– https://mvnrepository.com/artifact/net.sdruskat/net.sdruskat.fragment.sun.misc –>
<dependency>
<groupId>net.sdruskat</groupId>
<artifactId>net.sdruskat.fragment.sun.misc</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

版权声明:本文为cicy原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/cicy/p/8479584.html