//手动命令行测试 java -cp /usr1/junit:/usr1/cdncms/lib/* org.junit.runner.JUnitCore com.test.DomainServiceTest <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
增长配置项: windsows-->installJREs-->edit --> VM arguments --> 增长 -noverify
@BeforeClass 针对全部测试,只执行一次,且必须为static void
@Before: 初始化方法
@Test: 测试方法,在这里能够测试指望异常和超时时间
@After: 释放资源
@AfterClass: 针对全部测试,只执行一次,且必须为static void
@Ignore: 忽略的测试方法html
一个单元测试用例执行顺序为: @BeforeClass –> @Before –> @Test –> @After –> @AfterClass
每个测试方法的调用顺序为: @Before –> @Test –> @Afterjava
assertEquals(boolean expected, boolean actual) 检查两个变量或者等式是否平衡 assertFalse(boolean condition) 检查条件是假的 assertNotNull(Object object) 检查对象不是空的 assertNull(Object object) 检查对象是空的 assertTrue(boolean condition) 检查条件为真 fail() 在没有报告的状况下使测试不经过
@Test(expected = IllegalArgumentException.class) public void canVote_throws_IllegalArgumentException_for_zero_age() {.......}
a). 静态方法: import static org.mockito.Mockito.*; b). 验证方法是否调用: verify(test, times(2)).getUniqueId();
基本用法: (没法对static method和private method进行插桩) when(cls.methodName(args)).thenReturn(args) //对指定语句进行插桩 when(cls.methodName(args)).thenThrow(Exception) //抛出异常 一、 基本示例 LinkedList mockedList = mock(LinkedList.class); //插桩 when(mockedList.get(0)).thenReturn("first"); when(mockedList.get(1)).thenThrow(new RuntimeException()); System.out.println(mockedList.get(0)); //输出"first" System.out.println(mockedList.get(1)); //抛出异常 System.out.println(mockedList.get(999)); //输出 null , 由于get(999)未插桩 二、 插桩时可用 Matcher.anyString()/anyInt() 等进行入参匹配 when(mockedList.get(anyInt())).thenReturn("element"); 三、 针对void方法抛出异常 doThrow(new RuntimeException()).when(mockedList).clear(); 四、 针对void方法插桩 doNothing().when(spy).clear();
//两个重要注解 -- 使用ASM生成代理类进行mock @RunWith(PowerMockRunner.class) //通用配置 @PrepareForTest( { YourClassWithEgStaticMethod.class }) //须要powermock处理的class,static、final、私有方法等功能 1) 例如:去除'final方法的final标识,在静态方法的最前面加入本身的虚拟实现等。 2) 若是mock的是系统类的final/static,PowerMock会修改调用系统类的class文件,以知足mock需求。 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <powermock.version>1.7.1</powermock.version> </properties> <dependencies> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>${powermock.version}</version> <scope>test</scope> </dependency> </dependencies>
//经过PowerMock建立一个虚拟对象 InterfaceToMock mock = Powermockito.mock(InterfaceToMock.class) //value为你想要让这个method返回的值 Powermockito.when(mock.method(Params…)).thenReturn(valueToReturn) //若是这个方法返回值为空,则上面的写法会报错,可采用下面的写法 Powermockito.when(mock, “methodName”, Object… params).thenReturn(valueToReturn) // 也能够采用下面的写法,和上面的同样的效果 Powermockito.doReturn(valueToReturn).when(mock, “methodName”, Object… params) //这样写也行,适合返回值为void的方法 Powermockito.doReturn(valueToReturn).when(mock).methodName(Object… params) //你也可让方法抛异常 Powermockito.when(mock.method(Params..)).thenThrow(new OMSException(“oms”)) //你可让方法每一次返回的结果都不同,下面的例子第一次正常返回,第二次调用抛异常 Powermockito.when(mock.method(Params..)).thenReturn(valueToReturn).thenThrow(new OMSException(“some Exception”)) //若是方法返回值为void,不能用thenReturn,要用doThing() Powermockito.doNothing().when(mock.method(Params…))
public class IdGenerator { public static long generateNewId() { return 0L; } }
public class ClassUnderTest { public long methodToTest() { final long id = IdGenerator.generateNewId(); return id; } }
@RunWith(PowerMockRunner.class) @PrepareForTest(IdGenerator.class) public class TestStatic { @Test public void testCallInternalInstance() throws Exception { PowerMockito.mockStatic(IdGenerator.class); // 在这个测试用例中,当generateNewId()每次被调用时,都会返回15 PowerMockito.when(IdGenerator.generateNewId()).thenReturn(15L); Assert.assertEquals(15L, new ClassUnderTest().methodToTest()); } }
public class ClassUnderTest { public boolean createDirectoryStructure(String directoryPath) { File directory = new File(directoryPath); if (directory.exists()) { String msg = "\"" + directoryPath + "\" 已经存在."; throw new IllegalArgumentException(msg); } return directory.mkdirs(); } }
@RunWith(PowerMockRunner.class) @PrepareForTest(ClassUnderTest.class) public class TestConstruction { //模拟构造函数 @Test public void createDirectoryStructureWhenPathDoesntExist() throws Exception { final String directoryPath = "seemygod"; //建立File的模拟对象 File directoryMock = mock(File.class); //在当前测试用例下,当出现new File("seemygod")时,就返回模拟对象 whenNew(File.class).withArguments(directoryPath).thenReturn(directoryMock); //当调用模拟对象的exists时,返回false when(directoryMock.exists()).thenReturn(false); //当调用模拟对象的mkdirs时,返回true when(directoryMock.mkdirs()).thenReturn(true); assertTrue(new ClassUnderTest().createDirectoryStructure(directoryPath)); //验证new File(directoryPath); 是否被调用过 verifyNew(File.class).withArguments(directoryPath); } }
public class PrivatePartialMockingExample { public String methodToTest() { return methodToMock("input"); } private String methodToMock(String input) { return "REAL VALUE = " + input; } }
import static org.powermock.api.mockito.PowerMockito.*; @RunWith(PowerMockRunner.class) @PrepareForTest(PrivatePartialMockingExample.class) public class PrivatePartialMockingExampleTest { @Test public void demoPrivateMethodMocking() throws Exception { final String expected = "TEST VALUE"; final String nameOfMethodToMock = "methodToMock"; final String input = "input"; PrivatePartialMockingExample underTest = spy(new PrivatePartialMockingExample()); //模拟私有方法 when(underTest, nameOfMethodToMock, input).thenReturn(expected); assertEquals(expected, underTest.methodToTest()); verifyPrivate(underTest).invoke(nameOfMethodToMock, input); } }
public class ClassUnderTest { public boolean callSystemFinalMethod(String str) { return str.isEmpty(); } public String callSystemStaticMethod(String str) { return System.getProperty(str); } }
$RunWith(PowerMockRunner.class) public class TestClassUnderTest { $Test $PrepareForTest(ClassUnderTest.class) public void testCallSystemStaticMethod() { ClassUnderTest underTest = new ClassUnderTest(); PowerMockito.mockStatic(System.class); PowerMockito.when(System.getProperty("aaa")).thenReturn("bbb"); Assert.assertEquals("bbb", underTest.callJDKStaticMethod("aaa")); } }
//PushMsgPostProcessorImpl 是要测试的类,它有两个注解注入的类变量以下: @Resource private IMsgToUserService msgToUserService; //则测试类中可使用下面的方法注入 @Mock private IMsgToUserService msgToUserService; @Before public void setup() { MockitoAnnotations.initMocks(this); pushMsgPostProcessor = new PushMsgPostProcessorImpl(); //给注解的private变量塞一个值 ReflectionTestUtils.setField(pushMsgPostProcessor, "msgToUserService", msgToUserService); }
//PowerMockito.when(IOUtils.xMLReader()).thenThrow(SAXException.class); public class IOUtils { public static String xMLReader() throws SAXException { return "abc"; } }
//PowerMockito.doThrow(new SAXException()).when(IOUtils.class); public class IOUtils { public static String xMLReader(SAXReader reader) throws SAXException { System.out.println("IOUtils.xMLReader"); if (null == reader) { throw new SAXException(); } return "abc"; } }