代码之家  ›  专栏  ›  技术社区  ›  Matteo NNZ

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

  •  1
  • Matteo NNZ  · 技术社区  · 7 年前

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

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

    要测试的功能:

    我具有以下主体的功能:

    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;
    }
    

    true/false ,给定一个通用输入 List<?> products

    • 如果此列表不可用 null 也没有 empty ...
    • XmlProducts ...
    • ... 如果每 XML产品 例如 XmlProduct 包含的不是空的。。。

    true false 在任何其他情况下。

    单元测试

    为了涵盖此行为,我添加了一些单元测试,其中一个是:

    @Test
    public void testListOfOneEmptyXmlProducts() {
        List<Object> listToPrice = BatchPricingHelper.createPricingApiInputEmptyBatchXmlProducts();
        assertTrue(!ProductUtils.isBatchOfProducts(listToPrice));
    }
    

    BatchPricingHelper.createPricingApiInputEmptyBatchXmlProducts() ,正如名字所说,它只是一个 List<?> 只包含一个 没有 在其清单中( getXmlProduct() 是空的)。

    当我从Eclipse运行时

    我可以很容易地看到单元测试是绿色的:集合不尊重作为一批产品的所有条件,因此函数返回 而我 assertTrue 在…上 !false

    enter image description here

    当我从Maven编译时

    mvn清洁安装

    -------------------------------------------------------
     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)
    

    ... 当然引起了 BUILD FAILURE

    @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)
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Matteo NNZ    7 年前

    最后,这只是一个汇编问题。

    • 我已经编写了函数 isBatchOfProducts() 在课堂上 ProductUtils
    • 我已经编写了单元测试 testListOfOneEmptyXmlProducts() 进入Y项目
    • 我重新运行了单元测试,结果是绿色的(因为Eclipse显然是在幕后编译我对项目X所做的更改)
    • 我曾尝试编译项目Y,但失败了,因为它仍然指向 .jar

        2
  •  -1
  •   Matteo NNZ    7 年前

    assertTrue(!(ProductUtils.isBatchOfProducts(listToPrice))); ?