zoukankan      html  css  js  c++  java
  • [Spring Boot] Introduce to Mockito

    We have the implemetion: 

    @SpringBootApplication
    public class MockitoDemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MockitoDemoApplication.class, args);
        }
    
    }
    
    public class SomeBusinessImpl {
        private DataService dataService;
    
        public SomeBusinessImpl(DataService dataService ){
            super();
            this.dataService = dataService;
        }
    
        public int findTheGreatestFromAllData() {
            int[] data = dataService.retieveAllData();
            int greatest = Integer.MIN_VALUE;
            for (int value : data) {
    
                if (value > greatest) {
                    greatest = value;
                }
            }
            return greatest;
        }
    }
    
    
    public interface DataService {
        int[] retieveAllData();
    }

    And we want to test agaisnst it:

    import org.junit.Test;
    
    import static org.junit.Assert.assertEquals;
    import static org.mockito.Mockito.mock;
    import static org.mockito.Mockito.when;
    
    public class SomeBusinessMockTests {
    
        @Test
        public void testFindTheGreatestFromAllData() {
            DataService mockService = mock(DataService.class);
            when( mockService.retieveAllData()).thenReturn(new int[] {24, 6, 15});
    
            SomeBusinessImpl businessImpl = new SomeBusinessImpl(mockService);
            int result = businessImpl.findTheGreatestFromAllData();
            assertEquals(24, result);
        }
    }
  • 相关阅读:
    网络拓扑
    OSI 7层模型和TCP/IP 4层模型
    第一范式 第二范式 第三范式 BC范式 第四范式
    医院 信息科
    李纳斯•托瓦兹
    所谓绅士,就是做自己该做之事,而不是想做之事。
    活着
    开头词
    人际题目
    人际关系
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10725146.html
Copyright © 2011-2022 走看看