2

I'm trying to set up a meta-annotation for some integration tests. This test works fine.

@Test
@SqlGroup( {
        @Sql(scripts = {"classpath:test/sql/customers/customers.sql",
                "classpath:test/sql/orders/order_addresses.sql",
                "classpath:test/sql/orders/order_contact_info.sql",
                "classpath:test/sql/orders/orders.sql",
                "classpath:test/sql/resetKeys.sql"}),
        @Sql(scripts =  {"classpath:test/sql/orders/delete_orders.sql",
                "classpath:test/sql/customers/delete_customers.sql"}, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
})
public void testAddItemToOrder() throws Exception {
    logger.entry();
    Order order = orderService.findById(6l);
    Assert.assertNotNull(order);
    Assert.assertTrue(order.getOrderItems().isEmpty());

    OrderItem orderItem = new OrderItem();
    logger.exit();

}

Then I tried creating a meta-annotation interface:

@Target(ElementType.METHOD)
@SqlGroup( {
    @Sql(scripts = {"classpath:test/sql/customers/customers.sql",
                    "classpath:test/sql/orders/order_addresses.sql",
                    "classpath:test/sql/orders/order_contact_info.sql",
                    "classpath:test/sql/orders/orders.sql",
                    "classpath:test/sql/resetKeys.sql"}),
    @Sql(scripts =  {"classpath:test/sql/orders/delete_orders.sql",
                    "classpath:test/sql/customers/delete_customers.sql"}, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
            })
public @interface SqlOrders {
}  

But when I run the same method with the annotation replacing the SqlGroup, it fails because the sql data isn't loaded.

@Test
@SqlOrders
public void testAddItemToOrder() throws Exception {
    logger.entry();
    Order order = orderService.findById(6l);
    Assert.assertNotNull(order);
    Assert.assertTrue(order.getOrderItems().isEmpty());

    OrderItem orderItem = new OrderItem();
    logger.exit();
}

The relevant part of pom.xml follows, and spring.version is 4.2.1.RELEASE.

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
    <scope>test</scope>
</dependency>

Does anyone know why the data isn't being loaded with the meta-annotation version?

Richard G
  • 5,243
  • 11
  • 53
  • 95

1 Answers1

1

Your @SqlOrders annotation has no @Retention and the default is RetentionPolicy.CLASS so your annotation cannot be found by Spring at runtime. Add @Retention(RetentionPolicy.RUNTIME) and it should work.

You can also debug SqlScriptsTestExecutionListener to find out what Spring does with your annotation.

Крис
  • 1,190
  • 2
  • 11
  • 17