冲突org.springframework.boot.test.context.SpringBootTest;和org.springframework.test.context.junit.jupiter.SpringExtension;

冲突org.springframework.boot.test.context.SpringBootTest;和org.springframework.test.context.junit.jupiter.SpringExtension;,junit,mockito,spring-test,spring-boot-test,junit-jupiter,Junit,Mockito,Spring Test,Spring Boot Test,Junit Jupiter,我正在开发一个spring boot应用程序。 在我的build.gradle中,我添加了依赖项: compile group: 'org.springframework', name: 'spring-test', version: '3.1.1.RELEASE' implementation group: 'commons-io', name: 'commons-io', version: '2.6' 要在我的服务中使用MockMultipartFile,请执行以下操作: import o

我正在开发一个spring boot应用程序。 在我的build.gradle中,我添加了依赖项:

compile group: 'org.springframework', name: 'spring-test', version: '3.1.1.RELEASE'
implementation group: 'commons-io', name: 'commons-io', version: '2.6'
要在我的服务中使用MockMultipartFile,请执行以下操作:

import org.springframework.mock.web.MockMultipartFile;
...
...
MultipartFile multipartFile = new MockMultipartFile("file",
        file.getName(), "image/jpeg", IOUtils.toByteArray(input));
但现在在测试类中,我有一个错误:

这是我的身材。格雷德尔:

依赖关系{

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
    
compile 'org.hibernate:hibernate-core:5.2.11.Final'
compile 'org.hibernate:hibernate-entitymanager:5.2.11.Final'
compile 'mysql:mysql-connector-java'
compile 'org.apache.commons:commons-dbcp2:2.0.1'


compile 'javax.xml.bind:jaxb-api:2.3.1'
compile 'com.sun.xml.bind:jaxb-core:2.3.0'
compile 'com.sun.xml.bind:jaxb-impl:2.3.1'
compile 'javax.activation:activation:1.1.1'

compileOnly('org.projectlombok:lombok')
annotationProcessor 'org.projectlombok:lombok'

testCompile 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
testCompile('org.mockito:mockito-junit-jupiter:2.23.0')

compile 'org.modelmapper:modelmapper:2.3.0'

compile 'com.google.firebase:firebase-admin:6.13.0'
compile group: 'org.springframework', name: 'spring-test', version: '3.1.1.RELEASE'
implementation group: 'commons-io', name: 'commons-io', version: '2.6'

}

您使用的是非常旧的Spring测试版本:

compile group: 'org.springframework', name: 'spring-test', version: '3.1.1.RELEASE'
我假设SpringTest3.1.1还没有JUnitJupiter
SpringExtension
。将
spring-test
更新到
build.gradle
或将其从
build.gradle
中删除,并让spring使用
spring-test
的依赖版本启动

因此,更新的
依赖项部分可能如下所示:

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'mysql:mysql-connector-java'

// this is your testing swiss-army knife - take a look at its transitive dependency
testImplementation 'org.springframework.boot:spring-boot-starter-test'

implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
    
compile 'org.hibernate:hibernate-core:5.2.11.Final'
compile 'org.hibernate:hibernate-entitymanager:5.2.11.Final'
compile 'mysql:mysql-connector-java'
compile 'org.apache.commons:commons-dbcp2:2.0.1'


compile 'javax.xml.bind:jaxb-api:2.3.1'
compile 'com.sun.xml.bind:jaxb-core:2.3.0'
compile 'com.sun.xml.bind:jaxb-impl:2.3.1'
compile 'javax.activation:activation:1.1.1'

compileOnly('org.projectlombok:lombok')
annotationProcessor 'org.projectlombok:lombok'

// REMOVE (managed by Spring Boot Starter Test) testCompile 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
// REMOVE (managed by Spring Boot Starter Test) testCompile('org.mockito:mockito-junit-jupiter:2.23.0')

compile 'org.modelmapper:modelmapper:2.3.0'

compile 'com.google.firebase:firebase-admin:6.13.0'
// REMOVE compile group: 'org.springframework', name: 'spring-test', version: '3.1.1.RELEASE'
implementation group: 'commons-io', name: 'commons-io', version: '2.6'

不知道这是否有任何影响,但您添加了spring测试作为编译的依赖项,通常应该是testCompile或testImplementation我尝试使用testCompile或testImplementation编译spring测试,但问题仍然存在。感谢rieckpil的回复!我做了你推荐的更改。我在测试类中解决了这个问题,但现在我不再有这个导入:
import org.springframework.mock.web.MockMultipartFile我不能使用:
MultipartFile MultipartFile=new MockMultipartFile(“file”,file.getName(),“image/jpeg”,IOUtils.toByteArray(input))我找到了rieckpil的解决方案。我听从了你的建议,然后我在我的一个包中实现了MockMultipartFile类,这多亏了它,消除了import.perfect-现在它可以正常工作了!