Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of the parameters of Assert.assertEquals () method

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the example analysis of the parameters of the Assert.assertEquals () method, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

Assert under the junit.framework package provides several assertion methods. The main purpose is to compare the two parameters passed in by the test.

Assert.assertEquals (); and its overloading method:

1. If the two are consistent, the program continues to run.

two。 If the two are inconsistent, interrupt the test method and throw the exception information AssertionFailedError.

Take Assert.assertEquals (int expected, int actual) as an example:

/ * Asserts that two ints are equal. Assert that two int are equal * / static public void assertEquals (int expected, int actual) {assertEquals (null, expected, actual);}

You can see that the assertEquals (String message, int expected, int actual) method is called in it:

/ * Asserts that two ints are equal. If they are not * an AssertionFailedError is thrown with the given message. * if the AssertionFailedError message with message is not thrown, the two are equal * / static public void assertEquals (String message, int expected, int actual) {assertEquals (message, Integer.valueOf (expected), Integer.valueOf (actual));}

As you can see, here the int type is sealed into the Integer type. The comment says that an exception will be thrown, but there is no exception here. It doesn't matter. Let's go on to call the assertEquals (String message, Object expected, Object actual) method:

/ * Asserts that two objects are equal. If they are not * an AssertionFailedError is thrown with the given message. * if the AssertionFailedError information with message is not thrown, the two are equal (here we compare Object objects) * / static public void assertEquals (String message, Object expected, Object actual) {if (expected = = null & & actual = = null) {return;} if (expected! = null & & expected.equals (actual)) {return;} failNotEquals (message, expected, actual);}

Two if statements judge whether the two are equal: the reference (address) is equal or the content is equal. If both if cases fail, it means that parameter 1 and parameter 2 are actually not equal, so the code executes the failNotEquals (String message, Object expected, Object actual) method and throws an exception in this method, and then it is relatively simple:

Static public void failNotEquals (String message, Object expected, Object actual) {fail (format (message, expected, actual));} public static String format (String message, Object expected, Object actual) {String formatted = ""; if (message! = null & & message.length () > 0) {formatted = message + ";} return formatted +" expected: but was: " } / * * Fails a test with the given message.*/static public void fail (String message) {if (message = = null) {throw new AssertionFailedError ();} throw new AssertionFailedError (message);}

As can be seen above, it is the fail (String message) method that finally throws the exception information!

How to use Assert.assertEquals ():

Use, sample code:

Assert.assertEquals (true, arry.contains ("hello")); Assert.assertEquals (39991L, aa.getLong ("key3", 0L)); Assert.assertEquals (true, bb.getBoolean ("key4", false)); Assert.assertEquals (5.3f, cc.getFloat ("key5", 0.f)); Assert.assertEquals (99, dd.getInt ("key6", 1)); Assert.assertEquals ("if printing this information, prove that the parameters are not equal", 10L, 10)

According to the source code analysis, we can pass an expected result as a parameter. The 2 parameters pass the method we need to test. Then execute it. Equal, code continues to execute, unequal, interrupt execution, throw exception message!

I would like to mention briefly:

Assert.assertSame (Object expected, Object actual) method:

It compares whether the reference address is equal, but does not compare the content:

/ * Asserts that two objects refer to the same object. If they are not * the same an AssertionFailedError is thrown. * / static public void assertSame (Object expected, Object actual) {assertSame (null, expected, actual);} / * * Asserts that two objects refer to the same object. If they are not * an AssertionFailedError is thrown with the given message. * / static public void assertSame (String message, Object expected, Object actual) {if (expected = = actual) {return;} failNotSame (message, expected, actual);} Thank you for reading this article carefully. I hope the article "sample Analysis of Assert.assertEquals () method parameters shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!"

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report