tags:

views:

90

answers:

3

I am trying to use Jmockit API for indepentant use with out any framework for our incremental development. But I am not able to initialize the proxy for mocked class

So I have two question.

  1. Can we use JMockIt for independantly of any test framework.
  2. If yes how to use it

Thanks, Ashish

A: 

you can use it to stubbing your data access layer (DAO, repositories, etc).

My mock framework of choice is Mockito. For example you can mock a DAO like this:

interface UserDAO {

    boolean userExists(String user);
    ...
}

UserDAO userDAO = mock(UserDAO.class);
when(userDAO.userExists("dfa")).thenReturn(true);
when(userDAO.userExists("root")).thenReturn(false);

then you can pass around your new stub as it is implemented

dfa
JMockit has none of the limitations Mockito has, though.
Rogerio
can you provide examples please?
dfa
+1  A: 

Yes, you can use most mocking APIs without any test framework.

All the mocking APIs do, in their simple interface mocking operation, is create dynamic proxies (which are part of core Java) that intercept the method calls and respond based on the expectations you have set.

It's a little more complicated when you are mocking classes as byte code manipulation takes place to allow the method call interception but after that it's the same 'respond based on the expectations' scenario. Obviously if you are mocking classes you need to include whatever byte code manipulation API JMockit uses.

Nick Holt
JMockit only uses the standard java.lang.instrument package and a version of ASM (a bytecode manipulation library), which is embedded in jmockit.jar.
Rogerio
+1  A: 

The answer to this question is both yes and no.

Yes, the JMockit Annotations API (for state-based testing) does work fine if called directly from a "main" method.

No, the JMockit Expectations API (for behavior-based testing) currently does not work if called directly from "main". This happens because it keeps track of the current test being executed by the test runner, which can be a JUnit 3.8, a JUnit 4.5+, or a TestNG 5.8+ runner. So, when not running in the context of a testing framework (JUnit or TestNG), it lacks the necessary test tracking information.

JMockit Expectations could be changed, though. But what would be the point of running a test suite that is neither written with JUnit nor with TestNG, or with any other test framework (if one existed)? Or do you want to use it for something entirely different from developer testing?

Rogerio