Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java junit.Assert.assertTrue失败,但仅在编译模块时失败?_Java_Maven_Junit - Fatal编程技术网

Java junit.Assert.assertTrue失败,但仅在编译模块时失败?

Java junit.Assert.assertTrue失败,但仅在编译模块时失败?,java,maven,junit,Java,Maven,Junit,摘要 单元测试断言(junit.Assert.assertTrue)仅在我编译模块(mvn clean install)时失败。 然而,如果我在Eclipse上运行JUnit测试,它并没有失败,当我看到函数的作用时,我无法理解编译失败的原因。 有人知道我该如何排除出什么问题吗 以下是感兴趣的读者的所有详细信息:) 要测试的功能: 我具有以下主体的功能: public static boolean isBatchOfProducts(List<?> products) { bo

摘要

单元测试断言(
junit.Assert.assertTrue
)仅在我编译模块(
mvn clean install
)时失败。 然而,如果我在Eclipse上运行
JUnit
测试,它并没有失败,当我看到函数的作用时,我无法理解编译失败的原因。 有人知道我该如何排除出什么问题吗

以下是感兴趣的读者的所有详细信息:)

要测试的功能:

我具有以下主体的功能:

public static boolean isBatchOfProducts(List<?> products) {
    boolean areBatches = (products != null && products.size() != 0);
    for (Object product : products) {
        areBatches = areBatches && product instanceof XmlProducts;
        if (areBatches) {
            XmlProducts xmlProducts = (XmlProducts)product;
            areBatches = areBatches && !xmlProducts.getXmlProduct().isEmpty();              
        }
    }
    return areBatches;
}
BatchPricingHelper.createPricingApiInputEmptyBatchXmlProducts()
给出的输出,顾名思义,它只是一个
列表
,只包含一个
XmlProducts
,列表中没有
XmlProduct
getXmlProduct()
为空)

当我从Eclipse运行时

我可以很容易地看到单元测试是绿色的:集合不遵守作为一批产品的所有标准,因此函数返回
false
上的I
assertTrue
!错误

当我从Maven编译时

如果我使用Maven编译模块,特别是目录上的命令
mvncleanstall
,我会得到以下错误:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running mycompany.sdk.pricing.impl.PricingSessionTest
Tests run: 15, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.311 sec <<< FAILURE! - in mycompany.sdk.pricing.impl.PricingSessionTest
testListOfOneEmptyXmlProducts(mycompany.sdk.pricing.impl.PricingSessionTest)  Time elapsed: 0.004 sec  <<< FAILURE!
java.lang.AssertionError:
        at org.junit.Assert.fail(Assert.java:91)
        at org.junit.Assert.assertTrue(Assert.java:43)
        at org.junit.Assert.assertTrue(Assert.java:54)
        at mycompany.sdk.pricing.impl.PricingSessionTest.testListOfOneEmptyXmlProducts(PricingSessionTest.java:279)
奇怪的是,我可以在命令提示符输出中看到每个计算都进行得很顺利,实际上只有
assertTrue
不起作用:

Running mycompany.sdk.pricing.impl.PricingSessionTest
list not null and not empty: true << CORRECT!
product is instance of XmlProducts: true << CORRECT!
list of XmlProduct is not empty: false << CORRECT, THE LIST IS EMPTY!
Final result: false << CORRECT, IT SHOULD RETURN FALSE!

---BUT STILL THE FAILURE BELOW!---

Tests run: 15, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.311 sec <<< FAILURE! - in mycompany.sdk.pricing.impl.PricingSessionTest
testListOfOneEmptyXmlProducts(mycompany.sdk.pricing.impl.PricingSessionTest)  Time elapsed: 0.004 sec  <<< FAILURE!
java.lang.AssertionError:
        at org.junit.Assert.fail(Assert.java:91)
        at org.junit.Assert.assertTrue(Assert.java:43)
        at org.junit.Assert.assertTrue(Assert.java:54)
        at mycompany.sdk.pricing.impl.PricingSessionTest.testListOfOneEmptyXmlProducts(PricingSessionTest.java:279)
运行mycompany.sdk.pricing.impl.PricingSessionTest

