摘要
仅当我编译模块(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;
}
该函数的目的是在给定通用输入true/false
的情况下返回List<?> products
:
null
也不是empty
... XmlProducts
的实例... XmlProducts
实例,包含的XmlProduct
的列表也不为空... ...那么该函数应该返回true
,在任何其他情况下都应返回false
。
单元测试
为了解决此问题,我添加了一些单元测试,其中之一如下:
@Test
public void testListOfOneEmptyXmlProducts() {
List<Object> listToPrice = BatchPricingHelper.createPricingApiInputEmptyBatchXmlProducts();
assertTrue(!ProductUtils.isBatchOfProducts(listToPrice));
}
由BatchPricingHelper.createPricingApiInputEmptyBatchXmlProducts()
给出的输出,顾名思义,它只是一个List<?>
,仅包含一个XmlProducts
,其列表中没有XmlProduct
({{1} }为空)。
从Eclipse运行时
我可以很容易地看到单元测试是绿色的:该集合未遵循所有标准来成为一批产品,因此该函数返回getXmlProduct()
,而我在{{1}上返回false
}:
从Maven编译时
如果我使用Maven编译模块,特别是目录上的命令assertTrue
,则会出现以下错误:
!false
...当然导致mvn clean install
。我尝试将正在测试的函数的整体添加到单元测试中,并打印每个输出,以查看评估失败的地方:
-------------------------------------------------------
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));
}
答案 0 :(得分:2)
最后,这只是一个编译问题。
isBatchOfProducts()
中编写了函数ProductUtils
,该类在另一个Maven项目(例如Project X)中testListOfOneEmptyXmlProducts()
写入了Project Y isBatchOfProducts()
的代码.jar
基本上,不是问题。记住要始终重新编译您所做的更改:)
答案 1 :(得分:-1)
更多括号呢:public function appearanceiddata()
{
if (!Auth::guard('web')->check()) //check if someone is logged in
{
//redirect to login page.
}
else {
/*Check if the logged in user is your desired user.
Maybe try matching the logged in id with your desired id.
If you find that a user is logged in but they are not your desired user
then you may redirect them in some other place or show them a message. */
}
//$magic = DB::table('prog_title')->select('pr_id', 'pr_title')->get();
$magic = DB::table('prog_title')->select('pr_id', 'pr_title')-> where('pr_index', '=', 1)->get();
return view ('takealook', ['magical' => $magic]);
}
?