简体   繁体   English

如何在Junit中使用assertEquals解决此问题?

[英]How to fix this problem with assertEquals in Junit?

Please help me with the following problem, My Junit test fails because my output is 请帮助我解决以下问题,因为我的输出是

"09:39:43.704 [main] INFO by.iba.gomel.ShapeController - 145"

But i need just "145" ShapeController.LOGGER.info("{}", ShapeController.sum); 但是我只需要“ 145” ShapeController.LOGGER.info("{}", ShapeController.sum);

@Test
public void testSumma() {

    final Shape[] newShapes = new Shape[5];
    ShapeController.initializeArray(newShapes);
    ShapeController.summa(newShapes);
    Assert.assertEquals("these objects should be  equal", "145", this.log.getLog());
}

If you are not changing the log format and the class name you can use this.log.getLog().split("ShapeController - ")[1] . 如果不更改日志格式和类名,则可以使用this.log.getLog().split("ShapeController - ")[1]

But be wary of such hard-coded things. 但是要警惕这种硬编码的东西。 Anyways if you change something this test case will break and notify you. 无论如何,如果您更改了某些内容,此测试用例将中断并通知您。

You can use Java Library such as Hamcrest to make an assertion in a sentence if it ends with the number or word, such as 1 : 如果Java库(例如Hamcrest)以数字或单词(例如1)结尾,则可以在句子中进行断言:

assertThat("myStringOfNote", endsWith("Note"))

This can be translated into your use case as : 可以将其转换为您的用例:

assertThat(this.log.getLog(), endsWith("145"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM