Джунит разный в Eclipse / Maven - PullRequest
0 голосов
/ 02 июля 2018

У меня есть базовое приложение SpringBoot. используя Spring Initializer, JPA, встроенный Tomcat, шаблонизатор Thymeleaf и пакет в качестве исполняемого файла JAR. У меня есть этот класс:

@Entity
@Table(name="t_menu_alert")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "menu_type")
    public class MenuAlert implements Serializable {

        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @JsonProperty("id")
        private Long id;    

        ...
    }

и

@Entity
@DiscriminatorValue("price")
public class PriceAlert extends MenuAlert {
..
}

и

public class PriceAlertXResto  implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonProperty("id")
    private long id;

    public PriceAlertXResto() {
        super();
    }

    public PriceAlertXResto(PriceAlert priceAlert, Resto resto) {
        super();
        this.resto = resto;
        this.priceAlert = priceAlert;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "resto_id")
    @JsonProperty("resto")
    private Resto resto;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "price_alert_id")
    @JsonProperty("priceAlert")
    private PriceAlert priceAlert;
    ...
}

и это тест Junit (у меня есть только 1 метод):

@ContextConfiguration(classes={TestSystemConfig.class})
@RunWith(SpringRunner.class)
@SpringBootTest(classes = CanPerisApplication.class) 
@Transactional
public class PriceAlertXResoServiceTests {

    @Test
        public void testSave () {

            PriceAlert priceAlert = new PriceAlert();
            priceAlert.setName("PRICE_ALERT_TEST_NAME");

            priceAlertService.save(priceAlert);

            String restoId = System.currentTimeMillis()+"";

            Resto resto = new Resto(restoId);
            restoService.save(resto);

            priceAlert = priceAlertService.findByName("PRICE_ALERT_TEST_NAME");

            assertNotNull(priceAlert);

            PriceAlertXResto par = new PriceAlertXResto(priceAlert, resto);

            priceAlertXRestoService.save(par);
    }
}

Когда я запускаю тест из Eclipse, все в порядке, но когда я запускаю тест, используя команду maven mvn clean package У меня появляется эта ошибка

org.springframework.dao.DataIntegrityViolationException: 
could not execute statement; SQL [n/a]; constraint ["FKNM9YKG691CALX9LADL0QKQK7L: PUBLIC.T_PRICE_ALERT_RESTO FOREIGN KEY(PRICE_ALERT_ID) REFERENCES PUBLIC.T_MENU_ALERT(ID) (2)"; SQL statement:
insert into t_price_alert_resto (id, resto_id, price_alert_id) values (null, ?, ?) [23506-197]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

Я использую в памяти БД: spring.datasource.url=jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...