【发布时间】:2019-03-29 05:15:36
【问题描述】:
我创建了一个 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() 方法引发重复异常。之前的测试好像没有回滚。
会发生这样的事情:
删除noteService.addNote(note, customer); 行,一切都像魅力一样。
我做错了什么?为什么不保留测试隔离?
【问题讨论】:
-
你试过加
@DirtiesContext吗? -
@dehasi 解决了我的问题。谢谢!
标签: java spring-boot junit spring-test