Loading ...
Sorry, an error occurred while loading the content.

mock javax.naming.InitialContext

Expand Messages
  • congzb
    Hello, in my source code, including following code: InitialContext ctx = new InitialContext(); ............=
    Message 1 of 4 , Jun 1, 2011
    • 0 Attachment
      Hello,

      in my source code, including following code:

      InitialContext ctx = new InitialContext();
      ............= (...........)ctx.lookup("java:comp/env/service/someservce");

      so i wonder, in my junit test case, how to mock the InitialContext to look up and return a mock service?
      Thanks.
    • Alistair Israel
      ... The simplest thing I found that worked was to write a StubContext (implements Context) and a StubContextFactoryBuilder (implements
      Message 2 of 4 , Jun 1, 2011
      • 0 Attachment
        On Thu, Jun 2, 2011 at 3:39 AM, congzb <zhebincong@...> wrote:

        > in my source code, including following code:
        >
        >                InitialContext ctx = new InitialContext();
        >                ............= (...........)ctx.lookup("java:comp/env/service/someservce");
        >
        > so i wonder, in my junit test case, how to mock the InitialContext to look up and return a mock service?

        The simplest thing I found that worked was to write a StubContext
        (implements Context) and a StubContextFactoryBuilder (implements
        InitialContextFactoryBuilder) that returns instances of my
        StubContext.

        Not quite a dynamic mock, but I suppose you can use the same
        techniques with a Mockito-synthesized mock Context. I wasn't
        interested in mocking the JNDI context per se, anyway. I just needed a
        convenient way for JNDI client code to be able to resolve names at
        test time.

        Anyway—in my test's setup method I call
        NamingManager.setInitialContextFactoryBuilder() passing it an instance
        of my StubContextFactoryBuilder(). I also added a check to
        hasInitialContextFactoryBuilder() to avoid an exception when
        setInitialContextFactoryBuilder() is called more than once.

        You're welcome to look at StubJndiContext.java on junit-rules for the
        code that packages up the above as a @Rule:
        https://github.com/AlistairIsrael/junit-rules/blob/master/src/main/java/junit/rules/jndi/StubJndiContext.java

        Hope this helps!

        - alistair
        --
        http://alistairisrael.wordpress.com
      • Joakim Ohlrogge
        I would wrap the context behind my own interface and stub/mock/whatever testdouble technique you prefer that. Then I would add just enough system level tests
        Message 3 of 4 , Jun 2, 2011
        • 0 Attachment
          I would wrap the context behind my own interface and stub/mock/whatever
          testdouble technique you prefer that. Then I would add just enough system
          level tests to make sure it works with a JNDI context in place and use my
          own interface in the rest of my code. The instance of my interface would be
          injected something like this:

          ServiceFactory services = new ServiceFactoryStub();
          SomethingIWantToTest subject = new SomethingIWantToTest(services);

          Then you don't have to worry about JNDI anywhere in our code except of
          course in the JNDI service implementation which would look something like
          the code you provided internally.

          When it comes to mocking frameworks, unless you are interested in verifying
          interactions then roll your own stubs. That way your stub code will be in
          one place and not spread out and duplicated all over your tests and become
          an unmaintainable mess of expectations that you don't really care about
          except as a way to make mocks return the proper values for invocations that
          happen to occur in your tests.
          If on the other hand you believe that interactions between roles are
          important to verify then pick a good mock framework like JMock and learn how
          to use it properly. I have not read the book yet but I hear good things
          about Growing Object Oriented systems. I would assume, given who wrote it,
          that you will find everything you need to understand about mock frameworks
          and how to use them properly and efficiently without making a mess.

          I hope this helps some.
          Good luck!


          -----------------------------------------------------
          Joakim Ohlrogge
          Agical AB
          V�sterl�nggatan 79, 2 tr
          111 29 Stockholm, SWEDEN

          Blog: johlrogge.wordpress.com
          Twitter: @johlrogge
          E-mail: joakim.ohlrogge@...


          On Wed, Jun 1, 2011 at 9:39 PM, congzb <zhebincong@...> wrote:

          >
          >
          > Hello,
          >
          > in my source code, including following code:
          >
          > InitialContext ctx = new InitialContext();
          > ............= (...........)ctx.lookup("java:comp/env/service/someservce");
          >
          > so i wonder, in my junit test case, how to mock the InitialContext to look
          > up and return a mock service?
          > Thanks.
          >
          >
          >


          [Non-text portions of this message have been removed]
        • J. B. Rainsberger
          ... Two things. 1. Look for MockEJB, which I used years ago, and does what you want right now. 2. Stop trying to do this as soon as you can. Read these:
          Message 4 of 4 , Jun 4, 2011
          • 0 Attachment
            congzb wrote:

            > InitialContext ctx = new InitialContext();
            > ............= (...........)ctx.lookup("java:comp/env/service/someservce");
            >
            > so i wonder, in my junit test case, how to mock the InitialContext to
            > look up and return a mock service?

            Two things.

            1. Look for MockEJB, which I used years ago, and does what you want
            right now.

            2. Stop trying to do this as soon as you can. Read these:

            http://www.mockobjects.com/2008/11/only-mock-types-you-own-revisited.html
            http://www.davesquared.net/2011/04/dont-mock-types-you-dont-own.html
            http://dotnet.dzone.com/news/tdd-only-mock-types-you-own

            Enjoy.
            --
            J. B. Rainsberger :: http://www.jbrains.ca ::
            http://www.thecodewhisperer.com
          Your message has been successfully submitted and would be delivered to recipients shortly.