Java 使用spring数据持久性与SpringRunner和JUnit进行集成测试——每个测试的虚拟数据都不同

Java 使用spring数据持久性与SpringRunner和JUnit进行集成测试——每个测试的虚拟数据都不同,java,junit,spring-data-jpa,Java,Junit,Spring Data Jpa,我不确定这个标题是否足够描述性。我正在尝试将虚拟数据插入到数据库中进行测试,我认为应该检查虚拟数据是否已按预期持久化。令我惊讶的是,一个特定的记录并没有持续,所以我试图找出这个记录有什么问题。最后我意识到那张唱片没有什么问题 下面您可以看到,有4个测试断言完全相同的事情。其中1个不及格,3个及格。。。 我想我遗漏了一些基本的东西。下面是测试代码 package testingSpring.rentals.service; import testingSpring.affiliates.Af

我不确定这个标题是否足够描述性。我正在尝试将虚拟数据插入到数据库中进行测试,我认为应该检查虚拟数据是否已按预期持久化。令我惊讶的是,一个特定的记录并没有持续,所以我试图找出这个记录有什么问题。最后我意识到那张唱片没有什么问题

下面您可以看到,有4个测试断言完全相同的事情。其中1个不及格,3个及格。。。

我想我遗漏了一些基本的东西。下面是测试代码

package testingSpring.rentals.service;

import testingSpring.affiliates.Affiliate;
import testingSpring.affiliates.AffiliateDbUtils;
import testingSpring.cars.Car;
import testingSpring.cars.CarDbUtils;
import testingSpring.clients.Client;
import testingSpring.clients.ClientDbUtils;
import testingSpring.rentals.db.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;

@RunWith(SpringRunner.class)
@ComponentScan({
        "testingSpring.cars",
        "testingSpring.rentals.db",
        "testingSpring.clients",
        "testingSpring.rentals.service",
        "testingSpring.affiliates"
})
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class RentalServiceTest {

    @Autowired
    private RentalRepository rentalRepository;

    @Autowired
    private CarDbUtils carDbUtils;

    @Autowired
    private RentalDbUtils rentalDbUtils;

    @Autowired
    private ClientDbUtils clientDbUtils;

    @Autowired
    private RentalService rentalService;

    @Autowired
    private AffiliateDbUtils affiliateDbUtils;

    @Before
    public void setup(){
        addDummyData();
    }



    @Test
    public void dummyDataIsPersisted(){
        assertThat(carDbUtils.count(), equalTo(5L));
        assertThat(clientDbUtils.count(), equalTo(1L));
        assertThat(affiliateDbUtils.count(), equalTo(1L));
        assertThat(rentalDbUtils.count(), equalTo(6L));

    }

    @Test
    public void letsCheckAgainJustForFun(){
        assertThat(carDbUtils.count(), equalTo(5L));
        assertThat(clientDbUtils.count(), equalTo(1L));
        assertThat(affiliateDbUtils.count(), equalTo(1L));
        assertThat(rentalDbUtils.count(), equalTo(6L));
    }
    @Test
    public void letsCheckAgainJustForFun2(){
        assertThat(carDbUtils.count(), equalTo(5L));
        assertThat(clientDbUtils.count(), equalTo(1L));
        assertThat(affiliateDbUtils.count(), equalTo(1L));
        assertThat(rentalDbUtils.count(), equalTo(6L));
    }
    @Test
    public void letsCheckAgainJustForFun3(){
        assertThat(carDbUtils.count(), equalTo(5L));
        assertThat(clientDbUtils.count(), equalTo(1L));
        assertThat(affiliateDbUtils.count(), equalTo(1L));
        assertThat(rentalDbUtils.count(), equalTo(6L));
    }

    private void addDummyData(){

        Car car1 = new Car("1plate1", "datsun", "A", "2000", "manual", "4x4", "green");
        Car car2 = new Car("2plate2", "datsuna", "b", "2000", "manual", "4x4", "yellow");
        Car car3 = new Car("3plate3", "datsuno", "c", "2000", "manual", "4x4", "black");
        Car car4 = new Car("4plate4", "datsuni", "v", "2000", "manual", "4x4", "white");
        Car car5 = new Car("5plate5", "datsunu", "a", "2000", "manual", "4x4", "red");
        carDbUtils.addCar(car1);
        carDbUtils.addCar(car2);
        carDbUtils.addCar(car3);
        carDbUtils.addCar(car4);
        carDbUtils.addCar(car5);


        Client client0 = new Client(0L,"kakakias","trois", "licencenu3","GH92312312",1996,"09801983123","putanga3@grail.com","UK","40 falsefall way");
        clientDbUtils.addClient(client0);

        Affiliate affiliate = new Affiliate("Klasikos Malakas", 0.25F, "4234234");
        affiliateDbUtils.addAffiliate(affiliate);

        Rental rental1 = new Rental(1L, car2,client0, affiliate, "aerobromio", "aerobromio", new Date(2018, 8, 10,12,0), new Date(2018,8,15,11,0), 150L,"gamistronas in","afroditi", PaymentType.CASH, RemainingPetrol.R1_2, "owefowekokfwek1", "no");
        Rental rental3 = new Rental(2L, car3,client0, affiliate, "aerobromio", "aerobromio", new Date(2018, 8, 11,12,0), new Date(2018,8,5,11,0), 150L,"gamistronas in","afroditi", PaymentType.CASH, RemainingPetrol.R1_2, "owefowekokfwek2", "no");
        Rental rental33 = new Rental(3L, car3,client0, affiliate, "aerobromio", "aerobromio", new Date(2018, 8, 20,12,0), new Date(2018,8,30,11,0), 150L,"gamistronas in","afroditi", PaymentType.CASH, RemainingPetrol.R1_2, "owefowekokfwek3", "no");
        Rental rental4 = new Rental(4L, car4,client0, affiliate, "aerobromio", "aerobromio", new Date(2018, 8, 21,12,0), new Date(2018,8,30,11,0), 150L,"gamistronas in","afroditi", PaymentType.CASH, RemainingPetrol.R1_2, "owefowekokfwek4", "no");
        Rental rental5 = new Rental(5L, car5,client0, affiliate, "aerobromio", "aerobromio", new Date(2018, 8, 15,12,0), new Date(2018,8,25,11,0), 150L,"gamistronas in","afroditi", PaymentType.CASH, RemainingPetrol.R1_2, "owefowekokfwek8", "no");
        Rental rental = new Rental(6L, car1,client0, affiliate, "aerobromio", "aerobromio", new Date(2018, 8, 1,12,0), new Date(2018,8,10,11,0), 150L,"gamistronas in","afroditi", PaymentType.CASH, RemainingPetrol.R1_2, "owefoweko654kfwek", "no");
        rentalDbUtils.addRental(rental);
        rentalDbUtils.addRental(rental1);
        rentalDbUtils.addRental(rental3);
        rentalDbUtils.addRental(rental33);
        rentalDbUtils.addRental(rental4);
        rentalDbUtils.addRental(rental5);


    }

}
下面是依赖项

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.2</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/de.svenkubiak/jBCrypt -->
    <dependency>
        <groupId>de.svenkubiak</groupId>
        <artifactId>jBCrypt</artifactId>
        <version>0.4.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

