fix unique constraint violation in integration tests

This commit is contained in:
Michael Hoennig 2019-04-03 09:55:03 +02:00
parent 625d5295fd
commit f78b92f4cb
4 changed files with 47 additions and 15 deletions

View File

@ -9,7 +9,6 @@ import org.hostsharing.hsadminng.service.ContactService;
import org.hostsharing.hsadminng.service.dto.ContactDTO; import org.hostsharing.hsadminng.service.dto.ContactDTO;
import org.hostsharing.hsadminng.service.mapper.ContactMapper; import org.hostsharing.hsadminng.service.mapper.ContactMapper;
import org.hostsharing.hsadminng.web.rest.errors.ExceptionTranslator; import org.hostsharing.hsadminng.web.rest.errors.ExceptionTranslator;
import org.hostsharing.hsadminng.service.dto.ContactCriteria;
import org.hostsharing.hsadminng.service.ContactQueryService; import org.hostsharing.hsadminng.service.ContactQueryService;
import org.junit.Before; import org.junit.Before;
@ -368,7 +367,7 @@ public class ContactResourceIntTest {
@Transactional @Transactional
public void getAllContactsByRoleIsEqualToSomething() throws Exception { public void getAllContactsByRoleIsEqualToSomething() throws Exception {
// Initialize the database // Initialize the database
CustomerContact role = CustomerContactResourceIntTest.createEntity(em); CustomerContact role = CustomerContactResourceIntTest.createDefaultEntity(em);
em.persist(role); em.persist(role);
em.flush(); em.flush();
contact.addRole(role); contact.addRole(role);

View File

@ -10,7 +10,6 @@ import org.hostsharing.hsadminng.service.CustomerContactService;
import org.hostsharing.hsadminng.service.dto.CustomerContactDTO; import org.hostsharing.hsadminng.service.dto.CustomerContactDTO;
import org.hostsharing.hsadminng.service.mapper.CustomerContactMapper; import org.hostsharing.hsadminng.service.mapper.CustomerContactMapper;
import org.hostsharing.hsadminng.web.rest.errors.ExceptionTranslator; import org.hostsharing.hsadminng.web.rest.errors.ExceptionTranslator;
import org.hostsharing.hsadminng.service.dto.CustomerContactCriteria;
import org.hostsharing.hsadminng.service.CustomerContactQueryService; import org.hostsharing.hsadminng.service.CustomerContactQueryService;
import org.junit.Before; import org.junit.Before;
@ -84,6 +83,7 @@ public class CustomerContactResourceIntTest {
@Before @Before
public void setup() { public void setup() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
final CustomerContactResource customerContactResource = new CustomerContactResource(customerContactService, customerContactQueryService); final CustomerContactResource customerContactResource = new CustomerContactResource(customerContactService, customerContactQueryService);
this.restCustomerContactMockMvc = MockMvcBuilders.standaloneSetup(customerContactResource) this.restCustomerContactMockMvc = MockMvcBuilders.standaloneSetup(customerContactResource)
@ -95,12 +95,12 @@ public class CustomerContactResourceIntTest {
} }
/** /**
* Create an entity for this test. * Create a CustomerContaact entity for the given Customer for testing purposes.
* *
* This is a static method, as tests for other entities might also need it, * This is a static method, as tests for other entities might also need it,
* if they test an entity which requires the current entity. * if they test an entity which requires the current entity.
*/ */
public static CustomerContact createEntity(EntityManager em) { public static CustomerContact crateEnitity(final EntityManager em, Customer customer) {
CustomerContact customerContact = new CustomerContact() CustomerContact customerContact = new CustomerContact()
.role(DEFAULT_ROLE); .role(DEFAULT_ROLE);
// Add required entity // Add required entity
@ -109,16 +109,35 @@ public class CustomerContactResourceIntTest {
em.flush(); em.flush();
customerContact.setContact(contact); customerContact.setContact(contact);
// Add required entity // Add required entity
Customer customer = CustomerResourceIntTest.createEntity(em);
em.persist(customer); em.persist(customer);
em.flush(); em.flush();
customerContact.setCustomer(customer); customerContact.setCustomer(customer);
return customerContact; return customerContact;
} }
/**
* Create an arbitrary CustomerContact entity for tests.
*
* This is a static method, as tests for other entities might also need it,
* if they test an entity which requires the current entity.
*/
public static CustomerContact createDefaultEntity(EntityManager em) {
return crateEnitity(em, CustomerResourceIntTest.createEntity(em));
}
/**
* Create another arbitrary CustomerContact entity for this test.
*
* This is a static method, as tests for other entities might also need it,
* if they test an entity which requires the current entity.
*/
public static CustomerContact createAnotherEntity(EntityManager em) {
return crateEnitity(em, CustomerResourceIntTest.createAnotherEntity(em));
}
@Before @Before
public void initTest() { public void initTest() {
customerContact = createEntity(em); customerContact = createDefaultEntity(em);
} }
@Test @Test
@ -264,12 +283,11 @@ public class CustomerContactResourceIntTest {
defaultCustomerContactShouldNotBeFound("contactId.equals=" + (contactId + 1)); defaultCustomerContactShouldNotBeFound("contactId.equals=" + (contactId + 1));
} }
@Test @Test
@Transactional @Transactional
public void getAllCustomerContactsByCustomerIsEqualToSomething() throws Exception { public void getAllCustomerContactsByCustomerIsEqualToSomething() throws Exception {
// Initialize the database // Initialize the database
Customer customer = CustomerResourceIntTest.createEntity(em); Customer customer = CustomerResourceIntTest.createAnotherEntity(em);
em.persist(customer); em.persist(customer);
em.flush(); em.flush();
customerContact.setCustomer(customer); customerContact.setCustomer(customer);

View File

@ -48,10 +48,12 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
public class CustomerResourceIntTest { public class CustomerResourceIntTest {
private static final Integer DEFAULT_NUMBER = 10000; private static final Integer DEFAULT_NUMBER = 10000;
private static final Integer UPDATED_NUMBER = 10001; private static final Integer ANOTHER_NUMBER = 10001;
private static final Integer UPDATED_NUMBER = 10002;
private static final String DEFAULT_PREFIX = "lzv"; private static final String DEFAULT_PREFIX = "def";
private static final String UPDATED_PREFIX = "zf"; private static final String ANOTHER_PREFIX = "old";
private static final String UPDATED_PREFIX = "new";
@Autowired @Autowired
private CustomerRepository customerRepository; private CustomerRepository customerRepository;
@ -97,7 +99,7 @@ public class CustomerResourceIntTest {
} }
/** /**
* Create an entity for this test. * Create an entity for tests.
* *
* This is a static method, as tests for other entities might also need it, * This is a static method, as tests for other entities might also need it,
* if they test an entity which requires the current entity. * if they test an entity which requires the current entity.
@ -109,6 +111,19 @@ public class CustomerResourceIntTest {
return customer; return customer;
} }
/**
* Create another entity for tests.
*
* This is a static method, as tests for other entities might also need it,
* if they test an entity which requires the current entity.
*/
public static Customer createAnotherEntity(EntityManager em) {
Customer customer = new Customer()
.number(ANOTHER_NUMBER)
.prefix(ANOTHER_PREFIX);
return customer;
}
@Before @Before
public void initTest() { public void initTest() {
customer = createEntity(em); customer = createEntity(em);
@ -350,7 +365,7 @@ public class CustomerResourceIntTest {
@Transactional @Transactional
public void getAllCustomersByRoleIsEqualToSomething() throws Exception { public void getAllCustomersByRoleIsEqualToSomething() throws Exception {
// Initialize the database // Initialize the database
CustomerContact role = CustomerContactResourceIntTest.createEntity(em); CustomerContact role = CustomerContactResourceIntTest.createAnotherEntity(em);
em.persist(role); em.persist(role);
em.flush(); em.flush();
customer.addRole(role); customer.addRole(role);

View File

@ -21,7 +21,7 @@ spring:
type: simple type: simple
datasource: datasource:
type: com.zaxxer.hikari.HikariDataSource type: com.zaxxer.hikari.HikariDataSource
url: jdbc:h2:mem:hsadminNg;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE url: jdbc:h2:mem:hsadminNg;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE
name: name:
username: username:
password: password: