尝试使用Mockito库测试我的服务,但遇到了问题,请查看以下错误:
<DOCTYPE! html>
<html>
<head>
<title>Survey Form</title>
<link href="surveyform.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="yellow">
<h1>Survey Form</h1>
<div class="grey">
<center>
Let us know how we can improve freecode camp
</center>
<br>
<form>
<div class="row">
<div class="left">
<label>
Which option best describes your current role
</label>
</div>
<div class="right">
<select>
<option>student</option>
<option>engineer</option>
</select>
</div>
</div>
<div class="row">
<div class="left">
<label>
Things that should be improved in future
</label>
</div>
<div class="right">
<div class="input-group">
<input type="checkbox" name="a">
<span> Front end projects</span>
</div>
<div class="input-group">
<input type="checkbox" name="a">
<span> Backend Projects</span>
</div>
<div class="input-group">
<input type="checkbox" name="a">
<span> Structure </span>
</div>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
我尝试通过在堆栈上使用类似的文章来解决此问题,该链接为: Mockito - NullpointerException when stubbing Method
请查看我涉及的代码
存储库:
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)
服务:
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());
}
}
答案 0 :(得分:1)
方法findById返回一个Optional,所以您可以尝试:
when(bankDepositRepositorie.findById(anyLong())).thenReturn(Optional.of(b1));
似乎init()方法未在运行,因为@LukeWoodward提到Junit4和Junit5之间不匹配。将@BeforeEach
替换为@Before
。