Я создал тест SpringBoot:
@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(locations = "classpath:application-dev.properties")
@Transactional
public class ContactTests2 {
private Logger log = LogManager.getLogger();
@PersistenceContext
private EntityManager entityManager;
@Autowired
private ContactRepository customerRepository;
@Autowired
private StoreRepository storeRepository;
@Autowired
private NoteService noteService;
@Autowired
private Validator validator;
private Store store;
@Before
@WithMockUser(roles = "ADMIN")
public void setup() {
log.debug("Stores {}", storeRepository.count());
store = createStore();
storeRepository.save(store);
}
@Test
@WithMockUser(roles = "ADMIN")
public void saveWithNote() {
Contact customer = new Contact();
customer.setPersonType(PersonType.NATURAL_PERSON);
customer.setFirstName("Daniele");
customer.setLastName("Rossi");
customer.setGender(Gender.MALE);
customer.setBillingCountry(Locale.ITALY.getCountry());
customer.setShippingCountry(Locale.ITALY.getCountry());
customer.setStore(store);
Note note = new Note();
note.setGenre(NoteGenre.GENERIC);
note.setOperationType(AuditType.NOTE);
note.setText("note");
customer = customerRepository.save(customer);
noteService.addNote(note, customer);
}
@Test
@WithMockUser(roles = "ADMIN")
public void save() {
Contact customer = new Contact();
customer.setPersonType(PersonType.NATURAL_PERSON);
customer.setFirstName("Daniele");
customer.setLastName("Rossi");
customer.setGender(Gender.MALE);
customer.setBillingCountry(Locale.ITALY.getCountry());
customer.setShippingCountry(Locale.ITALY.getCountry());
customer.setStore(store);
customerRepository.save(customer);
assertEquals(customer, customerRepository.findById(customer.getId()).get());
}
// ====================================================
//
// UTILITY METHODS
//
// ====================================================
private Store createStore() {
Store store = new Store();
store.setName("Padova");
store.setCode("PD");
store.setCountry("IT");
return store;
}
}
Это служба заметок:
@Service
@Transactional
@PreAuthorize("isAuthenticated()")
public class NoteService {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private NoteRepository noteRepository;
/**
* Add a note to a specific object (parent).
*
* @param note
* @param parent
* @return the added note
*/
public Note addNote(Note note, Persistable<Long> parent) {
// ****************************************************
// VALIDATION CHECKS
// ****************************************************
Assert.notNull(note, InternalException.class, ExceptionCode.INTERNAL_ERROR);
Assert.notNull(parent, InternalException.class, ExceptionCode.INTERNAL_ERROR);
// ****************************************************
// END VALIDATION CHECKS
// ****************************************************
note.setParentId(parent.getId());
note.setParentType(parent.getClass().getSimpleName());
note.setRemoteAddress(NetworkUtils.getRemoteIpFromCurrentContext());
note = noteRepository.save(note);
return note;
}
}
Я использую Hibernate и Mysql 5.7.Проблема в том, что тест называется saveWithNote()
.Когда я запускаю этот тест, следующие тесты не выполняются, потому что метод setup()
создает дублированное исключение.Кажется, что предыдущий тест не откат.
Вот что происходит:
![enter image description here](https://i.stack.imgur.com/cOZcH.png)
Удаление строки noteService.addNote(note, customer);
все работает какОчарование.
Что я делаю не так?Почему тестовая изоляция не сохранилась?