1 Reply Latest reply on Aug 20, 2010 9:49 AM by Rafael Ribeiro

    jMock injection

    Alexandre Gattiker Newbie

      The reference documentation has a paragraph on injecting mock classes. Is there a way to inject dynamic mocks like those created with jMock?

      Here's an example dynamic mock that I would like to inject. Unwrapping all that logic to a full-blown class would be much less concise.

      Mockery context = new Mockery();
      final OntologyDataFactory factory = context.mock(OntologyDataFactory.class);
      context.checking(new Expectations() {
                atLeast(1).of(factory).createOntologySource(null, source);
                          with(ontologySource), with(any(String.class)),

        • 1. Re: jMock injection
          Rafael Ribeiro Novice

          Sorry for not answering that fast but... it might help others...
          we found a solution here that works fine...
          Suppose you have a CustomerDAO in your src/main folder under com.my.customer.CustomerDAO...
          Code a MockCustomerDAO on your src/test folder under the same package as follows

          public class MockCustomerDAO {
            public static CustomerDAO customerDAO;
            public CustomerDAO getCustomerDAO() {
               return customerDAO;

          The tricks are:

          1. Dont annotate the Mock class otherwise if you are using WTP and run it directly you'll get your mock class instead of your real class

          2. declare a static field and let seam inject this instance instead of the Mock Class using the @Unwrap annotation

          The next step is to configure the mock component with the same name of the real annotated component on the components.xml under src/test/resources with the mock precedence of 40.

          This solution follows the premise that your test suite will load the components.xml under src/test/resources/WEB-INF and your development Tomcat on WTP will use src/main/resources.

          Hope this helps,
          Rafael Ribeiro