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

  1. public aspect CommonAspectJ {
  2.     pointcut allCalls():execution(* *.*(..))&& !within(com.synnex.biz.common.*);
  3.     Object around(): allCalls() {
  4.         String className = thisJoinPoint.getSignature().getDeclaringType().getName();
  5.         Object receiver = thisJoinPoint.getThis();
  6.         if (receiver != null) {
  7.             className = receiver.getClass().getName();
  8.         }
  9.         String methodName = thisJoinPoint.getSignature().getName();
  10.         Object returnValue = CommonTestCase.getMockReturnValue(className, methodName);
  11.         if (returnValue != null) {
  12.             Hashtable arguments = (Hashtable) getArguments(thisJoinPoint);
  13.             CommonTestCase.indicateCalled(className, methodName, arguments);
  14.             return returnValue;
  15.         } else {
  16.             return proceed();
  17.         }
  18.     }
  19.     private Hashtable getArguments(JoinPoint jp) {
  20.         Hashtable arguments = new Hashtable();
  21.         Object[] argumentValues = jp.getArgs();
  22.         String[] argumentNames = ((CodeSignature) jp.getSignature()).getParameterNames();
  23.         for (int i = 0; i < argumentValues.length; i++) {
  24.             if (argumentValues[i] != null)
  25.                 arguments.put(argumentNames[i], argumentValues[i]);
  26.         }
  27.         return arguments;
  28.     }
  29. }

CommonTestCase.java

  1. public class CommonTestCase extends TestCase {
  2.     public CommonTestCase(String name) {
  3.         super(name);
  4.     }
  5.     public static void setMock(String className, String methodName, Object returnValue) {
  6.         testData.put(makeKey(className, methodName), returnValue);
  7.     }
  8.     public static void setMock(String className, String methodName) {
  9.         setMock(className, methodName, new Object());
  10.     }
  11.     public static Object getMockReturnValue(String className, String methodName) {
  12.         return testData.get(makeKey(className, methodName));
  13.     }
  14.     public void assertCalled(String className, String methodName) {
  15.         if (!isCalled(className, methodName))
  16.             fail("The method '" + methodName + "' in class '" + className + "' was expected to be called but it wasn't");
  17.     }
  18.     public Object getArgument(String className, String methodName, String argumentName) {
  19.         Object argument = null;
  20.         Hashtable arguments = (Hashtable) callsMade.get(makeKey(className, methodName));
  21.         if (arguments != null)
  22.             argument = arguments.get(argumentName);
  23.         return argument;
  24.     }
  25.     public static void indicateCalled(String className, String methodName, Hashtable arguments) {
  26.         callsMade.put(makeKey(className, methodName), arguments);
  27.     }
  28.     public static boolean isCalled(String className, String methodName) {
  29.         return callsMade.get(makeKey(className, methodName)) != null;
  30.     }
  31.     public void assertArgumentPassed(String className, String methodName, String argumentName, Object argumentValue) {
  32.         Object argument = getArgument(className, methodName, argumentName);
  33.         if (argument == null || !argument.equals(argumentValue))
  34.             fail("The argument '" + argumentName + "' of method '" + methodName + "' in class '" + className + " ' should have the value '" + argumentValue + "' but it was '" + argument + "'!");
  35.     }
  36.     private static String makeKey(String className, String methodName) {
  37.         return className + "." + methodName;
  38.     }
  39.     private static Hashtable testData = new Hashtable();
  40.     private static Hashtable callsMade = new Hashtable();
  41. }

How to use it?here is a simple example

  1. public void testDaoGetManagerInService() {
  2.         MyService service = new MyService();
  3.         String loginId = "testaccount";
  4.         Map mockResult = new HashMap();
  5.         mockResult.put("loginid""testaccount");
  6.         mockResult.put("firstName""stefli");
  7.         mockResult.put("lastName""TestLastTest");
  8.         try {
  9.             setMock("com.synnex.biz.MyDao""getManagerByLoginID", mockResult);
  10.             Integer result = service.getManagerByLoginID(loginId);
  11.             assertEquals(1, result.intValue());
  12.         } catch (Exception e) {
  13.             assertEquals("Nothing found!", e.getMessage());
  14.         }
  15.     }

 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.

原创粉丝点击