【发布时间】:2018-02-01 16:22:12
【问题描述】:
我写了一些容器测试来看看是否抛出了正确的异常。如果我在测试类中单独运行每个测试,它们会起作用,但是如果我一次在类中运行所有测试,则只有第一个测试通过,所有其他测试都失败。错误是: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 重置和播种。
【问题讨论】:
-
做一个快速概述我没有发现任何错误,您是否尝试过调试您的测试(您可以使用嵌入式容器使其更容易)并检查抛出异常的原因?还可以尝试在类级别移动 using 数据集,以查看它是否与 APE 相关。
-
嘿,谢谢,我已经尝试将数据集移动到类级别,但没有帮助。我尝试调试测试,但不幸的是它并没有在我的断点处停止。你知道调试容器内测试的设置吗?
-
可能您正在使用托管或远程模式,这意味着该进程正在另一个 JVM 中运行。因此,您可以从远程调试开始,也可以使用嵌入式模式依赖项,这样它就可以在与 IDE 相同的 JVM 中使用,这样您就可以像往常一样进行调试。
-
抱歉回复晚了,我花了一些时间让远程调试工作。这与我的 yaml-dataseed 文件中的错误有关。谢谢!
标签: java jakarta-ee junit integration-testing jboss-arquillian