Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to test JPA entity insertion is done?
    primarykey
    data
    text
    <ol> <li><p>The question is about how to test whether persist entity works or not. I think it makes sense to test whether JPA entity mapping is correct and everything works as expected, especially for complex entity mapping with @OneToMany/@ElementCollection/@ManyToMany/@OneToOne with <em>cascade</em> attributes (CascadeType.ALL, for example). And be sure that all FK/PK/Unique constraints are satisfied.</p></li> <li><p>This is the general question. The concrete part is about Spring Testing Framework. You can see on the code below, where DAO layer is testing, but because test method is marked as <em>@Transactional</em>, changes <em>should</em> be rollbacked, here is <em>should</em> instead of <em>must</em>, because how I can be sure, the changes were actually saved in the database at all, probably everything is hold in the memory, because transaction was marked for rollback, so no data is really sent to the database?</p></li> <li><p>More important, that this test is failed, because account.getId() returns 0 (id generation is done using @SequenceGenerator), but it seems that this value is never assigned into the Account's id field. This test is only passed when the method is annotated with @Rollback(false), so of course no rollback occurs.</p></li> <li><p>Am I right, that in this case when @Transactional for testing is used, everything is in the memory (at least, until flush() is not called), so it is not possible to check database constraints working.</p></li> <li><p>And according to the thoughts above, what is the appropriate way to test entity insertion, and be sure that inserting was done, and then rollback everything to the initial state, so to run other tests? (clean up the database inside @Before every time test is executed?)</p></li> <li><p>I don't want to use DBUnit, using Spring there is no sense at all to use DBUnit.</p> <pre><code>@ContextConfiguration( locations = {...}) @RunWith(SpringJUnit4ClassRunner.class) public abstract class JpaRepositoryTest { } public class JpaAccountRepositoryTest extends JpaRepositoryTest { @Inject private AccountRepository accountRepository; @Inject private Account account; @Test @Transactional public void createAccount() { accountRepository.save(account); assertEquals(1, account.getId()); // this assertion will be failed, until @Rollback(false) is used } } </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload