When I run this in test, NullPointerException occurs in the following line : Account acc = accRepo.findAccountById(Long id); As if the findAccountById(Long id) returns null. But when I call this method directly inside any other test method it works fine.
My Repository file
public Interface AccountRepo extends JpaRepository<Account,Long>{
@Query("select a from Account a where a.id = ?1")
public List<Account> findAccountById(Long id);
}
Service Interface and Implementation
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();
}