JUnit 4.x – Testing Framework : Assertion Example
Assertion Example |
AssertionTest.java
package com.javaskool;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class AssertionTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void test() {
//test data
String str1 = new String ("hello");
String str2 = new String ("hello");
String str3 = null;
String str4 = "hello";
String str5 = "hello";
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);
}
}
MyAssertRunner.java
package com.javaskool;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class MyAssertRunner {
/**
* @param args
*/
public static void main(String[] args) {
Result result = JUnitCore.runClasses(AssertionTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
Downloads Examples |
Click here to Download Assertion JUnit Test Example
Click here to Download Annotation based JUnit Example
Click here to Download Shopping Cart JUnit Example
Recent Comments