侧边栏壁纸
博主头像
lai博主等级

  • 累计撰写 21 篇文章
  • 累计创建 12 个标签
  • 累计收到 0 条评论

java.lang.NoClassDefFoundError: org/assertj/core/api/Assertions

lai
lai
2021-05-28 / 0 评论 / 0 点赞 / 1,043 阅读 / 4,312 字
温馨提示:
本文最后更新于 2021-05-28,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

使用STS在eclipse构建完简单Spring Boot 工程后,进行run - > maven test控制台抛出异常java.lang.NoClassDefFoundError: org/assertj/core/api/Assertions

问题描述

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 3.096 s <<< FAILURE! - in com.example.multimodule.MyServiceTest
contextLoads  Time elapsed: 0.511 s  <<< ERROR!
java.lang.NoClassDefFoundError: org/assertj/core/api/Assertions
	at com.example.multimodule.MyServiceTest.contextLoads(MyServiceTest.java:19)
Caused by: java.lang.ClassNotFoundException: org.assertj.core.api.Assertions
	at com.example.multimodule.MyServiceTest.contextLoads(MyServiceTest.java:19)

pom.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.6</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>laijava.com</groupId>
	<artifactId>library</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>laijava-library-module</name>
	<description>A library might merely provide pure Java APIs and no Spring features</description>
	<properties>
		<java.version>1.8</java.version>
		<!-- 配置之后,Maven->Update Project -> force Update of Snapshots/Releases 解决Eclipse下Maven 
			unknown问题 -->
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>
</project>

springBoot测试代码

package com.example.multimodule;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;

import com.example.multimodule.service.MyService;

@SpringBootTest("service.message=Hello")
public class MyServiceTest {
	@Autowired
	private MyService myService;

	@Test
	public void contextLoads() {
		assertThat(myService.message()).isNotNull();
	}

	@SpringBootApplication
	static class TestConfiguration {
	}
}

问题解决

翻查maven test执行日志,发现相关ERROR信息xxxxr时出错; invalid LOC header (bad signature)

[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ library ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to D:\java\myworkspace\laijava-library-module\target\classes
[ERROR] 读取C:\Users\user\.m2\repository\org\yaml\snakeyaml\1.27\snakeyaml-1.27.jar时出错; invalid LOC header (bad signature)
[ERROR] 读取C:\Users\user\.m2\repository\org\assertj\assertj-core\3.18.1\assertj-core-3.18.1.jar时出错; invalid LOC header (bad signature)
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ library ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory D:\java\myworkspace\laijava-library-module\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ library ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\java\myworkspace\laijava-library-module\target\test-classes
[ERROR] 读取C:\Users\user\.m2\repository\org\yaml\snakeyaml\1.27\snakeyaml-1.27.jar时出错; invalid LOC header (bad signature)
[ERROR] 读取C:\Users\user\.m2\repository\org\assertj\assertj-core\3.18.1\assertj-core-3.18.1.jar时出错; invalid LOC header (bad signature)
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ library ---

invalid LOC header (bad signature)原因是MAVEN本地仓库中的对应的某个jar没有下载完全,需要删除后,重新maven-update project,针对部分国内无法下载的jar需要修改仓库镜像源,这里推荐阿里云公共仓库

阿里云公共仓库

maven配置:apache-maven-3.6.0\conf\settings.xml

<mirrors>
	<mirror>
      		other mirror config...     
	</mirror>

	<mirror>
		<id>aliyunmaven</id>
		<mirrorOf>*</mirrorOf>
		<name>阿里云公共仓库</name>
		<url>https://maven.aliyun.com/repository/public</url>
	</mirror>	
</mirrors>

重新下载jar包后eclipse执行run - > maven test,测试通过,ClassNotFoundException问题解决

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.962 s - in com.example.multimodule.MyServiceTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.790 s
[INFO] Finished at: 2021-05-27T16:06:25+08:00
[INFO] Final Memory: 17M/368M
[INFO] ------------------------------------------------------------------------
0

评论区