При попытке протестировать мой сервис с библиотекой 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 при методе заглушки
Пожалуйста, смотрите мой код, который участвует
Репозиторий:
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?