Junit的Assert用法

  1 package junit.framework;
  2
  3 /**
  4  * A set of assert methods.  Messages are only displayed when an assert fails.
  5  */
  6
  7 public class Assert {
  8     /**
  9      * Protect constructor since it is a static only class
 10      */
 11     protected Assert() {
 12     }
 13
 14     /**
 15      * Asserts that a condition is true. If it isn‘t it throws
 16      * an AssertionFailedError with the given message.
 17      */
 18     static public void assertTrue(String message, boolean condition) {
 19         if (!condition)
 20             fail(message);
 21     }
 22     /**
 23      * Asserts that a condition is true. If it isn‘t it throws
 24      * an AssertionFailedError.
 25      */
 26     static public void assertTrue(boolean condition) {
 27         assertTrue(null, condition);
 28     }
 29     /**
 30      * Asserts that a condition is false. If it isn‘t it throws
 31      * an AssertionFailedError with the given message.
 32      */
 33     static public void assertFalse(String message, boolean condition) {
 34         assertTrue(message, !condition);
 35     }
 36     /**
 37      * Asserts that a condition is false. If it isn‘t it throws
 38      * an AssertionFailedError.
 39      */
 40     static public void assertFalse(boolean condition) {
 41         assertFalse(null, condition);
 42     }
 43     /**
 44      * Fails a test with the given message.
 45      */
 46     static public void fail(String message) {
 47         if (message == null) {
 48             throw new AssertionFailedError();
 49         }
 50         throw new AssertionFailedError(message);
 51     }
 52     /**
 53      * Fails a test with no message.
 54      */
 55     static public void fail() {
 56         fail(null);
 57     }
 58     /**
 59      * Asserts that two objects are equal. If they are not
 60      * an AssertionFailedError is thrown with the given message.
 61      */
 62     static public void assertEquals(String message, Object expected, Object actual) {
 63         if (expected == null && actual == null)
 64             return;
 65         if (expected != null && expected.equals(actual))
 66             return;
 67         failNotEquals(message, expected, actual);
 68     }
 69     /**
 70      * Asserts that two objects are equal. If they are not
 71      * an AssertionFailedError is thrown.
 72      */
 73     static public void assertEquals(Object expected, Object actual) {
 74         assertEquals(null, expected, actual);
 75     }
 76     /**
 77      * Asserts that two Strings are equal.
 78      */
 79     static public void assertEquals(String message, String expected, String actual) {
 80         if (expected == null && actual == null)
 81             return;
 82         if (expected != null && expected.equals(actual))
 83             return;
 84         String cleanMessage= message == null ? "" : message;
 85         throw new ComparisonFailure(cleanMessage, expected, actual);
 86     }
 87     /**
 88      * Asserts that two Strings are equal.
 89      */
 90     static public void assertEquals(String expected, String actual) {
 91         assertEquals(null, expected, actual);
 92     }
 93     /**
 94      * Asserts that two doubles are equal concerning a delta.  If they are not
 95      * an AssertionFailedError is thrown with the given message.  If the expected
 96      * value is infinity then the delta value is ignored.
 97      */
 98     static public void assertEquals(String message, double expected, double actual, double delta) {
 99         if (Double.compare(expected, actual) == 0)
100             return;
101         if (!(Math.abs(expected-actual) <= delta))
102             failNotEquals(message, new Double(expected), new Double(actual));
103     }
104     /**
105      * Asserts that two doubles are equal concerning a delta. If the expected
106      * value is infinity then the delta value is ignored.
107      */
108     static public void assertEquals(double expected, double actual, double delta) {
109         assertEquals(null, expected, actual, delta);
110     }
111     /**
112      * Asserts that two floats are equal concerning a positive delta. If they
113      * are not an AssertionFailedError is thrown with the given message. If the
114      * expected value is infinity then the delta value is ignored.
115      */
116     static public void assertEquals(String message, float expected, float actual, float delta) {
117         if (Float.compare(expected, actual) == 0)
118             return;
119         if (!(Math.abs(expected - actual) <= delta))
120                 failNotEquals(message, new Float(expected), new Float(actual));
121     }
122     /**
123      * Asserts that two floats are equal concerning a delta. If the expected
124      * value is infinity then the delta value is ignored.
125      */
126     static public void assertEquals(float expected, float actual, float delta) {
127         assertEquals(null, expected, actual, delta);
128     }
129     /**
130      * Asserts that two longs are equal. If they are not
131      * an AssertionFailedError is thrown with the given message.
132      */
133     static public void assertEquals(String message, long expected, long actual) {
134         assertEquals(message, new Long(expected), new Long(actual));
135     }
136     /**
137      * Asserts that two longs are equal.
138      */
139     static public void assertEquals(long expected, long actual) {
140         assertEquals(null, expected, actual);
141     }
142     /**
143      * Asserts that two booleans are equal. If they are not
144      * an AssertionFailedError is thrown with the given message.
145      */
146     static public void assertEquals(String message, boolean expected, boolean actual) {
147             assertEquals(message, Boolean.valueOf(expected), Boolean.valueOf(actual));
148       }
149     /**
150      * Asserts that two booleans are equal.
151       */
152     static public void assertEquals(boolean expected, boolean actual) {
153         assertEquals(null, expected, actual);
154     }
155     /**
156      * Asserts that two bytes are equal. If they are not
157      * an AssertionFailedError is thrown with the given message.
158      */
159       static public void assertEquals(String message, byte expected, byte actual) {
160         assertEquals(message, new Byte(expected), new Byte(actual));
161     }
162     /**
163         * Asserts that two bytes are equal.
164      */
165     static public void assertEquals(byte expected, byte actual) {
166         assertEquals(null, expected, actual);
167     }
168     /**
169      * Asserts that two chars are equal. If they are not
170      * an AssertionFailedError is thrown with the given message.
171      */
172       static public void assertEquals(String message, char expected, char actual) {
173             assertEquals(message, new Character(expected), new Character(actual));
174       }
175     /**
176      * Asserts that two chars are equal.
177      */
178       static public void assertEquals(char expected, char actual) {
179         assertEquals(null, expected, actual);
180     }
181     /**
182      * Asserts that two shorts are equal. If they are not
183      * an AssertionFailedError is thrown with the given message.
184      */
185     static public void assertEquals(String message, short expected, short actual) {
186             assertEquals(message, new Short(expected), new Short(actual));
187     }
188       /**
189      * Asserts that two shorts are equal.
190      */
191     static public void assertEquals(short expected, short actual) {
192         assertEquals(null, expected, actual);
193     }
194     /**
195      * Asserts that two ints are equal. If they are not
196      * an AssertionFailedError is thrown with the given message.
197      */
198       static public void assertEquals(String message, int expected, int actual) {
199         assertEquals(message, new Integer(expected), new Integer(actual));
200       }
201       /**
202         * Asserts that two ints are equal.
203      */
204       static public void assertEquals(int expected, int actual) {
205           assertEquals(null, expected, actual);
206     }
207     /**
208      * Asserts that an object isn‘t null.
209      */
210     static public void assertNotNull(Object object) {
211         assertNotNull(null, object);
212     }
213     /**
214      * Asserts that an object isn‘t null. If it is
215      * an AssertionFailedError is thrown with the given message.
216      */
217     static public void assertNotNull(String message, Object object) {
218         assertTrue(message, object != null);
219     }
220     /**
221      * Asserts that an object is null. If it isn‘t an {@link AssertionError} is
222      * thrown.
223      * Message contains: Expected: <null> but was: object
224      *
225      * @param object
226      *            Object to check or <code>null</code>
227      */
228     static public void assertNull(Object object) {
229         String message = "Expected: <null> but was: " + String.valueOf(object);
230         assertNull(message, object);
231     }
232     /**
233      * Asserts that an object is null.  If it is not
234      * an AssertionFailedError is thrown with the given message.
235      */
236     static public void assertNull(String message, Object object) {
237         assertTrue(message, object == null);
238     }
239     /**
240      * Asserts that two objects refer to the same object. If they are not
241      * an AssertionFailedError is thrown with the given message.
242      */
243     static public void assertSame(String message, Object expected, Object actual) {
244         if (expected == actual)
245             return;
246         failNotSame(message, expected, actual);
247     }
248     /**
249      * Asserts that two objects refer to the same object. If they are not
250      * the same an AssertionFailedError is thrown.
251      */
252     static public void assertSame(Object expected, Object actual) {
253         assertSame(null, expected, actual);
254     }
255     /**
256      * Asserts that two objects do not refer to the same object. If they do
257      * refer to the same object an AssertionFailedError is thrown with the
258      * given message.
259      */
260     static public void assertNotSame(String message, Object expected, Object actual) {
261         if (expected == actual)
262             failSame(message);
263     }
264     /**
265      * Asserts that two objects do not refer to the same object. If they do
266      * refer to the same object an AssertionFailedError is thrown.
267      */
268     static public void assertNotSame(Object expected, Object actual) {
269         assertNotSame(null, expected, actual);
270     }
271
272     static public void failSame(String message) {
273         String formatted= "";
274          if (message != null)
275              formatted= message+" ";
276          fail(formatted+"expected not same");
277     }
278
279     static public void failNotSame(String message, Object expected, Object actual) {
280         String formatted= "";
281         if (message != null)
282             formatted= message+" ";
283         fail(formatted+"expected same:<"+expected+"> was not:<"+actual+">");
284     }
285
286     static public void failNotEquals(String message, Object expected, Object actual) {
287         fail(format(message, expected, actual));
288     }
289
290     public static String format(String message, Object expected, Object actual) {
291         String formatted= "";
292         if (message != null && message.length() > 0)
293             formatted= message+" ";
294         return formatted+"expected:<"+expected+"> but was:<"+actual+">";
295     }
296 }

