Amish Raj
Amish Raj

Reputation: 43

How to Mock a Service Class in Spring Boot to be used in the testing of another Service Class? JUNIT 5

I have two services ServiceOne.class and ServiceTwo.class. I am trying to mock the ServiceTwo, but no solution online works and in the Test of ServiceOne.class, it acts as if it is not getting any result, despite clearly mentioning it.

I am aware there are many similar solutions to this, but none of the solutions worked for me.

methodTest is the method inside my ServiceOne.class that I am supposed to test.


@SpringBootTest
class ServiceOneTest {

    @MockBean
    private Repo1 repo1;
    @MockBean
    private Repo2 repo2;
    @MockBean
    private Repo3 repo3;
    @MockBean
    private Repo4 repo4;
    @MockBean
    private Repo5 repo5;
    @MockBean
    private Repo6 repo6;
    @MockBean
    private Repo7 repo7;
    @MockBean
    private Repo8 repo8;
    @MockBean
    private Repo9 repo9;
    @MockBean
    private Repo10 repo10;
    @MockBean
    private Repo11 repo11;

    @MockBean
     private ServiceTwo serviceTwo;

    @Autowired
    private ServiceOne serviceOne;

@BeforeEach
    void setUp() throws Exception { 
//static objs1-11 and serviceObj to be returned as mock data here
}

@AfterEach
    void tearDown() throws Exception {
}

@Test
    void methodTest() throws JsonProcessingException {
   when(repo1.findAll()).thenReturn(obj1);
   when(repo2.findAll()).thenReturn(obj2);
   //for other repo3...repo11
   when(repo11.findAll()).thenReturn(obj11);
   
  when(serviceTwo.getObj(params).thenReturn(serviceObj);

  String result= serviceOne.method();
  assertEquals(expectedResult, result);
}

the serviceObj is not returned and hence the snippet throws an error and does not reach the assert statement.

Upvotes: 4

Views: 6113

Answers (1)

chaitanya guruprasad
chaitanya guruprasad

Reputation: 691

I have two service classes: ServiceA and ServiceB

ServiceA has a getInteger() which always returns 5 ServiceB has a getLong() which calls the getInteger() on ServiceA and returns the long value.

ServiceA

@Service
public class ServiceA {

    public Integer getInteger() {
        return 5;
    }

}

ServiceB

@Service
public class ServiceB {

    @Autowired
    ServiceA serviceA;

    public Long getLong() {
        return serviceA.getInteger().longValue();
    }

}

Below are my unit tests:

ServiceATest

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
class ServiceATest {


    @Autowired
    ServiceA serviceA;

    @Test
    void getInteger() {

        assertThat(serviceA.getInteger()).isEqualTo(5);
    }
}

ServiceBTest

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

@SpringBootTest
class ServiceBTest {

    @Autowired
    ServiceB serviceB;

    @MockBean
    ServiceA serviceA;



    @Test
    void getLong() {

        when(serviceA.getInteger()).thenReturn(7);
        assertThat(serviceB.getLong()).isEqualTo(7L);

    }
}

Upvotes: 3

Related Questions