org.springframework.boot
spring引导启动器数据jpa
org.springframework.boot
SpringBootStarterWeb
org.liquibase
液化酶核心
org.postgresql
postgresql
42.2.2
org.springframework.boot
弹簧起动试验
测试
德斯文库比亚克
jBCrypt
0.4.1
朱尼特
朱尼特
测试
org.springframework.boot
springbootmaven插件
请让我知道,如果我需要包括任何其他的描述

谢谢大家!

[编辑] 我用的是一种可以被认为是黑客的工作方法,但暂时它会让我继续下去。 在Junit 4.11及更高版本中,您可以通过使用以下注释测试类来指定一组测试的运行顺序:
@FixMethodOrder(methodsorter.NAME\u升序)

所以一个空方法
@Test public void AAA_WarmUp(){}

将首先运行,然后其他为绿色。 不过我还是很好奇,所以如果有人能对所发生的事情有一个恰当的解释,我将不胜感激。谢谢
[/EDIT]

我唯一想到的是尝试使用Thread.sleep延迟第一次测试。我认为在插入数据和读取数据之间可能会有一些延迟

嗨,谢谢你的回答亚当,我试过了,但没用@在每次测试之前和每次测试之后运行方法之前,db被清除。那么只为第一次工作有意义吗?除非第一个测试是“预热”db或上下文或其他内容。。我没有idea@GiorgosLamprakis您是否也尝试过单独运行有问题的测试?此外,我不认为您的数据库在每次测试后都会被删除。Before为所有测试提供全局状态。我会尝试这些注释——BeforeMethod或Before和AfterMethod或After。尝试使用这些注释。在“批注前”中插入数据库,在“批注后”中清除数据库。我知道他的方法对性能不友好,但可能很好奇这会提供哪些结果。添加adddummydataHi@gaurav的代码,如果您向下滚动第一个代码片段集,您将能够看到该方法。谢谢