1.assertTrue(boolean condition)   用于判断一个条件为真,如果为false会抛出异常

2.assertFalse(boolean condition)   用于判断一个条件为假,如果为真会抛出异常。

3.assertEquals(..........)   用于判断前后相等。

时间: 2024-11-07 23:54:04

Junit的Assert用法的相关文章

【JUnit】EasyMock用法总结

使用EasyMock的总体步骤 1.生成Mock接口 IService mockService = EasyMock.createMock("name", IService.class); 如果要mock对象,而不是接口,应该使用class extension:org.easymock.classextension.EasyMock 如果要mock多个接口,最好使用MockControl来管理: IMocksControl control = EasyMock.createContro

JUnit Test Assert

断言 是测试的心脏,就是 判断 预期的内容 是否与实际一致,就是手动测试的 精髓 1).我们在使用一个断言(中 那些方法,比如assertEquals等)可以静态一次性导入 Assert类. import static org.junit.Assert.*; 2). JUnit框架 用一组Assert 方法封装了最常见的测试任务 ,这些Assert方法极大地简化了单元测试的编写 (我擦,这不就意味着一些复杂的测试任务 得你自己来写方法,自己去new,来进行测试) 3)Assert类包含了一组静态

spring assert 用法

spring在提供一个强大的应用开发框架的同时也提供了很多优秀的开发工具类,合理的运用这些工具,将有助于提高开发效率.增强代码质量.下面就最常用的Assert工具类,简要介绍一下它的用法.Assert断言工具类,通常用于数据合法性检查. 源码: /* * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"

Delphi 中ASSERT用法

http://blog.csdn.net/dongyonggan/article/details/5780979 用法:ASSERT(表达式) 如果为假,ASSERT会产生一个EASSERTIONFAiled异常,显示为 Assertion Failed (C:/src/unit1.pas, [size=+0]line 34) 如果不想再使用这些检查时,可以使用($ASSERTIONS OFF)或($C-)编译指令 要想使Assert在整个项目中失效, 关闭Project Options | C

assert()用法

assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义:[1] #include <assert.h>void assert( int expression ); assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行. //demo1.c #include <stdio.h> #include <asser

【转】python assert用法

1.assert语句用来声明某个条件是真的.2.如果你非常确信某个你使用的列表中至少有一个元素,而你想要检验这一点,并且在它非真的时候引发一个错误,那么assert语句是应用在这种情形下的理想语句.3.当assert语句失败的时候,会引发一AssertionError. 转自:http://blog.sina.com.cn/s/blog_76e94d210100vz37.html

thymeleaf中的th:assert用法

th:assert 断言标签 th:assert属性可以指定一个以逗号分隔的表达式对其进行评估并生产适用于每一个评价,如果不抛出异常 <div th:assert="${onevar},(${twovar} != 43)">...</div> 这方便验证参数的一个片断签名 <header th:fragment="contentheader(title)" th:assert="${!#strings.isEmpty(titl

断言assert用法

本文转自:http://blog.jobbole.com/76285/ 这个问题是如何在一些场景下使用断言表达式,通常会有人误用它,所以我决定写一篇文章来说明何时使用断言,什么时候不用. 为那些还不清楚它的人,Python的assert是用来检查一个条件,如果它为真,就不做任何事.如果它为假,则会抛出AssertError并且包含错误信息.例如: 1 2 3 4 5 6 py> x = 23 py> assert x > 0, "x is not zero or negativ

c assert 用法

#include <stdio.h> /* printf */ #include <assert.h> /* assert */ void print_number(int* myInt) { assert (myInt!=NULL); printf ("%d\n",*myInt); } int main () { int a=10; int * b = NULL; int * c = NULL; b=&a; print_number (b); prin