Arquillian in-container测试:只有第一次测试通过,其他测试失败

时间:2018-02-01 16:22:12

标签: java java-ee junit integration-testing jboss-arquillian

我写了一些容器测试,看看是否抛出了正确的异常。如果我在测试类中单独运行每个测试它们都可以工作,但如果我同时在类中运行所有测试,则只有第一个测试通过而所有其他测试都失败。错误是:java.lang.AssertionError: Expected exception: de.paylax.exception.user.KYCValidationAlreadyAskedException

我从eclipse(JUnit4 Runner)运行测试。

这是我的测试类:

@RunWith(Arquillian.class)
public class PaymentBoundaryExceptionTests extends InContainerTest {

    @Inject
    private PaymentBoundary paymentBoundary;

    @Inject
    private ContractControl contractControl;

    @Inject
    private UserControl userControl;
/***********************************************************************
     * Exception Tests
     */

    /**
     * Check if Exception is thrown when wrong payout amount
     */
    @Test(expected = WrongTransactionAmountException.class)
    @UsingDataSet({ "datasets/common/common.yml", "datasets/payments/payments.yml" })
    public void PaymentBoundary_WrongPayOutAmount_WrongTransactionAmountException() {
        ContractEntity contractEntity = contractControl.findContractByContractCode("goodsContract");
        paymentBoundary.createPayout(100, 20, 10, contractEntity.getPayee(), contractEntity, "test");
    }

    /**
     * Check if Exception is thrown when wrong transfer amount
     */
    @Test(expected = WrongTransactionAmountException.class)
    @UsingDataSet({ "datasets/common/common.yml", "datasets/payments/payments.yml" })
    public void PaymentBoundary_WrongTransferAmount_WrongTransactionAmountException() {
        ContractEntity contractEntity = contractControl.findContractByContractCode("goodsContract");
        paymentBoundary.transferFromWalletToWallet(contractEntity.getPayer(), contractEntity.getPayee(), 100, 20, 10,
                contractEntity);
    }
    // .... more tests here

我猜测我的测试设置有问题。 这是我的InContainer-Test:

public abstract class InContainerTest {

    /**
     * Create the Web Archive.
     * 
     * @return the web archive
     */
    @Deployment(testable = true)
    public static final WebArchive createDeployment() {
        // loads the pom configuration
        File[] dependencies = Maven.resolver().loadPomFromFile("pom.xml").importRuntimeDependencies().resolve()
                .withTransitivity().asFile();
        // loads the mockito framework for testing
        File mockito = Maven.resolver().loadPomFromFile("pom.xml").resolve("org.mockito:mockito-all:1.10.19")
                .withTransitivity().asSingleFile();
        // adds the package for MyProject pointing to the RestMyProject api
        WebArchive war = ShrinkWrap.create(WebArchive.class).addPackages(true, "de.MyProject").addClass(RestMyProject.class)
                .addAsLibraries(dependencies).addAsLibraries(mockito)
                // adds the test perisistence xml configuration
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                // adds the test beans.xml and the log4j2.xml
                .addAsResource("test-beans.xml", "META-INF/beans.xml").addAsResource("log4j2.xml", "log4j2.xml")
                // adds the MyProjectMapping.xml
                .addAsResource("MyProjectMapping.xml", "MyProjectMapping.xml")
                // EMail Templates
                .addAsResource("HTMLEmailTemplate/admin-info.html", "HTMLEmailTemplate/admin-info.html")
                // SQL
                .addAsResource("datasets/scripts/truncate-users.sql", "datasets/scripts/truncate-users.sql")
                .addAsResource("datasets/scripts/autoincrement-users.sql", "datasets/scripts/autoincrement-users.sql")
                .addAsResource("datasets/scripts/contracts.sql", "datasets/scripts/contracts.sql");
        ;
        return war;
    }
}

另外,我在每次测试中使用@UsingDataSet()而不是在课堂上使用一次是错误的吗?根据我的理解,这样就可以为每个@Test重置和播种表格。

1 个答案:

答案 0 :(得分:0)

关于@UsingDataSet,您可以在类级别设置。 APE负责在每次测试后删除数据。您可以修改此行为,但IIRC以这种方式工作