Mock unit test a Service implementation that calls a Stored Procedure in Java

Viewed 61

I’m trying to unit test a Service implementation that calls a Stored Procedure in Java. My idea is to Mock or Stub the service just to see if all the calls are made correctly. Not quite sure how to do this.

I’m using Springframework for the Service.

Any ideas on how to easily do this ? I'm getting null of course for the @Autowired private PerformanceService performanceService;

which doesn't initially happen, anyway I just want to Mock that and just make sure the calls are happening, thanks.

import com.integration.as400.entity.Performance;
import com.integration.as400.service.PerformanceService;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.BDDMockito.mock;
import static org.mockito.Mockito.when;


@Configuration
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes={PerformanceService.class, TestConfiguration.class})
@ActiveProfiles("test")
public class PerformanceServiceImplMockTest {

    private static final Logger logger = LoggerFactory.getLogger(PerformanceServiceImplMockTest.class);

    @Autowired
    private PerformanceService performanceService;

    @Before
    public void setupMock(){
        MockitoAnnotations.initMocks(this);
        performanceService = mock(PerformanceService.class);
    }



    @Test
    public void shouldReturnListPerformance_whenGetListPerformanceIsCalled() throws Exception{
        List<Performance> performances = new ArrayList<>();
        performances.add(new Performance("0000184","00001","MWR","SI",LocalDate.parse("2016-01-01"),LocalDate.parse("2016-01-31"),"CAD",48585.63,34821.01,47501.47,-94372.00,11184.70,10.000322,"",33105.91));
        performances.add(new Performance("0000184","00001","MWR","SI",LocalDate.parse("2016-01-01"),LocalDate.parse("2016-01-31"),"CAD",142743.11,260376.38,41688.49,-4886.00,11184.70,35937.14,"",7.475078));

        logger.info("Stubbing getListPerformance(int pageNumber, String loadStartDate, String loadEndDate) to return " + performances);
        // Arrange
        when(performanceService.getListPerformance(2, "2021-08-01", "2021-08-31")).thenReturn(performances);
        // Act
        List<Performance> retrievedPerformances = performanceService.getListPerformance(2, "2021-08-01", "2021-08-31");
        // Assert
        assertThat(retrievedPerformances, is(equalTo(performances)));
    }


}
0 Answers
Related