六道 发表于 2016-3-8 10:46:25

djunit用法

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
页: [1]
查看完整版本: djunit用法