Assertions
12691 단어 JUnit
특히 언급해야 할 단언은 assertThat인데, 그 매개 변수는 ([실패할 때 출력된 문자열 메시지], 실제 값,Matcher 대상) 이고, 매개 변수의 순서는 다른 단언 방법과 정반대이다.또한 Matcher 객체를 생성하려면 org가 필요합니다.hamcrest.CoreMatchers.*"안에 있는 방법이기 때문에 assertThat을 사용할 때hamcrest-core.jar와hamcrest-library.jar(다운로드 주소:http://search.maven.org/#search|ga|1|g%3Aorg.hamcrest).AssertThat에 대한 자세한 내용은 "Matchers and assertthat"을 참조하십시오.
Hamcrest가 뭘까요?공식 홈페이지에는 amcrest is a library of matchers, which can be combined in to create flexible expressions of intent in tests가 있습니다.내 이해에 따르면 Hamcrest는 Library이다. 이것은 매칭 부호인 Matcher를 제공했다. 이런 매칭 부호는 읽을 수 있고 유연하기 때문에 JUnit4는Hamcrest 프레임워크를 도입했다.
Hamcrest 홈페이지:http://hamcrest.org/.
Hamcrest 홈 페이지:http://code.google.com/p/hamcrest/wiki/Tutorial
Hamcrest 전체 패키지의 GitHub 다운로드 주소:https://github.com/hamcrest/JavaHamcrest
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
void
assertThat(String reason, T actual, Matcher super T> matcher)
Asserts that actual
satisfies the condition specified by matcher
. static
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)
홈페이지의 단언은 다음과 같다.
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import java.util.Arrays;
import org.hamcrest.core.CombinableMatcher;
import org.junit.Test;
public class AssertTests {
@Test
public void testAssertArrayEquals(){
byte[] expected="trial".getBytes();
byte[] actual="trial".getBytes();
assertArrayEquals("failure-byte arrays not same",expected, actual);
}
@Test
public void testAssertEquals(){
assertEquals("failure-strings are not equal","test","test");
}
@Test
public void testAssertFalse(){
assertFalse("failure-should be false",false);
}
@Test
public void testAssertNotNull(){
assertNotNull("should not be null",new Object());
}
@Test
public void testAssertNotSame(){
assertNotSame("should not be same object",new Object(),new String("hello"));
}
@Test
public void testAssertNull(){
assertNull("should be null",null);
}
@Test
public void testAssertSame(){
Integer aNumber=Integer.valueOf(78);
assertSame("should be the same",aNumber,aNumber);
}
// JUnit Matchers assertThat
@Test
public void testAssertThatBothContainsString() {
org.junit.Assert.assertThat("albumen", both(containsString("a")).and(containsString("b")));
}
@Test
public void testAssertThathasItemsContainsString() {
org.junit.Assert.assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
}
@Test
public void testAssertThatEveryItemContainsString() {
org.junit.Assert.assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n")));
}
// Core Hamcrest Matchers with assertThat
@Test
public void testAssertThatHamcrestCoreMatchers() {
assertThat("good", allOf(equalTo("good"), startsWith("good")));
assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));
assertThat("good", anyOf(equalTo("bad"), equalTo("good")));
assertThat(7, not(CombinableMatcher. either(equalTo(3)).or(equalTo(4))));
assertThat(new Object(), not(sameInstance(new Object())));
}
@Test
public void testAssertTrue() {
assertTrue("failure - should be true", true);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
책 「테스트 구동 개발」을 사경하기 위한 환경 구축우선 사용할 수 없으면 시작되지 않는다. 나중에 점점 사용할 수 있게 될 것이다. 에서 Eclipse IDE for Java Developers를 다운로드하여 설치하십시오. 이제 첫 화면이 나온다. 시작 후 화면에서...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.