java - java.lang.UnsupportedOperationException:删除
问题描述
我正在用 spring boot 构建一个 ATM,并且我试图在一个模拟存储库中包含一些测试。但是当我尝试使用我的一种测试方法时,我得到了“java.lang.UnsupportedOperationException:remove”错误。我是新手,所以我真的不明白为什么会这样。
不起作用的是withdrawOneThousand() 测试方法。getAllBills() 测试方法工作正常。
在我在测试类中创建一个模拟存储库之前,我直接在数据库上尝试了测试方法,它工作得很好。
我的控制器:
@RestController
@RequestMapping("/bills")
public class BillController {
private int calculate;
@Autowired
private BillRepository billRepository;
@GetMapping(path = "/withdraw2/{value}")
public String withdraw2(@PathVariable int value){
List<Bill> billsToWithdraw = new ArrayList<>();
List<Bill> dbCopy = (List<Bill>) billRepository.findAll();
calculate = value;
System.out.println("calc start " + calculate);
try {
while (calculate != 0) {
if (calculate >= 1000 && searchBillList(1000, dbCopy)) {
billTransfer(1000, billsToWithdraw, dbCopy);
} else if (calculate >= 500 && searchBillList(500, dbCopy)) {
billTransfer(500, billsToWithdraw, dbCopy);
} else if (calculate >= 100 && searchBillList(100, dbCopy)) {
billTransfer(100, billsToWithdraw, dbCopy);
}
else {
return "No bills left";
}
}
deleteFromDB(billsToWithdraw);
System.out.println("returning");
return "Bills: " + billsToWithdraw;
} catch (NullPointerException e) {
System.out.println(e);
return "Not enough bills";
} catch (Exception e){
return e.toString();
}
}
public void deleteFromDB(List<Bill> billsToWithdraw){
billsToWithdraw.forEach((bill) -> billRepository.delete(bill));
}
public Boolean searchBillList(int valueToCheck, List<Bill> tempList) {
return tempList.stream().anyMatch((bill -> bill.getValue() == valueToCheck));
}
public void billTransfer(int value, List<Bill> billsToWithdraw, List<Bill> tempList){
Bill bill = tempList.stream().filter((currentBill) -> currentBill.getValue() == value).findFirst().get();
calculate = calculate - bill.getValue();
billsToWithdraw.add(bill);
tempList.remove(bill);
}
我的存储库:
public interface BillRepository extends CrudRepository<Bill, Long> {
Iterable<Bill> findByValue(int value);
}
我的测试:
@SpringBootTest
@AutoConfigureMockMvc
public class BillControllerTest2 {
@Autowired
private MockMvc mvc;
@MockBean
private BillRepository mockRepo;
@BeforeEach
public void init(){
Bill b1 = new Bill(1000, 1L);
Bill b2 = new Bill(500, 2L);
Bill b3 = new Bill(100, 3L);
when(mockRepo.findByValue(1000)).thenReturn(List.of(b1));
when(mockRepo.findByValue(500)).thenReturn(Arrays.asList(b2));
when(mockRepo.findByValue(100)).thenReturn(Arrays.asList(b3));
when(mockRepo.findAll()).thenReturn(Arrays.asList(b1, b2, b3));
}
@Test
public void withdrawOneThousand() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/bills/withdraw2/1000").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string("Bills: [Bill{id=1, number=1000}]"));
}
@Test
public void getAllBills() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/bills/all").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().json("[{\"id\": 1, \"value\": 1000}, " +
"{\"id\": 2, \"value\": 500 }, " +
"{\"id\": 3, \"value\": 100}]"));
}
我得到的错误:
java.lang.AssertionError: Response content expected:<Bills: [Bill{id=1, number=1000}]> but was:<java.lang.UnsupportedOperationException: remove>
Expected :Bills: [Bill{id=1, number=1000}]
Actual :java.lang.UnsupportedOperationException: remove
<Click to see difference>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
at org.springframework.test.web.servlet.result.ContentResultMatchers.lambda$string$4(ContentResultMatchers.java:136)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:196)
at com.example.demo.controllers.BillControllerTest2.withdrawOneThousand(BillControllerTest2.java:51)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:210)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
解决方案
List.of(b1)
您在模拟中返回的返回不可修改的列表。顺便说一句Arrays.asList
,也不支持删除。如果您需要从集合中移除,您应该将模拟的返回更改为类似的内容
new ArrayList<>(List.of(b1));
new ArrayList<>(Arrays.asList(b2));
所以完整的代码应该是这样的:
when(mockRepo.findByValue(1000)).thenReturn(new ArrayList<>(List.of(b1)));
when(mockRepo.findByValue(500)).thenReturn(new ArrayList<>(Arrays.asList(b2)));
when(mockRepo.findByValue(100)).thenReturn(new ArrayList<>(Arrays.asList(b3)));
when(mockRepo.findAll()).thenReturn(new ArrayList<>(Arrays.asList(b1, b2, b3)));
推荐阅读
- swift - SwiftUI - 结构绑定未按预期更新 UI
- printf - 在 Julia 中重复格式字符串的正确方法是什么?
- javascript - 连接到数据库时从不调用 React Infinite Render 或 Handler
- powershell - powershell - 按列将csv拆分为多个csv
- ruby-on-rails - 如何找到()传递给 sidekiq 工作人员的资源?
- webots - Webbots 模拟:Mobsya 的 Thymio2 笔不写字
- ios - 如何在一定带宽内围绕中心频率发射噪声
- java - 如何找出哪个元素导致 Hibernate/Spring Data 中的事务错误?
- java - 如何从 Webview 的页面内部获取 json 响应(我得到用户代理的响应)?
- python - Django获取登录用户的用户名并将其传递给模型