package by.bsuir.mpp.computershop.general.service;

import by.bsuir.mpp.computershop.entity.BaseEntity;
import by.bsuir.mpp.computershop.service.CrudService;
import by.bsuir.mpp.computershop.utils.entity.supplier.EntitySupplier;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import org.springframework.data.repository.CrudRepository;

import java.io.Serializable;

import static org.mockito.Mockito.*;

public abstract class CrudServiceTest<E extends BaseEntity<ID>, ID extends Serializable> {


    @Before
    public void initTests() {
        MockitoAnnotations.initMocks(this);
    }

    protected abstract CrudService<E, ID> getCrudService();

    protected abstract CrudRepository<E, ID> getCrudRepository();

    protected abstract EntitySupplier<E, ID> getEntitySupplier();

    @Test
    public void findExistingEntityTest() throws Exception {
        E expectedEntity = getEntitySupplier().getValidEntityWithId();
        ID id = expectedEntity.getId();
        when(getCrudRepository().findOne(id)).thenReturn(expectedEntity);

        E actualResult = getCrudService().getOne(id);

        verify(getCrudRepository(), times(1)).findOne(id);
        Assert.assertEquals(expectedEntity, actualResult);
    }

}

@@ -151,16 +151,92 @@
 ervice;%0A
+import by.bsuir.mpp.computershop.service.exception.EntityNotFoundException;%0A
 import b
@@ -365,20 +365,85 @@
 g.junit.
-Test
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.ExpectedException
 ;%0Aimport
@@ -695,16 +695,90 @@
 ble%3E %7B%0A%0A
+    @Rule%0A    public ExpectedException thrown = ExpectedException.none();%0A
 %0A    @Be
@@ -1494,10 +1494,293 @@
 %0A    %7D%0A%0A
+%0A    @Test(expected = EntityNotFoundException.class)%0A    public void findNotExistingEntityTest() throws Exception %7B%0A        ID id = getEntitySupplier().getAnyId();%0A        when(getCrudRepository().findOne(id)).thenReturn(null);%0A        E entity = getCrudService().getOne(id);%0A    %7D%0A%0A
 %7D%0A

Add findNotExistingEntityTest