re-organize testing modules
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
public class Calculator {
|
||||
public int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public int sub(int a, int b) {
|
||||
return a - b;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class AdditionUnitTest {
|
||||
Calculator calculator = new Calculator();
|
||||
|
||||
@Test
|
||||
public void testAddition() {
|
||||
assertEquals("addition", 8, calculator.add(5, 3));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.hamcrest.core.IsCollectionContaining.hasItems;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Unit test that demonstrate the different assertions available within JUnit 4
|
||||
*/
|
||||
public class AssertionsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenAssertingEquality_thenEqual() {
|
||||
String expected = "Baeldung";
|
||||
String actual = "Baeldung";
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAssertingEqualityWithMessage_thenEqual() {
|
||||
String expected = "Baeldung";
|
||||
String actual = "Baeldung";
|
||||
|
||||
assertEquals("failure - strings are not equal", expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAssertingArraysEquality_thenEqual() {
|
||||
char[] expected = { 'J', 'u', 'n', 'i', 't' };
|
||||
char[] actual = "Junit".toCharArray();
|
||||
|
||||
assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNullArrays_whenAssertingArraysEquality_thenEqual() {
|
||||
int[] expected = null;
|
||||
int[] actual = null;
|
||||
|
||||
assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAssertingNull_thenTrue() {
|
||||
Object car = null;
|
||||
|
||||
assertNull("The car should be null", car);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAssertingNotNull_thenTrue() {
|
||||
Object car = new Object();
|
||||
|
||||
assertNotNull("The car should not be null", car);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAssertingNotSameObject_thenDifferent() {
|
||||
Object cat = new Object();
|
||||
Object dog = new Object();
|
||||
|
||||
assertNotSame(cat, dog);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAssertingSameObject_thenSame() {
|
||||
Object cat = new Object();
|
||||
|
||||
assertSame(cat, cat);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAssertingConditions_thenVerified() {
|
||||
assertTrue("5 is greater then 4", 5 > 4);
|
||||
assertFalse("5 is not greater then 6", 5 > 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_thenNotFailed() {
|
||||
try {
|
||||
methodThatShouldThrowException();
|
||||
fail("Exception not thrown");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
assertEquals("Operation Not Supported", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void methodThatShouldThrowException() {
|
||||
throw new UnsupportedOperationException("Operation Not Supported");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssertThatHasItems() {
|
||||
assertThat(Arrays.asList("Java", "Kotlin", "Scala"), hasItems("Java", "Kotlin"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.runners.BlockJUnit4ClassRunner;
|
||||
import org.junit.runners.model.FrameworkMethod;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
public class BlockingTestRunner extends BlockJUnit4ClassRunner {
|
||||
public BlockingTestRunner(Class<?> klass) throws InitializationError {
|
||||
super(klass);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Statement methodInvoker(FrameworkMethod method, Object test) {
|
||||
System.out.println("invoking: " + method.getName());
|
||||
return super.methodInvoker(method, test);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class CalculatorUnitTest {
|
||||
Calculator calculator = new Calculator();
|
||||
|
||||
@Test
|
||||
public void testAddition() {
|
||||
assertEquals("addition", 8, calculator.add(5, 3));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class SubstractionUnitTest {
|
||||
Calculator calculator = new Calculator();
|
||||
|
||||
@Test
|
||||
public void substraction() {
|
||||
assertEquals("substraction", 2, calculator.sub(5, 3));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import org.junit.runners.Suite.SuiteClasses;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({
|
||||
AdditionUnitTest.class,
|
||||
SubstractionUnitTest.class})
|
||||
public class SuiteUnitTest {
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.junit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runner.Runner;
|
||||
import org.junit.runner.notification.RunNotifier;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class TestRunner extends Runner {
|
||||
|
||||
private Class testClass;
|
||||
public TestRunner(Class testClass) {
|
||||
super();
|
||||
this.testClass = testClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Description getDescription() {
|
||||
return Description.createTestDescription(testClass, "My runner description");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(RunNotifier notifier) {
|
||||
System.out.println("running the tests from MyRunner: " + testClass);
|
||||
try {
|
||||
Object testObject = testClass.newInstance();
|
||||
for (Method method : testClass.getMethods()) {
|
||||
if (method.isAnnotationPresent(Test.class)) {
|
||||
notifier.fireTestStarted(Description
|
||||
.createTestDescription(testClass, method.getName()));
|
||||
method.invoke(testObject);
|
||||
notifier.fireTestFinished(Description
|
||||
.createTestDescription(testClass, method.getName()));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user