Skip to content

Instantly share code, notes, and snippets.

@tuphamphuong
Created January 12, 2016 13:47
Show Gist options
  • Save tuphamphuong/3a8805f9ba268d6e54bd to your computer and use it in GitHub Desktop.
Save tuphamphuong/3a8805f9ba268d6e54bd to your computer and use it in GitHub Desktop.
Basic JUnit
package controllers;
import org.junit.*;
import static org.junit.Assert.*;
/**
* Created by Tu Pham Phuong - phamptu@gmail.com on 1/11/16.
*/
public class BasicTest {
@BeforeClass
public static void oneTimeSetUp() {
// one-time initialization code
System.out.println("@BeforeClass - oneTimeSetUp");
}
@AfterClass
public static void oneTimeTearDown() {
// one-time cleanup code
System.out.println("@AfterClass - oneTimeTearDown");
}
@Before
public void setUp() {
System.out.println("@Before - setUp");
}
@After
public void tearDown() {
System.out.println("@After - tearDown");
}
@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);
}
@Test(expected = ArithmeticException.class)
public void divisionWithException() {
int i = 1/0;
}
@Ignore("Not Ready to Run")
@Test
public void divisionWithExceptionIgnore() {
System.out.println("Method is not ready yet");
}
@Test(timeout = 1000)
public void infinity() {
while (true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment