Skip to content

Video tutorial: Test Driven Development with Mock Objects

October 30, 2008 by Przemysław Bielicki

In this eleven minute tutorial I'm presenting TDD with mocking technique using JUnit 4.x and EasyMock library. This is more advanced example of Test Driven Development - video tutorial on basics can be downloaded from here: Test Driven Development in practice.

Source code for this tutorial can be downloaded here (you can find also Eclipse project files in this bundle, so it's easy to import the project directly to your IDE - NetBeans users have to create a new project and add relevant classpath changes). Note that external.jar was built with Java 6 and thus you have to use at least JDK version 6 to be able to compile and run the example.

Enjoy and comment the video if you find it useful or useless. I'm really curious what you think about the video as well as about the mocking technique in general.

AttachmentSize
tdd-with-mocks.zip72.87 KB

About the Author: Przemysław graduated from Gdańsk University of Technology in 2004 having specialized in Distributed Information Systems. He worked in Lufthansa Systems, Intel Corporation in the past where he developed complex IT solutions in many Java-related technologies. In professional life he is a real Java expert holding couple of Sun Java certificates (Programmer, Developer, Web Developer) and Certified Scrum Master, of course.

Przemysław is a regular contributor to AgileSoftwareDevelopment.com and the author of "From Java to Java EE" blog. He now works as a Software Craftsman in an international company that is the leading Global Distribution System (GDS) and the biggest processor of travel bookings in the world. Contact Przemysław

Comments

Good job

October 30, 2008 by Albert (not verified), 1 year 19 weeks ago
Comment id: 1954

That's a nice tutorial! I'd like to see something similar with Mockito instead of EasyMock. Have you tried it?

Re: Good job

October 30, 2008 by pbielicki, 1 year 19 weeks ago
Comment id: 1956

No I haven't tried it. I have a rule that if something works for me fine I don't touch sth else that does the same. Except the cases where the second option does it much much better.

I would gladly see the tutorial if you produce one :)

Cheers!
Przemek

Nice.

October 31, 2008 by Steve (not verified), 1 year 19 weeks ago
Comment id: 1957

Thanks for the introduction. It opened my eyes a little to what/how to use EasyMock. I intend to look at it in more depth now as well as look at Mockito.

problem running the tutorial

October 31, 2008 by Alain (not verified), 1 year 19 weeks ago
Comment id: 1959

Hi,

I have this error when running CpuLoadMonitorTest :

java.lang.NullPointerException
at java.lang.reflect.Proxy.getInvocationHandler(Proxy.java:636)
at org.easymock.EasyMock.getControl(EasyMock.java:1440)
at org.easymock.EasyMock.verify(EasyMock.java:1410)
at com.example.CpuLoadMonitorTest.after(CpuLoadMonitorTest.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.junit.internal.runners.BeforeAndAfterRunner.invokeMethod(BeforeAndAfterRunner.java:74)
at org.junit.internal.runners.BeforeAndAfterRunner.runAfters(BeforeAndAfterRunner.java:65)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:37)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Re: problem running the tutorial

October 31, 2008 by pbielicki, 1 year 19 weeks ago
Comment id: 1960

Well, I don't have it. I'm using Eclipse 3.4.1and JUnit 4.3.1 (JUnit Eclipse plugin)

problem running the tutorial

October 31, 2008 by Alain (not verified), 1 year 19 weeks ago
Comment id: 1961

thank you for the response.

I have JUnit 4.3.1 (JUnit Eclipse plugin) and Eclipse 3.4.0 , quite exactly the same configuration as you.

I tried with Netbeans and I had this error :

...
external.jar(com/external/GlobalHostMonitor.class)
class file has wrong version 50.0, should be 49.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
import com.external.GlobalHostMonitor
...

but it's ok, I understood how easymock is working, and I know better Test Driven Development, great tutorial

thank you

Re: problem running the tutorial

October 31, 2008 by pbielicki, 1 year 19 weeks ago
Comment id: 1962

GlobalHostMonitor.class was compiled using Java 6 so if you are trying to compile and running the examples with Java 5 and Java 1.4 you get the error you posted.

I hope it helps.

Thanks for comments.

[RESOLVED] problem running the tutorial

October 31, 2008 by Alain (not verified), 1 year 19 weeks ago
Comment id: 1963

fantastic. thank you.

Great work.

November 5, 2008 by Jongyun, Kim (not verified), 1 year 19 weeks ago
Comment id: 1978

I've learned the essentials of unit test with mock from the video.
thank you very much.

Heh - It was quick and essential

November 21, 2008 by Sebo (not verified), 1 year 16 weeks ago
Comment id: 2034

Thx, for Your work.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <b> <i> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <br> <blockquote>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.

Best of AgileSoftwareDevelopment.com