Results 1 to 7 of 7

Thread: Repository layer and integration test

  1. #1
    Join Date
    Mar 2008
    Posts
    249

    Default Repository layer and integration test

    Hello,

    I'm trying to implement an integration test against an entity handled by JPA and a repository layer.

    I did a reverse engineering of a database schema and then created the repository for each entity.

    Creating the entities from a reversed engineered database schema:

    Code:
    database reverse engineer --package ~.domain --schema PUBLIC --testAutomatically --activeRecord false
    Creating a repository for each entity of the schema:

    Code:
    repository jpa --interface com.learnintouch.lms.data.domain.AddressRepository --entity com.learnintouch.lms.data.domain.Address
    Since the schema contains 117 tables I created an equal number of repositories with the repository jpa above command.

    Question 1: Is this the way to proceed to have a repository layer and avoid the active record pattern ?

    I then tried to create an integration test:

    Code:
    	@Test
    	@Transactional
    	public void testSaveAndRetrieve() {
    		AddressDataOnDemand dod = new AddressDataOnDemand();
    		Address address = dod.getRandomAddress();
    		assertNotNull(address.getId());
    		addressRepository....
            }
    Question 2: How to expose a reference to the repository in the integration test ?

    I could not find the answer in the Spring Roo reference manual nor in the Spring Roo In Action book.
    Stephane

  2. #2
    Join Date
    Jun 2008
    Location
    Philadelphia, PA, USA
    Posts
    212

    Default

    Section 9.4.2 is your answer. Inject the repository using @Autowired, and make sure to make the tests @Transactional. Then you can use a @Before method (they are inside the transaction) to set up test data. Your transaction will automatically be rolled back.

    The generated Spring Roo integration tests are useful if only to get you a test context, and to test the crud. If you have specific data validation that is tough to test using the generated code, you can push in the breaking method. You can add your individual specific tests to the Java source of he integration test. And you also get an entity manager.

    However, the problem might be the DataOnDemand class. There are a feet methods in that ITD that you can push in, ones that generate fake data.

    Above all, Spring Roo is a Spring platform. So anything you can do in Spring you can do there. Hence 9.4.2 shows how to create a Java test class and write one yourself.
    Ken Rimple
    Chariot Solutions
    email: krimple@chariotsolutions.com
    work: www.chariotsolutions.com/education
    personal: www.rimple.com

    Author: Spring Roo in Action (Manning)
    MEAP Site: manning.com/rimple

  3. #3
    Join Date
    Mar 2008
    Posts
    249

    Default

    Okay, I didn't know if I had to make use of some Roo high chaparal magic :-)

    So, I'll do like I did with my old standard Dao and use the @Autowired annotation like you suggested.

    Thanks for the kind comment.
    Stephane

  4. #4
    Join Date
    Mar 2008
    Posts
    249

    Default

    I'm not sure I'm supposed to inject the repository with the @Autowired annotation.

    This gives an error at compile time with Maven saying there is a conflict of member variables between the one in the test class and the one in the test aspect class.

    In the AddressIntegrationTest_Roo_IntegrationTest class there is:

    @Autowired
    AddressRepository AddressIntegrationTest.addressRepository;
    And in the AddressIntegrationTest class there is:


    @Autowired
    AddressRepository addressRepository;
    And the compile does not like it.
    Stephane

  5. #5
    Join Date
    Jun 2008
    Location
    Philadelphia, PA, USA
    Posts
    212

    Default

    I was discussing how to build your own integration test. If you are trying to use an existing repository integration test ( with @RooIntegrationTest) then the ITD already injects it. That is if you created automated repositories for your entities with the repository creation command.

    You can add your own methods to the Roo integration tests. Just add them to the Java class. In that one, the ITD makes all test methods transactional anyway.
    Ken
    Ken Rimple
    Chariot Solutions
    email: krimple@chariotsolutions.com
    work: www.chariotsolutions.com/education
    personal: www.rimple.com

    Author: Spring Roo in Action (Manning)
    MEAP Site: manning.com/rimple

  6. #6
    Join Date
    Jun 2008
    Location
    Philadelphia, PA, USA
    Posts
    212

    Default

    One more thing. If you created Roo services for your entities and Roo repositories, then Roo will test at the service level. The ITD should have a reference to the service but not the repo at that point.
    Ken Rimple
    Chariot Solutions
    email: krimple@chariotsolutions.com
    work: www.chariotsolutions.com/education
    personal: www.rimple.com

    Author: Spring Roo in Action (Manning)
    MEAP Site: manning.com/rimple

  7. #7
    Join Date
    Mar 2008
    Posts
    249

    Default

    Hi Ken,

    Thanks for the sharing ! Indeed I had removed the @Autowired annotation and the member variable for the repository from the test class and the conflict disappeared.

    Only, now I'm fighting with a memory issue as my schema seems to have too many tables for my Maven build.

    But that's another thread... http://forum.springsource.org/showth...d=1#post426888
    Last edited by stephaneeybert; Oct 8th, 2012 at 02:56 PM.
    Stephane

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
  •