Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring test service class mocking utility class- Junit and Mockito

I want to write test cases for service layer of spring framework using Junit + Mockito.

How to call the actual service layer method using my ServiceTest class, If i mock the ServiceTest class then it's object wont execute the actual service method code because it wont get the object to call it's methods and if I try with the Spy still it was not working, I tried this example still I not able to execute the test cases.

MyService.java

@Service
 public class MyService{

    @Autowired
    Utility utility;

    public String showResult(){

    String result = utility.getName();

    return result;
    }
    }

MyServiceTest.java

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(loader=AnnotationConfigWebContextLoader.class)
    @WebAppConfiguration
    public class MyServiceTest {

        @Autowired
        MyService myService;


        @Autowired
        Utility utility; 




        @Test
        public void testShowResult() throws Exception {

            assertEquals("Test",myService.showResult());

        }

        @Configuration
        static class MykServiceTestContextConfiguration {

            @Bean
            public MyService myService() {
                return new MyService();
            }           

            @Bean
            public Utility utility() {
                return Mockito.mock(Utility.class);
            }
        }

    }
like image 753
Bhabani Sankar Sahoo Avatar asked Oct 29 '25 19:10

Bhabani Sankar Sahoo


2 Answers

You have to first mock the Utility class and then have to invoke it before calling your @Test using MockitoAnnotations.initMocks(this) as follows:

MyServiceTest.java

import static org.mockito.Mockito.when;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.AnnotationConfigWebContextLoader;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigWebContextLoader.class)
@WebAppConfiguration
public class MyServiceTest {

    @InjectMocks
    private MyService myService;

    @Mock
    private Utility utility;

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

    @Test
    public void testShowResult() throws Exception {
        when(utility.getName()).thenReturn("Test");
        Assert.assertEquals("Test", myService.showResult());
    }

    @Configuration
    static class MykServiceTestContextConfiguration {

        @Bean
        public MyService myService() {
            return new MyService();
        }

        @Bean
        public Utility utility() {
            return new Utility();
        }
    }

}

MyService.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyService {

    @Autowired
    private Utility utility;

    public String showResult() {
        String result = utility.getName();
        return result;
    }
}

Utility.java

import org.springframework.stereotype.Component;

@Component
public class Utility {
    public String getName() {
        return "hello";
    }

}
like image 118
Arpit Aggarwal Avatar answered Oct 31 '25 10:10

Arpit Aggarwal


Make use of @Spy

When spy is called, then actual method of real object is called.

https://www.tutorialspoint.com/mockito/mockito_spying.htm

please go through the tutorial

This worked for me

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
public class MyServiceTest {

    @Spy
    MyService myService;  


    @Test
    public void testShowResult() throws Exception {

        assertEquals("Test",myService.showResult());

    }    

    @Service
    public class MyService{


        public String showResult(){  
            return "Test";
        }
    }

}

still having issues share the spring version you are using

like image 26
Barath Avatar answered Oct 31 '25 10:10

Barath