spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
74.76k stars 40.59k forks source link

Integration tests failing after 2.0.5.RELEASE #14606

Closed wesleyody closed 6 years ago

wesleyody commented 6 years ago

When I run my integration tests it's giving me the error below on the moment that tries to insert something on the "database":

ERROR: relation "subscription" does not exist

subscription is the name of my table that I'm trying to insert

But when I just run my application it is working fine. It seems that is creating an empty database. With 1.5.4.RELEASE works fine

My configuration:

spring.datasource.platform=postgres
spring.datasource.dbcp.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL95Dialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.open-in-view=true

spring.flyway.enabled=true
spring.flyway.out-of-order=true
spring.flyway.validate-on-migrate=false
spring.flyway.table=schema_version
@RunWith( SpringJUnit4ClassRunner.class )
@SpringBootTest
@EnableJpaRepositories( repositoryBaseClass = BaseRepositoryImpl.class )
public class ApplicationIT {
@RunWith( SpringJUnit4ClassRunner.class )
@WebAppConfiguration
@Transactional
public class TestIT {

    private MockMvc mockMvc;
    @Resource
    private WebApplicationContext webApplicationContext;

    @Before
    public void setup () {
        mockMvc = MockMvcBuilders
            .webAppContextSetup( webApplicationContext )
            .apply( SecurityMockMvcConfigurers.springSecurity() )
            .build();
    }
}
wilkinsona commented 6 years ago

Thanks for the report. Unfortunately, you haven't provided enough information for us to be able to help you. It's not clear why you expect to be creating the subscription table. Is it Flyway, Hibernate, or something else?

If you would like us to spend some time trying to help you, please provide a minimal sample application that works with 1.5 and fails with 2.0.

wesleyody commented 6 years ago

The table it's already created but when I run the test it's not finding it. I'm using Flyway.

BaseStructure:

@Entity
public class Subscription {

    @Id
    @GeneratedValue( strategy = GenerationType.IDENTITY )
    private Integer id;
    private String name;

    // getters
}

public interface SubscriptionRepository extends JpaRepository< Subscription, Integer > {
}

@Service
public SubscriptionService {
    @Transactional
    public Subscription save ( Subscription subscription ) {
            return subscriptionRepository.saveAndFlush( subscription );
    }
}

Test:

public class BaseRepositoryImpl< T, ID extends Serializable >
    extends SimpleJpaRepository< T, ID >
    implements BaseRepository< T, ID > {

    private EntityManager entityManager;

    public BaseRepositoryImpl ( JpaEntityInformation< T, ? > entityInformation, EntityManager entityManager ) {
        super( entityInformation, entityManager );
        this.entityManager = entityManager;
    }

    @Override
    public T refresh ( T entity ) {
        entityManager.refresh( entity );
        return entity;
    }

    @Override
    public < C extends Collection< T > > C refresh ( C entities ) {
        for ( T entity : entities ) {
            refresh( entity );
        }

        return entities;
    }
}

@Configuration
@ComponentScan( lazyInit = true )
public class ApplicationConfig {

    @Configuration
    @Profile( "default" )
    @PropertySource( "classpath:application.properties" )
    static class Default {
    }
}

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( classes = { ApplicationConfig } )
@WebAppConfiguration
@Transactional
public class TestIT {

    private MockMvc mockMvc;
    @Resource
    private WebApplicationContext webApplicationContext;

    @Resource
    private SubscriptionService subscriptionService;

    @Before
    public void setup () {
        mockMvc = MockMvcBuilders
            .webAppContextSetup( webApplicationContext )
            .apply( SecurityMockMvcConfigurers.springSecurity() )
            .build();

        Subscription subscription = new Subscription();
        subscription.setName( "foo" );
        subscriptionService.save( subscription );
    }
}

The same code works with 1.5

wilkinsona commented 6 years ago

Thanks, but some code snippets in a comment aren't really what we need. Can you please turn those into a minimal sample application (something that we can git clone or unzip) that reproduces the problem. We'll also need details of how to setup any dependencies your application has. Ideally, a minimal sample should not have any unless they are strictly necessary to reproduce the problem.

wesleyody commented 6 years ago

It was a problem created by https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#updated-default-create-drop-handling

It's all good thanks