mercredi 17 août 2016

Expecting result in test with spring bean

Tere are bean transactional bean witn method

public class Bean {
     @Transactional(propogation = REQUARED) // propogation set for read clarity 
     public save(User user);

     @Transactional(propogation = REQUARED)
     public List<User> getUsers(); // implementation - 
                                          // native query - select * users;
                                          // with User.class
}

Test class:

class Test {

     @Autovired
     private Bean bean;

     public void test () {
          User user = getNewUser();
          bean.save(user);
          Assert.assertTrue(bean.getUsers().size() = 1); //False because
           //transaction do not commit 
           //but try it read data by sql query 
           //(not from persistence context)    
     }
}

There are annotation @AfterTransaction to resolve issue above.

@AfterTransaction 
public void check() {
    Assert.assertTrue(bean.getUsers().size() = 1); //True
}

But if other way to do it. Because @AfterTransaction one for test class but we can have many test methods with checks

Aucun commentaire:

Enregistrer un commentaire