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

问题描述 投票:2回答:1

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

Bellow你可以看到有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","[email protected]","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>

如果我需要在此说明中包含任何其他内容,请与我们联系。

谢谢!

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

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

将首先运行,然后其他是绿色。我仍然很好奇,所以如果有人对正在发生的事情有正确的解释,我将不胜感激。谢谢[/编辑]

java junit spring-data-jpa
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.