简体   繁体   中英

Spring Boot Java Cannot Mock JPARepository for Unit Test

So I am struggling to create a mock of my JPARepo for a unit test still new to spring even newer to testing it.

The error I am getting is > java.lang.NullPointerException: Cannot invoke "net.codejava.data.AdminRepository.findAll()" because "this.adminRepository" is null

I am confused because based off the example I was following I should be mocking the repository correctly.

My test is below.

@RunWith(MockitoJUnitRunner.class)
class AdminServiceTest {

    @InjectMocks
    AdminService adminService;
    
    @Mock
    private AdminRepository adminRepository;
    
    @Test
    void getAllTest() {
        List<Admin> list  = new ArrayList<Admin>();
        Admin admin = new Admin();
        
        list.add(admin);
        
        when(adminRepository.findAll()).thenReturn(list);
        
        List<Admin> adminList = adminService.getAllAdmins();
        
        assertEquals(1, adminList.size());
        
        verify(adminRepository, times(1)).findAll();
    }

}

This is my JpaRepo

@Repository
public interface AdminRepository extends CrudRepository<Admin, String> {
    public List<Admin> findByusername(String admin);

    public void deleteById(long l);

    public List<Admin> findById(long l);
}

This is my service class

@Service
public class AdminService {
    
    @Autowired
    private AdminRepository adminRepository;
    
    public List<Admin> getAllAdmins() {
        List<Admin> admins = new ArrayList<>();
        adminRepository.findAll().forEach(admins::add);
        return admins;
    }
    
    public List<Admin> getAllAdminsByUserName(String username) {
        List<Admin> admins = new ArrayList<>();
        adminRepository.findByusername(username).forEach(admins::add);
        return admins;
    }

    public List<Admin> getAdmins(long l) {
        return adminRepository.findById(l);
    }
    
    public void updateAdmin(long l, Admin admin) {
        adminRepository.save(admin);
    }

    public void deleteAdmin(long l, Admin admin) {
        adminRepository.deleteById(l);
    }

    public void addAdmin(Admin admin) {
        adminRepository.save(admin);
    } 
    
}

I am not to sure why it isn't injecting my mock of the Jpa Repo correctly. Its a noob question but I am really not sure.

The problem is that you no only test the repository but also the service.

This should work:

@SpringBootTest
class AdminServiceTest {

    @Autowired
    AdminService adminService;
    
    @MockBean
    private AdminRepository adminRepository;
    
    @Test
    void getAllTest() {
        List<Admin> list  = new ArrayList<Admin>();
        Admin admin = new Admin();
        
        list.add(admin);
        
        when(adminRepository.findAll()).thenReturn(list);
        
        List<Admin> adminList = adminService.getAllAdmins();
        
        assertEquals(1, adminList.size());
        
        verify(adminRepository, times(1)).findAll();
    }

}

Please also read https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.testing.spring-boot-applications.mocking-beans

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM