zoukankan      html  css  js  c++  java
  • [Unit Testing] Using Mockito Annotations

    Previously we have seen how to do Unit testing with Mockito;

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

    In this post, we are going to see, using annotation from Mockito to make testing easier:

    @RunWith(MockitoJUnitRunner.class)
    public class SomeBusinessMockTests {
    
        @Mock
        DataService mockService;
    
        @InjectMocks
        SomeBusinessImpl businessImpl;
    
        @Test
        public void testFindTheGreatestFromAllData() {
            
            when( mockService.retieveAllData()).thenReturn(new int[] {24, 6, 15});
            assertEquals(24, businessImpl.findTheGreatestFromAllData());
        }
    }    

    We need to place @RunWIth for the Test class, otherwise it won't work.

    @Mock makes it easier to create a mock class.

    @InjectMock makes it easier to inject created mock into a class.

  • 相关阅读:
    js正则表达式大全(2)
    Magic Trackpad 2 on win10 x64
    Google 日历短信通知没有了
    Ueditor 1.4.3 jsp utf-8版Bug修复
    [转]eclipse中build workspace的相关优化
    Hello,
    EpCloud开发日志
    为服务创建安装程序
    winform 通过WCF上传Dataset数据
    opcrcw.da.dll 和.net 4.0
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10732065.html
Copyright © 2011-2022 走看看