spring-boot-starter-test & jsonasserts 冲突

报错信息:java.lang.NoSuchMethodError: org.json.JSONArray.iterator()Ljava/util/Iterator…

错误原因:https://github.com/spring-projects/spring-boot/issues/8706
主要原因是
org.springframework.boot:spring-boot-starter-test -> 1.5.2.RELEASE has an indirect dependency on com.vaadin.external.google:android-json:0.0.20131108.vaadin1 via org.skyscreamer:jsonassert:1.4.0.
The reference documentation mentions both org.skyscreamer:jsonassert:1.4.0 and org.json:json:20140107. As far as I understand, they should be compatible. However, com.vaadin.external.google:android-json:0.0.20131108.vaadin1 conflicts with org.json:json:20140107 as it represents the re-implementation of some older version of org.json:json.
In particular, org.json.JSONTokener#JSONTokener(java.io.Reader) exists only in org.json:json:20140107 and is absent in com.vaadin.external.google:android-json:0.0.20131108.vaadin1, causing my tests to fail with java.lang.NoSuchMethodError: org.json.JSONTokener.(Ljava/io/Reader;)V exceptions.
The solution to my issue is not to use spring-boot-starter-test, but instead use spring-boot-test, spring-boot-test-autoconfigure and spring-test directly and add all other dependencies as needed.
In a case, that org.skyscreamer:jsonassert:1.4.0 is required, exclude com.vaadin.external.google:android-json:0.0.20131108.vaadin1.
If looking for the JSON assertions library consider net.javacrumbs.json-unit:json-unit-fluent that has similar to AssertJ fluent API.

引入了两个org.json包,其中一个是旧版本,不存在iterator方法。

解决方案1:
标记为不包含:

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<exclusions>
			<exclusion>
				<groupId>org.skyscreamer</groupId>
				<artifactId>jsonassert</artifactId>
			</exclusion>
		</exclusions>
	</dependency>

解决方案2:
不使用foreach循环调用iterator,使用普通for(int index=0; index<length; index++)循环。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值