Two important files for Aspect&Junit test.
来源:互联网 发布:pat考编程 编辑:程序博客网 时间:2024/05/09 16:52
These two files are CommonAspectJ.aj and CommonTestCase, they are very important for testing the complex system.
if there is an instance class created by "new" method, or other static method in our tested service method, it's quite difficult to let the test pass. So we need to mock an object to instead of it. how to mock it? we can use such two files to help us.
CommonAspectJ.aj
- public aspect CommonAspectJ {
- pointcut allCalls():execution(* *.*(..))&& !within(com.synnex.biz.common.*);
- Object around(): allCalls() {
- String className = thisJoinPoint.getSignature().getDeclaringType().getName();
- Object receiver = thisJoinPoint.getThis();
- if (receiver != null) {
- className = receiver.getClass().getName();
- }
- String methodName = thisJoinPoint.getSignature().getName();
- Object returnValue = CommonTestCase.getMockReturnValue(className, methodName);
- if (returnValue != null) {
- Hashtable arguments = (Hashtable) getArguments(thisJoinPoint);
- CommonTestCase.indicateCalled(className, methodName, arguments);
- return returnValue;
- } else {
- return proceed();
- }
- }
- private Hashtable getArguments(JoinPoint jp) {
- Hashtable arguments = new Hashtable();
- Object[] argumentValues = jp.getArgs();
- String[] argumentNames = ((CodeSignature) jp.getSignature()).getParameterNames();
- for (int i = 0; i < argumentValues.length; i++) {
- if (argumentValues[i] != null)
- arguments.put(argumentNames[i], argumentValues[i]);
- }
- return arguments;
- }
- }
CommonTestCase.java
- public class CommonTestCase extends TestCase {
- public CommonTestCase(String name) {
- super(name);
- }
- public static void setMock(String className, String methodName, Object returnValue) {
- testData.put(makeKey(className, methodName), returnValue);
- }
- public static void setMock(String className, String methodName) {
- setMock(className, methodName, new Object());
- }
- public static Object getMockReturnValue(String className, String methodName) {
- return testData.get(makeKey(className, methodName));
- }
- public void assertCalled(String className, String methodName) {
- if (!isCalled(className, methodName))
- fail("The method '" + methodName + "' in class '" + className + "' was expected to be called but it wasn't");
- }
- public Object getArgument(String className, String methodName, String argumentName) {
- Object argument = null;
- Hashtable arguments = (Hashtable) callsMade.get(makeKey(className, methodName));
- if (arguments != null)
- argument = arguments.get(argumentName);
- return argument;
- }
- public static void indicateCalled(String className, String methodName, Hashtable arguments) {
- callsMade.put(makeKey(className, methodName), arguments);
- }
- public static boolean isCalled(String className, String methodName) {
- return callsMade.get(makeKey(className, methodName)) != null;
- }
- public void assertArgumentPassed(String className, String methodName, String argumentName, Object argumentValue) {
- Object argument = getArgument(className, methodName, argumentName);
- if (argument == null || !argument.equals(argumentValue))
- fail("The argument '" + argumentName + "' of method '" + methodName + "' in class '" + className + " ' should have the value '" + argumentValue + "' but it was '" + argument + "'!");
- }
- private static String makeKey(String className, String methodName) {
- return className + "." + methodName;
- }
- private static Hashtable testData = new Hashtable();
- private static Hashtable callsMade = new Hashtable();
- }
How to use it?here is a simple example
- public void testDaoGetManagerInService() {
- MyService service = new MyService();
- String loginId = "testaccount";
- Map mockResult = new HashMap();
- mockResult.put("loginid", "testaccount");
- mockResult.put("firstName", "stefli");
- mockResult.put("lastName", "TestLastTest");
- try {
- setMock("com.synnex.biz.MyDao", "getManagerByLoginID", mockResult);
- Integer result = service.getManagerByLoginID(loginId);
- assertEquals(1, result.intValue());
- } catch (Exception e) {
- assertEquals("Nothing found!", e.getMessage());
- }
- }
Tips:
1. For different projects ,we need to change the package to adapt the certain project.
pointcut allCalls():execution(* *.*(..))&& !within(com.synnex.biz.common.*);
2. We let MyDao.getManagerByLoginID method return the mock Map.
- Two important files for Aspect&Junit test.
- Two @test parameters in Junit
- Android项目集成Jenkins(JUnit test & Coverage)
- Junit test for Spring and Struts
- Android JUnit Test for Eclipse使用入门
- junit test
- junit test
- junit-test
- junit @Test
- A template build.xml for running junit test by ant
- Android JUnit Test Unable to execute dex: Multiple dex files define 解决方法
- Test lifecycle -- important chapter in test plan
- What is the most important aspect of programming?
- 重新开张 &amp;amp;amp;&amp;amp;amp; two pieces of fascinating code
- for test test test
- java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger for JUnit test case for Java mail
- java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger for JUnit test case for Java mail
- Matroska Test Files
- 闹中取静
- 写在2008年的最后第二天
- Linux下Makefile的automake生成全攻略
- 如何控制文本框只能输入数字
- 足浴航母富侨保健引进IZO企业电视
- Two important files for Aspect&Junit test.
- GridView与ObjectDataSource的使用
- /proc文件系统中网络参数
- 如何输出long long型数据
- oracle 如何在SQL PLUS中执行FUNCTION?
- d3d9 固定管线,打开光照的情况下还保留有自己定义的顶点颜色
- 自己对static和reference理解不透彻造成使用linkedlist.add错误
- 七个受用一生的心理寓言
- 使用Script控制readOnly属性