简体   繁体   中英

Integration testing using spring data persistance with SpringRunner and JUnit - dummy data differ for each test

I am not sure if the title is descriptive enough. I am trying to insert dummy data to my database for testing purposes and I thought I should check that the dummy data has been persisted as expected. To my surprise a particular record was not persisting, so I tried to to find what is wrong with that record. In the end I realized that there is nothing wrong with that particular record.

Bellow you can see the that there are 4 tests asserting exactly the same thing. 1 of them is failing 3 are passing... 在此处输入图片说明

在此处输入图片说明

I think I am missing something fundamental here. Bellow is the test code

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);


    }

}

Bellow is the dependencies

<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>

Please let me know if I need to include anything else to this description.

Thank you!

[EDIT] I am using a workaround which can be consider as a hack, but for the time being it will let me proceed. In Junit 4.11 and later you can specify the order a set of test will run by annotating the test class with: @FixMethodOrder(MethodSorters.NAME_ASCENDING)

so a an empty method @Test public void AAA_WarmUp(){}

will run first and then the others are green. I am still curious though, so if someone has a proper explanation of what's going on I will be grateful. Thanks [/EDIT]

Only thing that comes to my mind is to try to delay the first test using Thread.sleep. I think that there might be some delay between inserting data and reading them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM