Skip to content

noRollback test code in Chapter 11 #5

@mklinkj

Description

@mklinkj

hello.

Java Persistence with Spring Data and Hibernate has been published in Korea, so I am reading it well.

Among the example tests in Chapter 11, there is a noRollback test method as follows, but I am inquiring because it does not seem to be a test of the operation of noRollback.

@Test
public void noRollback() {
// no rollback - log message is persisted in the logs even after exception
// because transaction was not rolled back
itemRepository.addItemNoRollback("Item1", LocalDate.of(2022, 5, 1));
itemRepository.addItemNoRollback("Item2", LocalDate.of(2022, 3, 1));
itemRepository.addItemNoRollback("Item3", LocalDate.of(2022, 1, 1));
DuplicateItemNameException ex = assertThrows(DuplicateItemNameException.class, () -> itemRepository.addItem("Item2", LocalDate.of(2016, 3, 1)));
assertAll(
() -> assertEquals("Item with name Item2 already exists", ex.getMessage()),
() -> assertEquals(4, logRepository.findAll().size()),
() -> assertEquals(3, itemRepository.findAll().size())
);

Since noRollback is set in ItemRepository#addItemNoRollback() -> LogRepository#log(), it seems that there must be a scenario where duplication occurs when calling addItemNoRollback().

In the case of ItemRepository#addItem() in the middle, it seems to have no relation to setting noRollback because it is set to REQUIRED_NEW.

So I thought about the following test code:

 @Test 
 public void noRollback() { 
     itemRepository.addItemNoRollback("Item1", LocalDate.of(2022, 5, 1)); 
     itemRepository.addItemNoRollback("Item2", LocalDate.of(2022, 3, 1)); 
     
     DuplicateItemNameException ex = assertThrows(DuplicateItemNameException.class, () -> itemRepository.addItemNoRollback("Item2", LocalDate.of(2022, 1, 1))); 
     assertAll( 
             () -> assertEquals("Item with name Item2 already exists", ex.getMessage()), 
             () -> assertEquals(3, logRepository.findAll().size()), 
             () -> assertEquals(2, itemRepository.findAll().size()) 
     ); 
     // ...

Then, I will finish reading the book. I've read up to chapter 11 now..😅

thank you have a good day. 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions