Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
4.6 kB
3
Indexable
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.List;

public class YourServiceTest {

    @Mock
    private CacheConfigService cacheConfigService;

    @InjectMocks
    private YourService yourService;

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

    @Test
    void testGetInsuranceProducts_Success() throws ApplicationException {
        // Setup
        List<CMSInsuranceProduct> mockList = new ArrayList<>();
        mockList.add(new CMSInsuranceProduct("prod1", "CIF1"));
        when(cacheConfigService.getInsuranceProducts(anyString(), anyString())).thenReturn(mockList);

        // Execute
        List<CMSInsuranceProduct> result = yourService.getInsuranceProducts();

        // Verify
        assertNotNull(result);
        assertEquals(1, result.size());
        assertEquals("prod1", result.get(0).getProductId());
        verify(cacheConfigService).getInsuranceProducts(anyString(), anyString());
    }

    @Test
    void testGetInsuranceProducts_NoProducts() throws ApplicationException {
        // Setup
        when(cacheConfigService.getInsuranceProducts(anyString(), anyString())).thenReturn(new ArrayList<>());

        // Execute & Verify
        assertThrows(ApplicationException.class, () -> yourService.getInsuranceProducts());
    }
}





import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.HashSet;
import java.util.Set;

public class YourServiceTest {

    @Mock
    private ProductMasterService productMasterService;

    @InjectMocks
    private YourService yourService;

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

    @Test
    void testGetActiveInsuranceProducts() throws ApplicationException {
        // Setup
        ProductMasterResponse response1 = new ProductMasterResponse();
        response1.setCurrentPage(1);
        response1.setLastPage(2);
        response1.setData(List.of(new ProductData("prod1", "ACTIVE"), new ProductData("prod2", "INACTIVE")));

        ProductMasterResponse response2 = new ProductMasterResponse();
        response2.setCurrentPage(2);
        response2.setLastPage(2);
        response2.setData(List.of(new ProductData("prod3", "ACTIVE")));

        when(productMasterService.getInsuranceProductMasterDetails(0, 0)).thenReturn(response1);
        when(productMasterService.getInsuranceProductMasterDetails(1, 0)).thenReturn(response2);

        // Execute
        List<String> result = yourService.getActiveInsuranceProducts();

        // Verify
        assertNotNull(result);
        assertEquals(Set.of("prod1", "prod3"), new HashSet<>(result));
        verify(productMasterService, times(2)).getInsuranceProductMasterDetails(anyInt(), anyInt());
    }
}






import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.List;

public class YourServiceTest {

    @Mock
    private YourService yourService;

    @InjectMocks
    private YourServiceImpl yourServiceImpl;

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

    @Test
    void testGetFilteredInsuranceProducts() throws ApplicationException {
        // Setup
        List<CMSInsuranceProduct> cmsInsProdList = List.of(
            new CMSInsuranceProduct("prod1", "CIF1"),
            new CMSInsuranceProduct("prod2", "CIF2")
        );

        when(yourService.getActiveInsuranceProducts()).thenReturn(List.of("prod1"));

        // Execute
        List<CMSInsuranceProduct> result = yourServiceImpl.getFilteredInsuranceProducts(cmsInsProdList);

        // Verify
        assertNotNull(result);
        assertEquals(1, result.size());
        assertEquals("prod1", result.get(0).getProductId());
    }
}
Leave a Comment