Java 无法在SpringBoot-Junit5-Mockito中模拟RestTemplate
我试图在我的DAO类中模拟rest模板,但Mockito抛出了一个奇怪的错误,说它无法模拟 试图涵盖我的Spring boot应用程序2.x版的单元测试用例。我几乎已经在互联网上尝试了所有可能的解决方案,比如更新JDK/JRE进行编译,但我遇到了以下错误:Java 无法在SpringBoot-Junit5-Mockito中模拟RestTemplate,java,spring-boot,mockito,resttemplate,junit5,Java,Spring Boot,Mockito,Resttemplate,Junit5,我试图在我的DAO类中模拟rest模板,但Mockito抛出了一个奇怪的错误,说它无法模拟 试图涵盖我的Spring boot应用程序2.x版的单元测试用例。我几乎已经在互联网上尝试了所有可能的解决方案,比如更新JDK/JRE进行编译,但我遇到了以下错误: org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: class org.springframework.web.client.RestT
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class org.springframework.web.client.RestTemplate.
Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
Java : 1.8
JVM vendor name : Oracle Corporation
JVM vendor version : 25.181-b13
JVM name : Java HotSpot(TM) 64-Bit Server VM
JVM version : 1.8.0_181-b13
JVM info : mixed mode
OS name : Windows 10
OS version : 10.0
Underlying exception : java.lang.IllegalArgumentException: Could not create type
at org.mockito.junit.jupiter.MockitoExtension.beforeEach(MockitoExtension.java:115)
....
以下是我的代码:
格雷德尔先生
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.retry:spring-retry'
implementation 'org.aspectj:aspectjrt'
implementation 'org.aspectj:aspectjweaver'
implementation 'org.springframework:spring-aop'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.2.0'
testCompile 'org.junit.jupiter:junit-jupiter-params:5.2.0'
testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.2.0'
testImplementation 'org.mockito:mockito-core:2.+'
testImplementation 'org.mockito:mockito-junit-jupiter:2.18.3'
}
test {
testLogging.showStandardStreams = true
useJUnitPlatform()
}
MyDao.java
@Repository
public class MyDao {
@Value("${app.prop.service.url}")
private String url;
@Autowired
public RestTemplate restTemplate;
public String getSignals() {
System.out.println("url -----------------------> " + url);
return new RetryTemplate().execute(context -> {
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
if (response.getStatusCodeValue() == 200) {
System.out.println("Server response -> " + response.getBody());
return response.getBody();
} else {
throw new RuntimeException("server response status: " + response.getStatusCode());
}
}, context -> {
System.out.println("retry count: " + context.getRetryCount());
System.err.println("error -> " + context.getLastThrowable());
return null;
});
}
}
任何建议都会很有帮助
注意:应用程序通过gradle命令运行得非常好
gradlew bootRun
问题只在于单元测试
gradlew test
所述(或从属)问题的一个原因可能是调用/模拟
restemplate.getForEntity()
。。。在中,此方法有三种类型/具有重载参数:
。。。getForEntity(字符串url、类响应类型、对象…变量).
。。。getForEntity(字符串url、类响应类型、映射变量).
。。。getForEntity(URI url、类响应类型)….
...restTemplate.getForEntity(Mockito.anyString(), String.class
/*!!!*/, ArgumentMatchers.<Object>any());
不是:
…如所示。
如果这(和其他教程的结果)没有帮助,考虑到:
向邮件列表(.)报告(it)
我(有生以来)第一次听到/读到: 在我的DAO类中模拟rest模板
考虑将dao命名为“服务”(+相应的步骤/重构;)好的,我解决了这个问题。这是mockito内核和mockito junit jupiter jars之间的版本不匹配,导致了thr问题 正确的依赖关系是:
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.2.0'
testCompile 'org.junit.jupiter:junit-jupiter-params:5.2.0'
testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.2.0'
testImplementation 'org.mockito:mockito-core:2.18.3'
testImplementation 'org.mockito:mockito-junit-jupiter:2.18.3'
早些时候是这样的
testImplementation 'org.mockito:mockito-core:2.+'
Gradle选择了mockito core jar的最新版本,因为它被要求选择构建文件中定义的2.x系列中的任何版本。我相信其余的都是不言自明的
快乐,单元测试!:P
这个问题发生在我身上,因为我无意中运行了Java11。一旦我切换到项目的标准Java8,这个问题就消失了
testCompile "org.mockito:mockito-core:2.+"
testCompile('org.mockito:mockito-junit-jupiter:2.18.3')
...
testImplementation 'org.mockito:mockito-core:2.+'
testImplementation 'org.mockito:mockito-junit-jupiter:2.18.3'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.2.0'
testCompile 'org.junit.jupiter:junit-jupiter-params:5.2.0'
testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.2.0'
testImplementation 'org.mockito:mockito-core:2.18.3'
testImplementation 'org.mockito:mockito-junit-jupiter:2.18.3'
testImplementation 'org.mockito:mockito-core:2.+'