gpt4 book ai didi

java - Mockito NoSuchElementException when() findById() get() thenReturn()

转载 作者:行者123 更新时间:2023-12-02 09:22:35 37 4
gpt4 key购买 nike

尝试使用 Mockito 库测试我的服务,但遇到了问题,请参阅下面的错误:

java.util.NoSuchElementException: No value present

at java.util.Optional.get(Optional.java:135)
at org.HappyRestApi.springREST.services.BankDepositServiceTests.findBankDepositById(BankDepositServiceTests.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

我尝试使用堆栈上的类似文章来解决此问题,链接如下: Mockito - NullpointerException when stubbing Method

请查看我涉及到的代码

存储库:


import org.HappyRestApi.springREST.domain.BankDeposit;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BankDepositRepositorie extends JpaRepository<BankDeposit,Long> {
}

服务:

package org.HappyRestApi.springREST.services;

import org.HappyRestApi.springREST.domain.BankDeposit;
import org.HappyRestApi.springREST.repositories.BankDepositRepositorie;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BankDepositServiceImpl implements BankDepositService {

private final BankDepositRepositorie bankDepositRepositorie;

public BankDepositServiceImpl(BankDepositRepositorie bankDepositRepositorie) {
this.bankDepositRepositorie = bankDepositRepositorie;
}

@Override
public BankDeposit findBankDepositById(Long id) {
return bankDepositRepositorie.findById(id).get();
}

@Override
public List<BankDeposit> findAllBankDeposit() {
return bankDepositRepositorie.findAll();
}

@Override
public void addBankDeposit(BankDeposit bankDeposit) {
this.bankDepositRepositorie.save(bankDeposit);
}

@Override
public void deleteBankDepositaById(Long id){
this.bankDepositRepositorie.deleteById(id);
}
}

测试:

package org.HappyRestApi.springREST.services;

import org.HappyRestApi.springREST.domain.BankDeposit;
import org.HappyRestApi.springREST.domain.enums.Period;
import org.HappyRestApi.springREST.repositories.BankDepositRepositorie;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;

import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@AutoConfigureMockMvc
public class BankDepositServiceTests {

private BankDepositServiceImpl bankDepositService;
private BankDeposit b1;

@Mock
BankDepositRepositorie bankDepositRepositorie;

@BeforeEach
public void init() throws Exception{
bankDepositService = new BankDepositServiceImpl(bankDepositRepositorie);

b1 = new BankDeposit();
b1.setId(1L);
b1.setName("jakaslokata1");
b1.setInterest(0.1);
b1.setCapitalizationPeriod(Period.TWELVE_MONTHS);
b1.setPeriodOfValidityStart(new Date());
b1.setPeriodOfValidityEnd(new Date());
}

@Test
public void findBankDepositById() throws Exception {

when(bankDepositRepositorie.findById(anyLong()).get()).thenReturn(b1);

BankDeposit result = bankDepositService.findBankDepositById(1L);

Assert.assertNotNull(b1);
Assert.assertEquals("jakaslokata1", b1.getName());

}
}

问题似乎与测试类中的这行代码有关:

when(bankDepositRepositorie.findById(anyLong()).get()).thenReturn(b1);

But i'm not really sure, does anyone have any suggestions?

最佳答案

方法findById返回一个可选的,所以你可以尝试:

when(bankDepositRepositorie.findById(anyLong())).thenReturn(Optional.of(b1));

似乎 init() 方法没有运行,正如 @LukeWoodward 提到的,Junit4 和 Junit5 之间不匹配。将 @BeforeEach 替换为 @Before

关于java - Mockito NoSuchElementException when() findById() get() thenReturn(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58580750/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com