All Downloads are FREE. Search and download functionalities are using the official Maven repository.

driven-adapter.jpa-repository.helper.adapter-operations.unit.test.mustache Maven / Gradle / Ivy

Go to download

Gradle plugin to create a clean application in Java that already works, It follows our best practices!

There is a newer version: 3.20.10
Show newest version
package {{package}}.jpa.helper;

import {{package}}.jpa.JPARepository;
import {{package}}.jpa.JPARepositoryAdapter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.reactivecommons.utils.ObjectMapper;
import org.springframework.data.domain.Example;

import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

class AdapterOperationsTest {

    @Mock
    private JPARepository repository;

    @Mock
    private ObjectMapper objectMapper;

    private JPARepositoryAdapter adapter;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);

        when(objectMapper.map("value", Object.class)).thenReturn("value");

        adapter = new JPARepositoryAdapter(repository, objectMapper);
    }

    @Test
    void testSave() {

        Object objectValue = "value";

        when(repository.save(objectValue)).thenReturn(objectValue);

        Object result = adapter.save(objectValue);

        assertEquals(result, objectValue);
    }

    @Test
    void testSaveAllEntities() {

        List objectValues = List.of("value", "value");

        when(repository.saveAll(objectValues)).thenReturn(objectValues);

        Object result = adapter.saveAllEntities(objectValues);

        assertEquals(result, objectValues);
    }

    @Test
    void testFindById() {

        Object objectValue = "value";

        when(repository.findById("id")).thenReturn(Optional.of(objectValue));

        Object result = adapter.findById("id");

        assertEquals(result, objectValue);
    }

    @Test
    void testFindAll() {

        List objectValues = List.of("value", "value");

        when(repository.findAll()).thenReturn(objectValues);

        Object result = adapter.findAll();

        assertEquals(result, objectValues);
    }

    @Test
    void testFindByExample() {

        Object objectValue = "value";
        List objectValues = List.of(objectValue, objectValue);

        when(repository.findAll(any(Example.class))).thenReturn(objectValues);

        Object result = adapter.findByExample(objectValue);

        assertEquals(result, objectValues);
    }
}