简体   繁体   English

春季测试服务类模拟实用程序类-Junit和Mockito

[英]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. 我想使用Junit + Mockito.为spring框架的服务层编写测试用例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. 如何使用我的ServiceTest类调用实际的服务层方法,如果我模拟ServiceTest类,则它的对象将不会执行实际的服务方法代码,因为它不会使该对象调用其方法,并且如果我尝试使用Spy仍然不会工作, 我尝试了此示例,但仍然无法执行测试用例。

MyService.java MyService.java

@Service
 public class MyService{

    @Autowired
    Utility utility;

    public String showResult(){

    String result = utility.getName();

    return result;
    }
    }

MyServiceTest.java 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);
            }
        }

    }

You have to first mock the Utility class and then have to invoke it before calling your @Test using MockitoAnnotations.initMocks(this) as follows: 您必须首先模拟Utility类,然后在使用MockitoAnnotations.initMocks(this)调用@Test之前先调用它,如下所示:

MyServiceTest.java 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 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 Utility.java

import org.springframework.stereotype.Component;

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

}

How about using @MockBean ? 使用@MockBean怎么@MockBean It suits Spring + JUnit and, probably you need to implement mock behavior. 它适合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();
        }
    }
}

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 仍然有问题共享您正在使用的春季版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM