All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.drus.jpa.unit.api.JpaUnitRunner Maven / Gradle / Ivy

Go to download

JUnit 4 extension to run tests for JPA entities using EntityManagerFactory or EntityManager

There is a newer version: 0.5.0
Show newest version
package eu.drus.jpa.unit.api;

import static eu.drus.jpa.unit.rule.MethodRuleRegistrar.registerRules;

import java.util.List;

import org.junit.Rule;
import org.junit.rules.MethodRule;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkField;
import org.junit.runners.model.InitializationError;

import eu.drus.jpa.unit.core.JpaUnitContext;
import eu.drus.jpa.unit.spi.DecoratorExecutor;

public class JpaUnitRunner extends BlockJUnit4ClassRunner {

    private final DecoratorExecutor executor;

    public JpaUnitRunner(final Class clazz) throws InitializationError {
        super(clazz);
        executor = new DecoratorExecutor();

        final List ruleFields = getTestClass().getAnnotatedFields(Rule.class);
        if (ruleFields.stream().anyMatch(f -> f.getType().equals(JpaUnitRule.class))) {
            throw new InitializationError("JpaUnitRunner and JpaUnitRule exclude each other");
        }
    }

    @Override
    protected List rules(final Object target) {
        return registerRules(super.rules(target), executor, JpaUnitContext.getInstance(getTestClass().getJavaClass()));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy