有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring测试服务类模拟实用程序类Junit和Mockito

我想使用Junit + Mockito.为spring框架的服务层编写测试用例

如何使用我的ServiceTest类调用实际的服务层方法,如果我模拟ServiceTest类,那么它的对象不会执行实际的服务方法代码,因为它不会让对象调用它的方法,如果我尝试使用Spy,它仍然不工作,I tried this example 我仍然无法执行测试用例

我的服务。java

@Service
 public class MyService{

    @Autowired
    Utility utility;

    public String showResult(){

    String result = utility.getName();

    return result;
    }
    }

我的服务测试。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);
            }
        }

    }

共 (2) 个答案

  1. # 1 楼答案

    您必须首先模拟Utility类,然后在使用MockitoAnnotations.initMocks(this)调用@Test之前调用它,如下所示:

    我的服务测试。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();
            }
        }
    
    }
    

    我的服务。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;
        }
    }
    

    实用程序。java

    import org.springframework.stereotype.Component;
    
    @Component
    public class Utility {
        public String getName() {
            return "hello";
        }
    
    }
    
  2. # 2 楼答案

    使用@MockBean怎么样?它适合Spring+JUnit,并且可能需要实现模拟行为

    我猜Utility.getName()在测试用例中返回“Test”

    下面是我试过的测试代码

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(loader = AnnotationConfigWebContextLoader.class)
    @WebAppConfiguration
    public class MyServiceTest {
    
        @Autowired
        MyService myService;
    
        @MockBean
        Utility utility;
    
        @Test
        public void testShowResult() throws Exception {
            Mockito.when(utility.getName()).thenReturn("Test");
    
            assertEquals("Test", myService.showResult());
        }
    
        @Configuration
        static class MykServiceTestContextConfiguration {
    
            @Bean
            public MyService myService() {
                return new MyService();
            }
        }
    }