list not null and not empty:true更多的括号是什么:
assertTrue(!(ProductUtils.isBatchOfProducts(listToPrice))

最后,这只是一个编译问题

  • 我已经在类
    ProductUtils
    中编写了函数
    isBatchOfProducts()
    ,该类在另一个Maven项目中(比如项目X)
  • 我已经将单元测试
    TestListofOnemptyxmlProducts()
    写入到项目Y中
  • 多亏了这个单元测试,我发现了一个bug并更改了
    isBatchOfProducts()
  • 我重新运行了单元测试,结果是绿色的(因为Eclipse显然是在幕后编译我对项目X所做的更改)
  • 我尝试编译项目Y,但失败了,因为它仍然指向
    .jar

基本上,这不是问题。请记住总是要重新编译您更改的内容:)

我建议将调试输出添加到
isBatchOfProducts
方法本身,而不是将其代码复制粘贴到其他地方。然后,您将能够跟踪方法真正执行过程中的哪个点
AreBatchs
变为false。与其使用
assertTrue(!条件)
为什么不使用
assertFalse(条件)
?此外,在
isBatchOfProducts
中,您永远不会中断
for
循环,这意味着
arebacks
是测试列表最后一个元素的结果。换句话说,一个元素可以将
arebacks
设置为
false
,但如果最后一个元素将其设置为
true
,则该方法的结果为
true
。这是故意的行为吗?@Slaw你是对的,
assertFalse
更具可读性,没有解决问题,但我使用它来提高可读性。关于函数本身,
arebacks
仅在函数顶部初始化,然后一直说
arebacks=arebacks&&someothercondition
。因此,一旦
arebacks
变为
false
一次,它就不再是
true
。即使在我们已经知道结果的情况下继续循环是没有用的,我也会添加一个
中断
:)啊,我没有考虑
&&
。但是,是的,一次
中断
会更有效
错误
:)。至于手头的问题,我选择第二个默认语言环境:尝试添加调试输出或附加调试器并单步执行。@defaultlocale感谢您的评论,我已经找出了问题所在!实际上,
isBatchOfProducts
函数是在另一个模块中定义的,并导入到这个模块中。多亏了单元测试,我发现了一个修复程序,但是我忘记了重新编译另一个模块。Eclipse有一个新的
.jar
的本地副本,但是当手动编译单个模块
mvn
时,没有检测到依赖项的变化,也没有使用旧版本的函数。当我按照你的建议在函数中添加
System.out.println
时,我就明白了!如果你写一个答案,我会接受的:)试过了,但这不是问题,谢谢。
@Test
public void testListOfOneEmptyXmlProducts() {
    List<Object> listToPrice = BatchPricingHelper.createPricingApiInputEmptyBatchXmlProducts();
    boolean areBatches = (listToPrice != null && listToPrice.size() != 0);
    System.out.println("list not null and not empty: " + areBatches);
    for (Object product : listToPrice) {
        areBatches = areBatches && product instanceof XmlProducts;
        System.out.println("product is instance of XmlProducts: " + areBatches);
        if (areBatches) {
            XmlProducts xmlProducts = (XmlProducts)product;
            areBatches = areBatches && !xmlProducts.getXmlProduct().isEmpty(); 
            System.out.println("list of XmlProduct is not empty: " + areBatches);
        }
    }
    System.out.println("Final result: " + areBatches);
    assertTrue(!ProductUtils.isBatchOfProducts(listToPrice));
}
Running mycompany.sdk.pricing.impl.PricingSessionTest
list not null and not empty: true << CORRECT!
product is instance of XmlProducts: true << CORRECT!
list of XmlProduct is not empty: false << CORRECT, THE LIST IS EMPTY!
Final result: false << CORRECT, IT SHOULD RETURN FALSE!

---BUT STILL THE FAILURE BELOW!---

Tests run: 15, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.311 sec <<< FAILURE! - in mycompany.sdk.pricing.impl.PricingSessionTest
testListOfOneEmptyXmlProducts(mycompany.sdk.pricing.impl.PricingSessionTest)  Time elapsed: 0.004 sec  <<< FAILURE!
java.lang.AssertionError:
        at org.junit.Assert.fail(Assert.java:91)
        at org.junit.Assert.assertTrue(Assert.java:43)
        at org.junit.Assert.assertTrue(Assert.java:54)
        at mycompany.sdk.pricing.impl.PricingSessionTest.testListOfOneEmptyXmlProducts(PricingSessionTest.java:279)