分享

djunit用法

六道 发表于 2016-3-8 10:46:25 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 10638
1.首先开发平台不要用Eclipse Java EE IDE for Web Developers ,工程属性里面都没有DjUnit。需要用Eclipse Platform,工程属性DjUnit里面勾选Virtual Mock Objects,这样就可以使用mock了。
jar包要包含djunit.jar 、junit.jar
2.example
TaskDispatcherThreadTest.java
package com.fujitsu.cfmg.view.thread;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import jp.co.dgic.testing.common.virtualmock.MockObjectManager;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.junit.Test;
import com.fujitsu.cfmg.job.JobService;

public class TaskDispatcherThreadTest {

public static Object reflectionRunMethod(Object object, String methodName,
   Class<?>[] classParams, Object[] params)
   throws IllegalArgumentException, IllegalAccessException,
   InvocationTargetException, SecurityException, NoSuchMethodException {
  Class<?> classType = object.getClass();
  Method method = classType.getDeclaredMethod(methodName, classParams);
  method.setAccessible(true);
  return method.invoke(object, params);
}

public static OMElement addNewElement(OMElement element, String name) {
  OMFactory fac = OMAbstractFactory.getOMFactory();
  return fac.createOMElement(name, null, element);
}
TaskDispatcherThread thread = new TaskDispatcherThread();

@Test
public void MT_101002_001() throws Exception {

  Class<?>[] classParams = new Class<?>[] { String.class };
  Object[] params = new Object[] { "000000001" };
  
  MockObjectManager.initialize();
  MockObjectManager.addReturnNull(JobService.class,"getJob");
  
  try {
   Object ret = reflectionRunMethod(thread,"judgejobGroup", classParams, params);  
   
   System.out.println("ret="+ret);
  } catch (Exception e) {
   
  } finally {
  }
}

@Test
public void MT_101002_002() throws Exception {
  
  Class<?>[] classParams = new Class<?>[] { String.class };
  Object[] params = new Object[] { "000000001" };
  
  OMElement omelementTest = addNewElement(null, "job");  
  OMElement jobTypeElement = addNewElement(null, "jobType");
  jobTypeElement.setText("configurationchange");  
  OMElement processesOMElement = addNewElement(null, "processes");
  String processes=null;
  processesOMElement.setText(processes);
  
  omelementTest.addChild(jobTypeElement);
  //omelementTest.addChild(processesOMElement);
  System.out.println(omelementTest.toString());
  
  MockObjectManager.initialize();
  MockObjectManager.addReturnValue(JobService.class,"getJob",omelementTest);
  
  try {
   Object ret = reflectionRunMethod(thread,"judgejobGroup", classParams, params);  
   
   System.out.println("ret="+ret);
  } catch (Exception e) {
   
  } finally {
  }
  
}

@Test
public void MT_101002_003() throws Exception {
  
  Class<?>[] classParams = new Class<?>[] { String.class };
  Object[] params = new Object[] { "000000001" };
  
  OMElement omelementTest = addNewElement(null,"job");  
  OMElement jobTypeElement = addNewElement(null,"jobType");
  jobTypeElement.setText("configurationchange");  
  OMElement processesOMElement = addNewElement(null,"processes");
  OMElement processOMElement = addNewElement(null,"process");
  processOMElement.setText("");
  //processesOMElement.addChild(processOMElement);
  
  omelementTest.addChild(jobTypeElement);
  omelementTest.addChild(processesOMElement);
  
  System.out.println(omelementTest.toString());
  
  MockObjectManager.initialize();
  MockObjectManager.addReturnValue(JobService.class,"getJob",omelementTest);
  
  try {
   Object ret = reflectionRunMethod(thread,"judgejobGroup", classParams, params);  
   
   System.out.println("ret="+ret);
  } catch (Exception e) {
   
  } finally {
  }
  
}
}
3.右键运行,选djUnit
如果报log4j之类的错误,在src下创建log4j.properties,其内容为:
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条