【发布时间】:2021-05-24 10:27:18
【问题描述】:
当我在测试中运行它时,NullPointerException 发生在以下行: Account acc = accRepo.findAccountById(Long id); 好像 findAccountById(Long id) 返回 null。但是当我直接在任何其他测试方法中调用此方法时,它工作正常。
我的存储库文件
public Interface AccountRepo extends JpaRepository<Account,Long>{
@Query("select a from Account a where a.id = ?1")
public List<Account> findAccountById(Long id);
}
服务接口和实现
public Interface AccountService{
Account showAcc();
}
@Service
public class AccServiceImpl implements AccountService {
@Autowired
private AccountRepo accRepo;
@Override
Account showAcc(){
Long id = 10L;
Account acc = accRepo.findAccountById(id); // Null Pointer exception at this line
String name = acc.getName();
System.out.println(name);
}
}
@Autowired
AccountServiceimpl accServImpl;
@Test
public void testAccount(){
accServImpl.showAcc();
}
【问题讨论】:
标签: java spring-data-jpa spring-test spring-repositories