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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41486115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:55:06  来源:igfitidea点击:

Spring test service class mocking utility class- Junit and Mockito

javaspringunit-testingjunitmockito

提问by Bhabani Sankar Sahoo

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

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

How to call the actual service layer method using my ServiceTestclass, If i mock the ServiceTestclass 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 examplestill I not able to execute the test cases.

如何使用我的ServiceTest类调用实际的服务层方法,如果我模拟ServiceTest该类,那么它的对象不会执行实际的服务方法代码,因为它不会让对象调用它的方法,如果我尝试使用 Spy 仍然无法正常工作,我试过这个例子仍然无法执行测试用例。

MyService.java

我的服务

@Service
 public class MyService{

    @Autowired
    Utility utility;

    public String showResult(){

    String result = utility.getName();

    return result;
    }
    }

MyServiceTest.java

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

    }

回答by Arpit Aggarwal

You have to first mock the Utilityclass and then have to invoke it before calling your @Testusing MockitoAnnotations.initMocks(this)as follows:

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

MyServiceTest.java

我的服务测试.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

实用程序.java

import org.springframework.stereotype.Component;

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

}

回答by hiroyukik

How about using @MockBean? It suits Spring + JUnit and, probably you need to implement mock behavior.

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

I guess that Utility.getName()return "Test" in the test case.

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

The following is the test code I tried.

以下是我试过的测试代码。

@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();
        }
    }
}

回答by Barath

Make use of @Spy

使用@Spy

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

当 spy 被调用时,则调用真实对象的实际方法。

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

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

仍然有问题分享您正在使用的 spring 版本