Follow @RoyOsherove on Twitter

Setting Up VirtualMock in the Eclipse IDE

One of the things I teach as part of Team Agile is how to do Test Driven Development and write unit tests in Java. Yes. This is .NET weblogs, but the following information is so scarce and hard to find I just have to post it.
After a long time of trial and error trying to run a framework called "VirtualMock" (which is similar to TypeMock in .NET in its purposes).
Anyway - it's impossible to figure out how to make the thing work under Eclipse - no documentation found on the net for this.
Luckily, Eli Lopian of Amdocs finally found a way to make it work with the Eclipse IDE. He was kind enough to let me post the steps he used to make this happen so that future generations could use this practically non-existent documentation as they search it via Google:
 
 
Hi,
 
Here are the instuctions for setting up using virtual mock in eclipse.
 
A.1. Setting Up VirtualMocks in eclipse
 
Following is the method to set up virtual mocks in eclipse (3.0).
 
1. Install AJDT (The AspectJ Development Tool) and restart eclipse.
 

2. Upzip the virtualmock-0.1.9-src.zip and add the following jars to the classpath: (they are in the lib directory)
aspectwerkz-0.9.RC1.jar
throwableutil-1.0.jar
log4j-1.2.8.jar
bcel-5.1.jar
jrexx-1.1.1.jar
virtualmock-0.1.9.jar
virtualmock-aspect-0.1.9.jar
cglib-asm-1.0.jar
commons-lang-2.0.jar
easymock.jar
easymock-patch-1.0.jar
hansel-1.02.jar
Note: It is possible to create a User Library that includes these files - see eclipse help on User Libraries
 
3. Change the test project to AcpectJ doint one of the following.
   1. New AspectJ project – for new projects
   2. Right click project->Convert to AcpectJ project
 
4. Open the Project Properties…
 
5. Select AspectJ Aspect Path ->Add the virtualmock-aspect-0.1.19.jar
 
6. Add the classes from the production project as follows 
         Select AspectJ InPath -> Add Class Folder -> choose the classes directory of the tested project.
         Add additional Jars of the tested project that will be mocked.
 
7. You are now ready to go, write your tests and run and debug as normal.
 
8. If your classes are not being mocked make sure that they are in the AspectJ InPath, to debug, you can check "output weaving info" in the AspectJ portion of the projects properties.
 
 
 
 
 
A.2. Setting Up Dynamic VirtualMocks in eclipse
 
Following is the method to set up dynamic virtual mocks in eclipse. Dynamic mocks are instrumented at runtime. There is no need to install any extra plugins but this works from Java 1.4 as it uses the HotSwap feature:
 
 
1. Upzip the virtualmock-0.1.9-src.zip and add the following jars to the classpath: (they are in the lib directory)
aspectwerkz-0.9.RC1.jar
throwableutil-1.0.jar
log4j-1.2.8.jar
bcel-5.1.jar
jrexx-1.1.1.jar
virtualmock-0.1.9.jar
virtualmock-aspect-0.1.9.jar
cglib-asm-1.0.jar
commons-lang-2.0.jar
easymock.jar
easymock-patch-1.0.jar
hansel-1.02.jar
Note: It is possible to create a User Library that includes these files - see eclipse help on User Libraries
 
2. In the Run Configuration of your tests -> (Run->Run... and choose or create a new JUnit Run Configuration) Choose Arguments Tab -> Add the following to the VM arguments:
 
-Xdebug -Xrunaspectwerkz -Xbootclasspath/a:${project_loc}/lib/bcel-5.1.jar;${project_loc}/lib/aspectwerkz-core-0.9.RC1.jar
 
3. Make sure that aspectWerkz.dll in on your path.
 

 
 

Try to avoid multiple asserts in a single unit test

Blogs.RegexAdvice.com - smells like community spirit