其他分享
首页 > 其他分享> > 单元测试常识

单元测试常识

作者:互联网

文章目录

Mickito

Question

  1. Spring如何mock掉service和dao ?

参考

public class MyServiceImplTest 
{
	@InjectMocks
    private MyDataService myDataService;
	@InjectMocks
    private NyService myService;

    @Mock
    private MyRepository myRepository;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this); // this is needed for inititalizytion of mocks, if you use @Mock 
        // 有了@InjectMocks 似乎不需要这句了
		// myDataService = new MyDataServiceImpl(myRepository); 
        myService = new MyServiceImpl(myDataService);
    }

    @Test
    public void getById_ValidId() {

        doReturn(someMockData).when(myRepository).findOne("1");
        MyObject myObject = myService.getById("1");

        //Whatever asserts need to be done on the object myObject 
    }
}

标签:myRepository,myDataService,单元测试,常识,private,myService,InjectMocks,public
来源: https://blog.csdn.net/qq_30118563/article/details/121043106