Package com.axelor.db

Class JpaFixture


  • public class JpaFixture
    extends Object
    This class can be used to load test data for JPA entities.

    It processes YAML files located in /fixtures.

    For example, the following schema:

    
     @Entity
     @Table(name = "CONTACT_CIRCLE")
     public class Circle extends Model {
         private String code;
         private String name;
         ...
         ...
     }
    
     @Entity
     @Table(name = "CONTACT_CONTACT")
     public class Contact extends Model {
         private String firstName;
         private String lastName;
         private String email;
    
         @ManyToMany
         private Set<Circle> circles;
         ...
         ...
         ...
     
    The fixtures should be defined like this:
      - !Circle: &family
       code: family
       name: Family
    
     - !Circle: &friends
       code: friends
       name: Friends
    
     - !Circle: &business
       code: business
       name: Business
    
     - !Contact:
       firstName: John
       lastName: Smith
       email: j.smith@gmail.com
       circles:
         - *friends
         - *business
    
     - !Contact:
       firstName: Tin
       lastName: Tin
       email: tin.tin@gmail.com
       circles:
         - *business
     

    In order to use the fixture data, the JpaFixture must be injected.

     @RunWith(GuiceRunner.class)
     @GuiceModules({MyModule.class})
     class FixtureTest {
    
         @Inject
         private JpaFixture fixture;
    
         @Before
         public void setUp() {
             fixture.load("demo-data.yml");
             fixture.load("demo-data-extra.yml");
         }
    
         @Test
         public void testCount() {
             Assert.assertEqual(2, Contact.all().count());
             ...
         }
         ...
     }
     
    • Constructor Detail

      • JpaFixture

        public JpaFixture()
    • Method Detail

      • load

        public void load​(String fixture)