【问题标题】:How to test the following code using junit? Any solution would be appreciated如何使用junit测试以下代码?任何解决方案将不胜感激
【发布时间】:2019-04-16 05:41:42
【问题描述】:

以下代码如何编写JUnit测试用例?

public static boolean validEmail(String emailAddress) {
    String EMAIL = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$";
    if (emailAddress.trim().matches(EMAIL)) {
        return true;
    } else {
        return false;
    }
}

【问题讨论】:

  • 你试过了吗?对某事有任何疑问?
  • 我会尝试this方式。
  • 还有:return emailAddress.trim().matches(EMAIL);.

标签: java unit-testing junit


【解决方案1】:

使用 JUnit 5,我会推荐这个:

  @ParameterizedTest
  @CsvSource({"true, tester@mail.test", "false, unknown", "false, A@A"})
  void testIsValidEmail(boolean expected, String email) {
    assertEquals(expected, YourClass.validEmail(email));
  }

但是您应该在额外的测试参数上付出一些努力。您可以在https://www.istqb.org/downloads/send/51-ctfl2018/208-ctfl-2018-syllabus.html 阅读有关等价分区和边界值分析的内容@

【讨论】:

  • 谢谢 Jens Dibbern。感谢您的帮助。
  • 不客气。你会考虑接受答案吗?如果答案对您有用,这就是 SO 中的标准程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
  • 1970-01-01
  • 2019-05-24
  • 1970-01-01
  • 2020-05-29
  • 1970-01-01
  • 2022-11-19
相关资源
最近更新 更多