1.环境创建(Module:app) dependencies { implementation “org.mockito:mockito-core:2.11.0” } 2.测试类
package com.example.androietest; public class Add { private Velue mVelue; public Add(Velue mockMultiplication){ mVelue = mockMultiplication; } public int addAndValue(int a,int b){ return a+b+mVelue.getVelue(); } }3.moke类
package com.example.androietest; public class Velue { public int getVelue(){ return 5; } }4.测试代码
package com.example.androietest; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; public class MokeAddTest { @Test public void testIsNotNull(){ Velue velue = mock(Velue.class); doReturn(4).when(velue).getVelue(); Add add = new Add(velue); int add_result = add.addAndValue(2,2); assertEquals(8 ,add_result); } }5.测试结果