Page 3 of 3 FirstFirst 123
Results 21 to 24 of 24

Thread: Unit testing - Autowire and Injecting mock

  1. #21
    Join Date
    Jan 2007
    Location
    Washington, DC
    Posts
    11

    Default

    Manco,

    I'm guessing your com.my.TransactionManager class implements some TransactionManagerInterface somewhere in its hierarchy? I'd try to mock that interface instead of the TransactionManager class itself (use com.my.TransactionManagerInterface as the constructor-arg value of the mockTransactionManager bean)...

    A more interesting question is: why do you need to mock the TransactionManager? Is it used directly in the code base?
    In my experience, you need the real TransactionManager instance in the Integration tests, but you don't need any TransactionManager in Unit tests...

  2. #22
    Join Date
    Apr 2007
    Posts
    14

    Default

    thanks for the tip, there was an interface. I originally thought it was being used directly.
    this is a big body of code that I don't know much about and I am learning both spring and mocks. I am attempting to mock out all parts that I can for my first run. Since, unit testing was not an original priority the layering is not quite as clean as could be.

  3. #23
    Join Date
    Apr 2007
    Posts
    14

    Default

    abelsky,

    I have mocked all interfaces, but still there are some instances in the code where
    interfaces aren't used and those classes primarily factories extend from a common
    base class that implements BeanNameAware, which brings me back to my original
    problem. I found the following link http://i-proving.ca/space/Technologi...+Inject-Object Using jMock and Inject-Object, I gave
    it a shot but was unsucccessful, an am not sure if it is a valid way to go.

    Back to your "the more interesting question is". As I said this is a large body of code that I am unfamiliar with. It has some 650 context xml files. The code that I am working on is a Soap Interface. It connects to the backend via an in-house lib that
    connects it to an esb. My code is one of may similar soap clients that use the co.
    in-house soap-interface lib. The code was not developed using Unit Testing, which
    is why this is not running smoothly, I am not in a position to change the code base.
    Essentially the code translates the in-house xml req to the soap req and soap rsp
    to in-house rsp. The TransactionManager is used to deal with async rsps. My goal
    is to perform a limited integration test, no esb, mock out the txmngr , db access. inject my
    code with the xml req, let it run out to a test soap-server/soapUI and compare
    actual in-house xml rsp to expected.

    Thanks for your help

  4. #24
    Join Date
    Jan 2007
    Location
    Washington, DC
    Posts
    11

    Default

    Hey Manco,

    Here are a couple of thoughts... I don't know the intricate architectural details of your solution, so some of them may not be applicable to your situation.

    #1 Since you seem to be set on using mocks in integration tests, I'd recommend giving Mockito a shot (there are some examples in this thread). It's less restrictive, so it will work in your BeanNameAware situation. The setBeanName() calls will be recorded by Mockito-based mocks, but this won't fail the test, and you won't even need to setup any expectations for this to happen).

    #2 For integration tests, I always try to instantiate the Spring container without any mocks. It might take some time to instantiate a Spring context with 650 context files, but each subsequent test will inherit it and will run a lot faster. It's usually the easiest solution, and the side benefit is you'll validate the full context with all of its wired dependencies every time it runs.
    If I have to provide mocks in integration tests, I try to get away with as few mocks as possible. If I have any JNDI dependencies, I override them by providing these required resources locally (such as data sources, etc). You may, for example, need to mock the ESB proxy if you want to remove the ESB dependency for the integration tests.
    Once you're able to instantiate your Spring container in your integration test, you should be able to cover your test scenario - give it an xml request, hit the SoapUI mock service, compare the result, etc. The transaction rollback will roll back database changes (if any) at the end.

    #3 Sometimes it makes sense to write a unit test instead, mocking all of its dependencies and verifying the functionality of that class in isolation. In case of a SOAP client, you could still provide a SoapUI mock service for it to hit. Since this is done without any Spring context, this approach should work for you as well.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •