views:

449

answers:

8

I'd like to know your thoughts about test/mocking frameworks that are widely used and have a good level of compatibility between Java and .NET. I mean, I want to learn those tools to use in a .NET project, but I still wanna be able to apply that knowledge in Java projects.

  • I know there're many questions about test/mocking frameworks to those platforms especifically here in SO, but I've not found one question comparing those frameworks regarding its similarities in those two platforms.
+2  A: 

We use RhinoMocks and NUnit for our .NET projects. JUnit will be the Java alternative

crauscher
+5  A: 

The N/J-series of frameworks (NUnit/JUnit, NMock/JMock, etc.) are typically parallel ports of each other or are based on the same starting principles. Those will definitely let you transfer at least some of your knowledge between them.

John Feminella
For the record, JMock2 is significantly different from JMock1 (from which NMock was cloned). It doesn't use strings for method names any more.
Steve Freeman
+1  A: 

I've used EasyMock in some Java projects and I like it very much. There seems to be a .net port but I haven't used it yet, personally.

Concerning unit tests, I use JUnit 4.x, which has some nice extensions compared to 3.x. I guess NUnit provides similar functionality but haven't used it, either.

Huxi
+4  A: 

Here is what I see from my experience on the Java side of the fence.

Unit testing frameworks

As for unit testing in Java, pretty much everyone is using JUnit and with JUnit 4.0 using annotations I understand that it is more like NUnit now.

Mocking frameworks

We were using EasyMock on our project for about half a year and we determined that it was eating up a lot of our time just to do simple tasks. Actually, we made a lot of jokes about how EasyMock is not easy.

After attending a lecture on mocking frameworks, I decided to go with Mockito and have never looked back. It allows partial mocking painlessly - something that EasyMock required a separate library for. Also, Mockito has much better error messaging. When you do something wrong, it will give you verbose errors on how you violated Mockito's contract.

Anyways, give both a spin and I think you will agree that Mockito is a clear winner.

Elijah
+1 for Mockito. It's far better than EasyMock because you can mock with less implementation
furtelwart
+1 for separation of testing and mocking!
azheglov
A: 

We use MbUnit and Rhino.Mocks for testing. I really like it.

The reason we use MbUnit is the Reflector.. Perhaps it can be found in NUnit also, but I found it in MbUnit first ;)

Carl Bergquist
+3  A: 

I would suggest that you take a look at Moq for mocking in .NET. It is conceptually very similar to Mockito on the Java side.

Petter Wigle
+2  A: 

NMock is far from being best mock framework in .NET world. It is string based:

Expect.Once.On(mockView).GetProperty("FromAccount").Will(Return.Value("1234"));

I strongly suggest somethingmore up-to-date like Rhino.Mocks:

Expect.Call(mockView.FromAccount).Returns("1234");
Pawel Lesnikowski
+1  A: 

First of all, it's important to separate the concepts of testing and mocking. For unit-testing (to drive your tests), JUnit (for Java) and NUnit (for .NET) are the most popular choices. The situation around mocking (or isolation) frameworks is more complicated.

Mocking frameworks divide into two camps, which I like to call "conventional" and "alternative."

Conventional frameworks rely primarily on design for testability and dependency injection. Alternative frameworks rely on things like Profiling API or the Java 5 SE instrumentation feature (the java.lang.instrument package) to modify CIL or bytecode at runtime, which makes any dependency mockable.

Examples of such alternative networks are Typemock Isolator (for .NET) and JMockit (for Java) (Typemock is not free unless you use it in an open-source project.)

As for the conventinal mocking frameworks for .NET, the two most popular ones currently are RhinoMocks and Moq. Both rely heavily on C# 3.0 features, particularly the lambda syntax, to allow concise specifications of mock object behaviors and expectations.

azheglov
Great answer! Just a small correction: the JMockit project moved to Google Code last october; the current url is http://code.google.com/p/jmockit.
Rogerio
Rogerio: thank you for the correction! I'm more up-to-date on .NET stuff than on Java.
azheglov