如何为以下代码编写 JUnit 5 测试用例?
How to write JUnit 5 test cases for the following code?
这是为
编写JUnit测试用例的方法
public List<LoanApiCallEntity> getAllApiCallDetails() {
return apiCallDetailsRepository.findAll();
}
在 LoanApiCallEntity 内部 class 这些是:
public class LoanApiCallEntity {
@Id
@Column(name = "loan_api_call_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int apiCallId;
@CreationTimestamp
@Column(name="call_timestamp", nullable=false, updatable=false)
private Timestamp callDateTime;
@Column(name = "request_url")
private String requestURL;
@Column(name="request_parameters")
private String requestParameters;
@Column(name = "username")
private String username;
@Column(name = "response_status")
private String responseStatus;
}
这是我试过的测试用例,但是显示错误:
@MockBean ApiCallDetailsServiceImpl apiCallDetailsService;
@Test public void apiCallDetailsServiceImpltest() {
LoanApiCallEntity entity = new LoanApiCallEntity();
entity.setApiCallId(1);
Mockito.doReturn(entity).when(this.apiCallDetailsService).getAllApiCallDetails();
Assertions.assertEquals(entity.getApiCallId(),1);
}
运行 项目 (mvn test
) 时显示的错误是 - getAllApiCallDetails() 无法返回 LoanApiCallEntity。
getAllApiCallDetails() returns 一个对象列表,因此您需要在 doReturn 函数调用中提供一个列表作为模拟值。
这是为
编写JUnit测试用例的方法public List<LoanApiCallEntity> getAllApiCallDetails() {
return apiCallDetailsRepository.findAll();
}
在 LoanApiCallEntity 内部 class 这些是:
public class LoanApiCallEntity {
@Id
@Column(name = "loan_api_call_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int apiCallId;
@CreationTimestamp
@Column(name="call_timestamp", nullable=false, updatable=false)
private Timestamp callDateTime;
@Column(name = "request_url")
private String requestURL;
@Column(name="request_parameters")
private String requestParameters;
@Column(name = "username")
private String username;
@Column(name = "response_status")
private String responseStatus;
}
这是我试过的测试用例,但是显示错误:
@MockBean ApiCallDetailsServiceImpl apiCallDetailsService;
@Test public void apiCallDetailsServiceImpltest() {
LoanApiCallEntity entity = new LoanApiCallEntity();
entity.setApiCallId(1);
Mockito.doReturn(entity).when(this.apiCallDetailsService).getAllApiCallDetails();
Assertions.assertEquals(entity.getApiCallId(),1);
}
运行 项目 (mvn test
) 时显示的错误是 - getAllApiCallDetails() 无法返回 LoanApiCallEntity。
getAllApiCallDetails() returns 一个对象列表,因此您需要在 doReturn 函数调用中提供一个列表作为模拟值。