简体   繁体   English

java-有时返回字符串且有时返回null时如何使用Assert.assertEquals

[英]java - How to use Assert.assertEquals when sometimes a String is returned and sometimes null is returned

This is my first question on StackOverflow and I'm a complete beginner so I apologize if my question is vague 这是我关于StackOverflow的第一个问题,我是一个完整的初学者,因此,如果我的问题含糊不清,我深表歉意

I have a method that will sometimes return a String and sometimes return null. 我有一个方法,有时会返回字符串,有时会返回null。

public String confirmLogo() {
String logo = null;

if(sql.getLogo(ID).equals("Qwerty Logo")
logo += "Qwerty Logo";

return logo;
}

So sometimes "qwerty logo" will be returned and sometimes it will return null. 因此,有时会返回“ qwerty徽标”,有时会返回null。

Then in a test class, I'm trying to confirm that whats returned matched whats expected. 然后在测试课程中,我试图确认返回的内容与预期的内容相符。

String primaryLogo = getLogoName(ID);
Assert.assert.equals(primaryLogo, example.confirmLogo());

I'm understandably getting NPEs (java.lang.NullPointerException) returned but I was wondering how I can accomplish this or if someone can't point me to an appropriate reference. 可以理解,我正在返回NPE(java.lang.NullPointerException),但是我想知道如何实现此目标,或者是否有人无法将我指向适当的参考。

Your test should be predictable I would use two different tests to test this, one that expects null and one that doesn't. 您的测试应该是可预测的,我将使用两种不同的测试来进行测试,一种期望为null而另一种则不。 Use different input parameters to achieve the desired behavior (eg once provide x = "test" , and in other use x = "other" ). 使用不同的输入参数来实现所需的行为(例如,一次提供x = "test" ,另一种使用x = "other" )。

Unless you really have eg a Math.random() in the method :) 除非您的方法中确实包含例如Math.random()

You can find a reference for the Assert class here. 您可以在此处找到Assert类的参考

When it is exepcted to return null, you can use assertNull. 当执行返回空值时,可以使用assertNull。 When it isn't, you can use assertNotNull before you assertEquals -- avoiding the NPE. 如果不是,则可以在使用assertEquals之前使用assertNotNull -避免使用NPE。

According to the JavaDoc of Assert.assertEquals() ( http://junit.sourceforge.net/javadoc/org/junit/Assert.html#assertEquals(java.lang.Object,%20java.lang.Object) ) 根据Assert.assertEquals()的JavaDoc( http://junit.sourceforge.net/javadoc/org/junit/Assert.html#assertEquals ( Assert.assertEquals() ,% Assert.assertEquals()

Asserts that two objects are equal. 断言两个对象相等。 If they are not, an AssertionError without a message is thrown. 如果不是,则抛出一条不带消息的AssertionError。 If expected and actual are null, they are considered equal . 如果期望值和实际值均为null,则将它们视为相等

you should never get a NullPointerException from calling Assert.assertEquals() . 永远不要通过调用Assert.assertEquals()获得NullPointerException

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

相关问题 Selenium Web驱动程序-Java-TestNG-Assert.assertEquals如何将实际结果与预期结果之间的范围进行比较 - Selenium Web Driver - Java - TestNG - Assert.assertEquals how to compare actual result with the range between expected result Assert.assertEquals在两个列表上 - Assert.assertEquals on two lists Assert.assertEquals 没有正确比较 String - Assert.assertEquals didn't compares String right way Assert.assertEquals的奇怪行为 - Strange behaviour of Assert.assertEquals 如何使用 Assert.assertEquals() 测试单链表 - How to test Single Linked List using Assert.assertEquals() 如果在Assert.assertEquals硒测试NG上有其他条件 - if else condition on Assert.assertEquals selenium testNG assertEquals()和Assert.assertEquals()之间的JUnit测试差异 - JUnit test difference between assertEquals() and Assert.assertEquals() Spring 引导 JSON 有时返回一个空白字符串 有时 null 关键字 - Spring Boot JSON returned sometime returns a blank string sometimes the null keyword 检查字符串是否为null或为空,有时为null,有时为null - Check if a String is null or empty when it's null sometimes and sometimes not 如何使用三元运算符将有时可能为空的字符串转换为 Java 中的整数? - How to use a ternary operator to convert a String that can sometimes be null into an integer in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM