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

时间:2018-09-08 20:36:37

标签: java junit spring-data-jpa

我不确定标题是否具有足够的描述性。我试图将虚拟数据插入数据库以进行测试,我认为我应该检查虚拟数据是否已按预期保存。令我惊讶的是,某个特定的记录没有持久化,因此我试图找出该记录的问题。最后,我意识到该特定记录没有错。

下面您可以看到有4个测试断言完全相同的事物。其中1个失败3个通过... enter image description here

enter image description here

我认为我在这里缺少一些基本的东西。贝娄是测试代码

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>

如果需要在此说明中添加其他内容,请告诉我。

谢谢!

[编辑] 我正在使用一种变通办法,可以将其视为一种hack,但暂时它会让我继续。 在Junit 4.11及更高版本中,您可以通过以下方式注释测试类来指定一组测试的运行顺序: @FixMethodOrder(MethodSorters.NAME_ASCENDING)

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

将首先运行,然后其他运行为绿色。 不过,我仍然很好奇,因此,如果有人对发生的事情有正确的解释,我将不胜感激。谢谢 [/ EDIT]

1 个答案:

答案 0 :(得分:0)

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