7 Replies Latest reply on Apr 30, 2012 3:07 PM by gonzalad

    arq-persistence : dataset duplication in dataset.yml and in java

    gonzalad

      Hello,

       

      I've just began using arquillian persistence.

       

      When using datasets, I'm ending duplicating [1] my datasets in the yml file and in my unit test in order to verify the test result.

       

      Is there a way to avoid code duplication (perhaps reading myself YamlDataSet but this approach doesn't seem clean) ?

       

      Thanks very much !

       

      [1] sample code - I'm duplicating my charge-clientele.yml inside my UT class:

       

      @RunWith(Arquillian.class)
      @Transactional(TransactionMode.ROLLBACK)
      @Cleanup(strategy=CleanupStrategy.USED_ROWS_ONLY)
      @UsingDataSet("charge-clientele.yml")
      public class ChargeClienteleManagerTest {
      
        private List<ChargeClientele> dataset = new ArrayList<ChargeClientele>();
      
        @Test
        public void testFindById() {
          ChargeClientele chargeClientele = chargeClienteleManager
              .findById(dataset.get(0).getId());
          assertNotNull(chargeClientele);
          assertEquals("id invalide", dataset.get(0).getId(),
              chargeClientele.getId());
          assertEquals("nom invalide", dataset.get(0).getNom(),
              chargeClientele.getNom());
          assertEquals("prenom invalide", dataset.get(0).getPrenom(),
              chargeClientele.getPrenom());
          assertEquals("code etab invalide", dataset.get(0)
              .getCompanyCode(), chargeClientele.getCompanyCode());
          assertEquals("racf invalide", dataset.get(0).getUserRACF(),
              chargeClientele.getUserRACF());
        }
      
      
        @Before
        public void setUp() throws Exception {
          ChargeClientele chargeClientele = new ChargeClientele();
          chargeClientele.setId(160L);
          chargeClientele.setCompanyCode("000");
          chargeClientele.setNom("JUNITNOM1");
          chargeClientele.setPrenom("JUNITPRENOM1");
          chargeClientele.setUserRACF("junit1");
          dataset.add(chargeClientele);
      
      
          chargeClientele = new ChargeClientele();
          chargeClientele.setId(161L);
          chargeClientele.setCompanyCode("000");
          chargeClientele.setNom("JUNITNOM2");
          chargeClientele.setPrenom("JUNITPRENOM2");
          chargeClientele.setUserRACF("junit2");
          dataset.add(chargeClientele);
      
      
          chargeClientele = new ChargeClientele();
          chargeClientele.setId(162L);
          chargeClientele.setCompanyCode("000");
          chargeClientele.setNom("JUNITNOM3");
          chargeClientele.setPrenom("JUNITPRENOM3");
          chargeClientele.setUserRACF("junit3");
          dataset.add(chargeClientele);
        }
      }
      

       

      Sample yml :

      ChargeClientele:
        - id: 160
          companyCode: 000
          nom: JUNITNOM1
          prenom: JUNITPRENOM1
          userRACF: junit1
        - id: 161
          companyCode: 000
          nom: JUNITNOM2
          prenom: JUNITPRENOM2
          userRACF: junit2
        - id: 162
          companyCode: 000
          nom: JUNITNOM3
          prenom: JUNITPRENOM3
          userRACF: junit3
      
        • 1. Re: arq-persistence : dataset duplication in dataset.yml and in java
          bmajsak

          Hi Gonzalad,

           

          just to be sure that I understand your use case correctly - why would you need to validate created data set in your test? The idea of data set is to define your data needed to be inserted to your test database outside of your test class, so you don't need to declare entities directly in the code.

          • 2. Re: arq-persistence : dataset duplication in dataset.yml and in java
            david.salter

            I'm not sure of the purpose of your setup() method.  If you are using the @UsingDataSet annotation, that should load your data for you into the test database and you shouldn't need to load it yourself.

             

            Is this not happening?

            • 3. Re: arq-persistence : dataset duplication in dataset.yml and in java
              gonzalad

              Hi,

               

              I need to validate that data returned by my findById method is correct (in other words, that my entity is populated correctly).

               

              For instance, if my table contains a column companyCodewith with value '000', I want to verify that the returned entity contains 000.

              • 4. Re: arq-persistence : dataset duplication in dataset.yml and in java
                bmajsak

                I would rather assume that data is there already, inserted by the extension itself. Then I would simply assert the values straight in the test code. You have total control on what you are inserting so I believe such approach is self explanatory.

                • 5. Re: arq-persistence : dataset duplication in dataset.yml and in java
                  gonzalad

                  > I would rather assume that data is there already, inserted by the extension itself.

                  Yep, that's what I'm doing (I'm sorry my sample wasn't really clear ;( ).

                   

                  > Then I would simply assert the values straight in the test code.

                  This is exactly my problem : if I insert value '000' with my dataset file (yml), I need to duplicate this same value in my JUnit assertion.

                   

                  e.g.

                  ChargeClientele:
                    - id: 160
                      companyCode: 000

                   

                  Then :

                  @Test
                    public void testFindById() {
                      ChargeClientele chargeClientele = chargeClienteleManager.findById(dataset.get(0).getId());
                      assertEquals("000", chargeClientele.getCompanyCode());

                   

                  I can do with it but perhaps there's a nicer way (i.e. a dataset API ?) - or perhaps just using @ShouldMatchDataSet is the way to go (but does it work with an already populated database) ?

                   

                  Sorry for my newbie questions

                  • 6. Re: arq-persistence : dataset duplication in dataset.yml and in java
                    bmajsak

                    All questions are more than welcome - they help us to focus on what's really important for end users like you.

                     

                    If you want to test if your query works properly I would stick to plain assertions veryfying values, like the one you have included above.

                    Providing "injection" of data sets is doable but I don't really see a huge gain of having it comparing to "classical approach" using plain assertions. I'm quite fine with this kind of "duplication" - you do it as well when you mock other components in your unit tests, aren't you?

                     

                    @ShouldMatchDataSet on the other hand is useful what your logic under test is modifying database state which might be a bit cumbersome to verify in the test code itself (for instance a lot of changes in the db). Then you provide dataset which content is used to match current state of the tables.

                    • 7. Re: arq-persistence : dataset duplication in dataset.yml and in java
                      gonzalad

                      Thanks you very much for your insight Bartosz !

                       

                      I'll stick to plain assertions.