(译) 08-JUnit-使用断言
2018-03-15 16:44
281 查看
断言
所有断言都在 Assert 类中。public class Assert extends java.lang.Object此类提供了一组断言方法, 用于编写测试。仅记录失败的断言。断言类的一些重要方法如下−
Method Summary | ||
---|---|---|
static void | assertArrayEquals(boolean[] expecteds, boolean[] actuals) Asserts that two boolean arrays are equal. | |
static void | assertArrayEquals(byte[] expecteds, byte[] actuals) Asserts that two byte arrays are equal. | |
static void | assertArrayEquals(char[] expecteds, char[] actuals) Asserts that two char arrays are equal. | |
static void | assertArrayEquals(double[] expecteds, double[] actuals, double delta) Asserts that two double arrays are equal. | |
static void | assertArrayEquals(float[] expecteds, float[] actuals, float delta) Asserts that two float arrays are equal. | |
static void | assertArrayEquals(int[] expecteds, int[] actuals) Asserts that two int arrays are equal. | |
static void | assertArrayEquals(long[] expecteds, long[] actuals) Asserts that two long arrays are equal. | |
static void | assertArrayEquals(Object[] expecteds, Object[] actuals) Asserts that two object arrays are equal. | |
static void | assertArrayEquals(short[] expecteds, short[] actuals) Asserts that two short arrays are equal. | |
static void | assertArrayEquals(String message, boolean[] expecteds, boolean[] actuals) Asserts that two boolean arrays are equal. | |
static void | assertArrayEquals(String message, byte[] expecteds, byte[] actuals) Asserts that two byte arrays are equal. | |
static void | assertArrayEquals(String message, char[] expecteds, char[] actuals) Asserts that two char arrays are equal. | |
static void | assertArrayEquals(String message, double[] expecteds, double[] actuals, double delta) Asserts that two double arrays are equal. | |
static void | assertArrayEquals(String message, float[] expecteds, float[] actuals, float delta) Asserts that two float arrays are equal. | |
static void | assertArrayEquals(String message, int[] expecteds, int[] actuals) Asserts that two int arrays are equal. | |
static void | assertArrayEquals(String message, long[] expecteds, long[] actuals) Asserts that two long arrays are equal. | |
static void | assertArrayEquals(String message, Object[] expecteds, Object[] actuals) Asserts that two object arrays are equal. | |
static void | assertArrayEquals(String message, short[] expecteds, short[] actuals) Asserts that two short arrays are equal. | |
static void | assertEquals(double expected, double actual) Deprecated. Use assertEquals(double expected, double actual, double delta)instead | |
static void | assertEquals(double expected, double actual, double delta) Asserts that two doubles are equal to within a positive delta. | |
static void | assertEquals(float expected, float actual, float delta) Asserts that two floats are equal to within a positive delta. | |
static void | assertEquals(long expected, long actual) Asserts that two longs are equal. | |
static void | assertEquals(Object[] expecteds, Object[] actuals) Deprecated. use assertArrayEquals | |
static void | assertEquals(Object expected, Object actual) Asserts that two objects are equal. | |
static void | assertEquals(String message, double expected, double actual) Deprecated. Use assertEquals(String message, double expected, double actual, double delta)instead | |
static void | assertEquals(String message, double expected, double actual, double delta) Asserts that two doubles are equal to within a positive delta. | |
static void | assertEquals(String message, float expected, float actual, float delta) Asserts that two floats are equal to within a positive delta. | |
static void | assertEquals(String message, long expected, long actual) Asserts that two longs are equal. | |
static void | assertEquals(String message, Object[] expecteds, Object[] actuals) Deprecated. use assertArrayEquals | |
static void | assertEquals(String message, Object expected, Object actual) Asserts that two objects are equal. | |
static void | assertFalse(boolean condition) Asserts that a condition is false. | |
static void | assertFalse(String message, boolean condition) Asserts that a condition is false. | |
static void | assertNotEquals(double unexpected, double actual, double delta) Asserts that two doubles are not equal to within a positive delta. | |
static void | assertNotEquals(float unexpected, float actual, float delta) Asserts that two floats are not equal to within a positive delta. | |
static void | assertNotEquals(long unexpected, long actual) Asserts that two longs are not equals. | |
static void | assertNotEquals(Object unexpected, Object actual) Asserts that two objects are not equals. | |
static void | assertNotEquals(String message, double unexpected, double actual, double delta) Asserts that two doubles are not equal to within a positive delta. | |
static void | assertNotEquals(String message, float unexpected, float actual, float delta) Asserts that two floats are not equal to within a positive delta. | |
static void | assertNotEquals(String message, long unexpected, long actual) Asserts that two longs are not equals. | |
static void | assertNotEquals(String message, Object unexpected, Object actual) Asserts that two objects are not equals. | |
static void | assertNotNull(Object object) Asserts that an object isn't null. | |
static void | assertNotNull(String message, Object object) Asserts that an object isn't null. | |
static void | assertNotSame(Object unexpected, Object actual) Asserts that two objects do not refer to the same object. | |
static void | assertNotSame(String message, Object unexpected, Object actual) Asserts that two objects do not refer to the same object. | |
static void | assertNull(Object object) Asserts that an object is null. | |
static void | assertNull(String message, Object object) Asserts that an object is null. | |
static void | assertSame(Object expected, Object actual) Asserts that two objects refer to the same object. | |
static void | assertSame(String message, Object expected, Object actual) Asserts that two objects refer to the same object. | |
static
| assertThat(String reason, T actual, Matcher<? super T> matcher) Asserts that actualsatisfies the condition specified by matcher. | |
static
| assertThat(T actual, Matcher<? super T> matcher) Asserts that actualsatisfies the condition specified by matcher. | |
static void | assertTrue(boolean condition) Asserts that a condition is true. | |
static void | assertTrue(String message, boolean condition) Asserts that a condition is true. | |
static void | fail() Fails a test with no message. | |
static void | fail(String message) Fails a test with the given message. |
在示例中, 让我们使用上面提到的一些方法。在 C: \ > JUNIT_WORKSPACE 中创建名为TestAssertions的 java 类文件。import org.junit.Test;
import static org.junit.Assert.*;
public class TestAssertions {
@Test
public void testAssertions() {
//test data
String str1 = new String ("abc");
String str2 = new String ("abc");
String str3 = null;
String str4 = "abc";
String str5 = "abc";
int val1 = 5;
int val2 = 6;
String[] expectedArray = {"one", "two", "three"};
String[] resultArray = {"one", "two", "three"};
//Check that two objects are equal
assertEquals(str1, str2);
//Check that a condition is true
assertTrue (val1 < val2);
//Check that a condition is false
assertFalse(val1 > val2);
//Check that an object isn't null
assertNotNull(str1);
//Check that an object is null
assertNull(str3);
//Check if two object references point to the same object
assertSame(str4,str5);
//Check if two object references not point to the same object
assertNotSame(str1,str3);
//Check whether two arrays are equal to each other.
assertArrayEquals(expectedArray, resultArray);
}
}
接下来, 在 C: \ > JUNIT_WORKSPACE 中创建名为TestRunner的 java 类文件以执行测试用例。import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner2 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestAssertions.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
使用 javac 编译测试用例和测试运行流类。C:\JUNIT_WORKSPACE>javac TestAssertions.java TestRunner.java
现在运行测试运行程序, 它将在提供的测试用例类中定义测试用例。C:\JUNIT_WORKSPACE>java TestRunner
验证输出。true注解注解类似于可以添加到代码中的元标记, 并将它们应用于方法或类中。JUnit 中的这些注释提供了有关测试方法的以下信息−
在测试方法之前和之后将运行哪些方法。
在所有方法之前和之后运行的方法, 以及。
在执行过程中将忽略哪些方法或类。
下表提供了在 JUnit −中的注释及其含义的列表。
Sr 号 | 注释 & 说明 |
---|---|
1 | @Test测试注释告诉 JUnit, 它附加到public void 方法可以作为测试用例运行。 |
2 | @Before几个测试需要创建类似的对象才能运行。使用 @Before 对public void 方法进行批注会导致在每个测试方法之前运行该方法。 |
3 | @After如果在以前的方法中分配外部资源, 则需要在测试运行后释放它们。使用 @After 对public void 方法进行批注会导致在测试方法之后运行该方法。 |
4 | @BeforeClass使用 @BeforeClass public static void 方法进行注释会导致它在类中的任何测试方法之前运行一次(一般一个TestCase类只执行一次) |
5 | @AfterClass这将在所有测试完成后执行该方法。这可用于执行清理活动。(一般一个TestCase类只执行一次) |
6 | @Ignore忽略注释用于忽略测试, 并且不会执行测试。 |
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class JunitAnnotation {
//execute before class
@BeforeClass
public static void beforeClass() {
System.out.println("in before class");
}
//execute after class
@AfterClass
public static void afterClass() {
System.out.println("in after class");
}
//execute before test
@Before
public void before() {
System.out.println("in before");
}
//execute after test
@After
public void after() {
System.out.println("in after");
}
//test case
@Test
public void test() {
System.out.println("in test");
}
//test case ignore and will not execute
@Ignore
public void ignoreTest() {
System.out.println("in ignore test");
}
}
接下来, 在 C: \ > JUNIT_WORKSPACE 中创建名为TestRunner的 java 类文件以执行批注。import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(JunitAnnotation.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
使用 javac 编译测试用例和测试运行流类。C:\JUNIT_WORKSPACE>javac JunitAnnotation.java TestRunner.java
现在运行测试运行程序, 它将执行在提供的测试用例类中定义的测试用例。C:\JUNIT_WORKSPACE>java TestRunner
验证输出。in before class
in before
in test
in after
in after class
true翻译自:https://www.tutorialspoint.com/junit/junit_using_assertion.htm
相关文章推荐
- JUnit中使用Hamcrest测试框架的assertThat断言【4.4新特性】
- JUnit - 使用断言
- 使用junit Assert 断言来判断参数是否为空
- Java序列化(Serializable)、Junit及断言使用
- junit 断言assertThat使用
- JUnit中使用Hamcrest测试框架的assertThat断言
- junit使用及断言
- 10.Android UiAutomator Junit 断言函数的使用
- 2018_03_08 搭建基础mybatis框架,以及使用junit测试crud功能
- Junit框架使用(4)--JUnit常用断言及注解
- 编程规范之Junit测试断言使用
- Junit框架使用(4)--JUnit常用断言及注解
- JUnit中使用Hamcrest测试框架的assertThat断言【4.4新特性】
- JUnit-4.11使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误
- java Junit的各种断言
- iOS断言 NSAssert的使用
- 立此存照(13)[C++]C++中的assert断言使用示例
- Junit和Ant入门(三) Junit和Ant的混合使用
- 自动化测试断言Assent的使用
- 在VC程序中使用调试语句----(一)断言