请看我涉及到的代码存储库: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;@Servicepublic 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; }
1 回答
炎炎设计
TA贡献1808条经验 获得超4个赞
findById方法返回一个Optional,所以你可以尝试:
when(bankDepositRepositorie.findById(anyLong())).thenReturn(Optional.of(b1));
似乎 init() 方法没有运行,Junit4 和 Junit5 之间不匹配。@BeforeEach
用。。。来代替@Before
。
添加回答
举报
0/150
提交
取消