pom.xml增加相关依赖
junit junit test org.powermock powermock-module-junit4 1.4.10 test org.powermock powermock-api-mockito 1.4.10 test org.springframework spring-test 3.0.0.RELEASE jar compile
testcase基类 配置文件视情况而定
主要是init方法,根据接口mock对象塞到指定对象中,并保存到mockMap中。
/** * Copyright (c) 2011-2014 All Rights Reserved. */ import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.net.JarURLConnection; import java.net.URL; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Arrays; import java.util.Enumeration; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarFile; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.slf4j.Logger; import org.springframework.beans.BeansException; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.GlobalTransactionalTestExecutionListener; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.test.context.transaction.TransactionConfigurationAttributes; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.interceptor.DelegatingTransactionAttribute; import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute; @RunWith(SpringJUnit4ClassRunner.class) @TransactionConfiguration(defaultRollback = false) @TestExecutionListeners(GlobalTransactionalTestExecutionListener.class) @ContextConfiguration(locations = {"classpath:app-test.xml"}) public abstract class SpringContextTests extends AbstractJUnit4SpringContextTests { protected MapmockMap = new HashMap (); protected Object getBean(String name) { return applicationContext.getBean(name); } protected void init(Object target, Class>... classes) { // mock classes Class> targetClass = target.getClass(); List list = getFields(targetClass); List > mockList = Arrays.asList(classes); for (Field field : list) { String fieldName = field.getName(); Object obj = null; Class> type = field.getType(); int modifier = type.getModifiers(); if (!Modifier.isStatic(modifier) && !Modifier.isFinal(modifier) && !Logger.class.equals(type)) { if (mockList.contains(type)) { Object ret = mockMap.get(fieldName) == null ? PowerMockito.mock(type) : mockMap.get(fieldName); mockMap.put(fieldName, ret); setProperty(target, fieldName, ret); } else if (Modifier.isInterface(modifier)) { if (fieldName.contains("Adapter")) { setProperty(target, fieldName, getBean(fieldName)); } else { Set > set = getClasses(type.getName().split("." + type.getSimpleName())[0]); for (Class> setClass : set) { if (type.isAssignableFrom(setClass) && !Modifier.isInterface(setClass.getModifiers()) && ((setClass.getSimpleName().substring(0, 1).toLowerCase() + setClass.getSimpleName().substring(1)).contains(fieldName) || setClass.getSimpleName().contains("Converter"))) { try { obj = setClass.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (obj != null) { init(obj, classes); } setProperty(target, fieldName, obj); } } } } else { try { obj = type.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } setProperty(target, fieldName, obj); } } } } private List getFields(Class> targetClass) { List list = new ArrayList (); list.addAll(Arrays.asList(targetClass.getDeclaredFields())); list.addAll(Arrays.asList(targetClass.getFields())); return list; } /** * 从包package中获取所有的Class * * @param pack * @return */ public Set > getClasses(String pack) { // 第一个class类的集合 Set > classes = new LinkedHashSet >(); // 是否循环迭代 boolean recursive = true; // 获取包的名字 并进行替换 String packageName = pack; String packageDirName = packageName.replace('.', '/'); // 定义一个枚举的集合 并进行循环来处理这个目录下的things Enumeration dirs; try { dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName); // 循环迭代下去 while (dirs.hasMoreElements()) { // 获取下一个元素 URL url = dirs.nextElement(); // 得到协议的名称 String protocol = url.getProtocol(); // 如果是以文件的形式保存在服务器上 if ("file".equals(protocol)) { // 获取包的物理路径 String filePath = URLDecoder.decode(url.getFile(), "UTF-8"); // 以文件的方式扫描整个包下的文件 并添加到集合中 findAndAddClassesInPackageByFile(packageName, filePath, recursive, classes); } else if ("jar".equals(protocol)) { // 如果是jar包文件 // 定义一个JarFile JarFile jar; try { // 获取jar jar = ((JarURLConnection) url.openConnection()).getJarFile(); // 从此jar包 得到一个枚举类 Enumeration entries = jar.entries(); // 同样的进行循环迭代 while (entries.hasMoreElements()) { // 获取jar里的一个实体 可以是目录 和一些jar包里的其他文件 如META-INF等文件 JarEntry entry = entries.nextElement(); String name = entry.getName(); // 如果是以/开头的 if (name.charAt(0) == '/') { // 获取后面的字符串 name = name.substring(1); } // 如果前半部分和定义的包名相同 if (name.startsWith(packageDirName)) { int idx = name.lastIndexOf('/'); // 如果以"/"结尾 是一个包 if (idx != -1) { // 获取包名 把"/"替换成"." packageName = name.substring(0, idx).replace('/', '.'); } // 如果可以迭代下去 并且是一个包 if ((idx != -1) || recursive) { // 如果是一个.class文件 而且不是目录 if (name.endsWith(".class") && !entry.isDirectory()) { // 去掉后面的".class" 获取真正的类名 String className = name.substring(packageName.length() + 1, name.length() - 6); try { // 添加到classes classes.add(Class.forName(packageName + '.' + className)); } catch (ClassNotFoundException e) { // log // .error("添加用户自定义视图类错误 找不到此类的.class文件"); e.printStackTrace(); } } } } } } catch (IOException e) { // log.error("在扫描用户定义视图时从jar包获取文件出错"); e.printStackTrace(); } } } } catch (IOException e) { e.printStackTrace(); } return classes; } /** * 以文件的形式来获取包下的所有Class * * @param packageName * @param packagePath * @param recursive * @param classes */ public void findAndAddClassesInPackageByFile(String packageName, String packagePath, final boolean recursive, Set > classes) { // 获取此包的目录 建立一个File File dir = new File(packagePath); // 如果不存在或者 也不是目录就直接返回 if (!dir.exists() || !dir.isDirectory()) { // log.warn("用户定义包名 " + packageName + " 下没有任何文件"); return; } // 如果存在 就获取包下的所有文件 包括目录 File[] dirfiles = dir.listFiles(new FileFilter() { // 自定义过滤规则 如果可以循环(包含子目录) 或则是以.class结尾的文件(编译好的java类文件) public boolean accept(File file) { return (recursive && file.isDirectory()) || (file.getName().endsWith(".class")); } }); // 循环所有文件 for (File file : dirfiles) { // 如果是目录 则继续扫描 if (file.isDirectory()) { findAndAddClassesInPackageByFile(packageName + "." + file.getName(), file.getAbsolutePath(), recursive, classes); } else { // 如果是java类文件 去掉后面的.class 只留下类名 String className = file.getName().substring(0, file.getName().length() - 6); try { // 添加到集合中去 // classes.add(Class.forName(packageName + '.' + className)); // 经过回复同学的提醒,这里用forName有一些不好,会触发static方法,没有使用classLoader的load干净 classes.add(Thread.currentThread().getContextClassLoader().loadClass(packageName + '.' + className)); } catch (ClassNotFoundException e) { // log.error("添加用户自定义视图类错误 找不到此类的.class文件"); e.printStackTrace(); } } } } public void setProperty(Object obj, String propertyName, Object value) { Class> cls = obj.getClass(); try { Field field = cls.getDeclaredField(propertyName); if (!Modifier.isFinal(field.getModifiers())) { field.setAccessible(true); field.set(obj, value); } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } private TransactionDefinition transactionDefinition; private PlatformTransactionManager transactionManager; private TransactionStatus transactionStatus; public void beginTransaction() { RuleBasedTransactionAttribute transactionAttribute = new RuleBasedTransactionAttribute(); transactionAttribute .setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);// nested // transactions transactionDefinition = new DelegatingTransactionAttribute( transactionAttribute) { public String getName() { return "ClassTransactionalTestCase"; } }; transactionManager = getTransactionManager(); transactionStatus = transactionManager .getTransaction(transactionDefinition); } public void endTransaction() { transactionManager.rollback(transactionStatus); } private TransactionConfigurationAttributes retrieveConfigurationAttributes() { Class> clazz = this.getClass(); Class annotationType = TransactionConfiguration.class; TransactionConfiguration config = clazz.getAnnotation(annotationType); if (logger.isDebugEnabled()) { logger.debug("Retrieved @TransactionConfiguration [" + config + "] for test class [" + clazz + "]"); } String transactionManagerName; boolean defaultRollback; if (config != null) { transactionManagerName = config.transactionManager(); defaultRollback = config.defaultRollback(); } else { transactionManagerName = (String) AnnotationUtils.getDefaultValue( annotationType, "transactionManager"); defaultRollback = (Boolean) AnnotationUtils.getDefaultValue( annotationType, "defaultRollback"); } TransactionConfigurationAttributes configAttributes = new TransactionConfigurationAttributes( transactionManagerName, defaultRollback); if (logger.isDebugEnabled()) { logger.debug("Retrieved TransactionConfigurationAttributes [" + configAttributes + "] for class [" + clazz + "]"); } return configAttributes; } private PlatformTransactionManager getTransactionManager() { String tmName = retrieveConfigurationAttributes() .getTransactionManagerName(); try { return this.applicationContext.getBean(tmName, PlatformTransactionManager.class); } catch (BeansException ex) { if (logger.isWarnEnabled()) { logger.warn( "Caught exception while retrieving transaction manager with bean name [" + tmName + "] for test context [" + this + "]", ex); } throw ex; } } }
编写具体测试类,继承上面的基类。
并创建setUp方法,加之以@Before注解,内部调用init方法。方便后续测试方法调用。
常用powerMock方法:
PowerMockito.doReturn(result).when(mock).someMethod(Mockito.any(Argument.class)...); PowerMockito.doThrow(new RuntimeException()).when(mock).someVoidMethod();