jeudi 21 février 2019

Seperate liquibase file with test data per test class in Spring Boot

I want to seperate test data for particular test. For example we have two test classes A and B they are running on diffrent data so I want to create ATestData.xml and BTestData.xml and when test class starts it should execute ATestData.xml then rollback then start test B execute BTestData.xml. in Arquillian Persistence Extension there was @UsingDataSet("datasets/users.yml").

I started to wrtie my own annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface LiquibaseTestDataFile {
    String value() default "";
}

And

@Slf4j
@Aspect
@Component
public class LiquibaseTestDataFileAspect {

    @Before("execution(* (@LiquibaseTestDataFile *).*(..)) || execution(@LiquibaseTestDataFile * *(..))")
    public void insertTestData(JoinPoint joinPoint){
    log.info("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Class<?> declaringClass = signature.getMethod().getDeclaringClass();
        LiquibaseTestDataFile myAnnotation = declaringClass.getAnnotation(LiquibaseTestDataFile.class);

       //TODO if null maybe on method

       //log.info(myAnnotation.value());
    }
}

But it not trigers when annotation is on test class.

I am using @EnableAspectJAutoProxy in configuration

and

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {TransferManagerRestApplication.class})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

in test class.

Aucun commentaire:

Enregistrer un commentaire