您的位置:首页 > 其它

(译) 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
<T> void
assertThat(String reason, T actual, Matcher<? super T> matcher)
 
          Asserts that 
actual
 satisfies the condition specified by 
matcher
.
static
<T> void
assertThat(T actual, Matcher<? super T> matcher)
 
          Asserts that 
actual
 satisfies 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.
参考:https://junit.org/junit4/javadoc/latest/org/junit/Assert.html
在示例中, 让我们使用上面提到的一些方法。在 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忽略注释用于忽略测试, 并且不会执行测试。
在 C: \ > JUNIT_WORKSPACE 中创建名为JunitAnnotation的 java 类文件以测试批注。import org.junit.After;
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: