This commit is contained in:
alincojanu 2017-10-04 21:08:28 +03:00 committed by Grzegorz Piwowarek
parent 8a77746e4d
commit eaba521fdf
7 changed files with 127 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.junit;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class AdditionTest {
Calculator calculator = new Calculator();
@Test
public void testAddition() {
assertEquals("addition", 8, calculator.add(5, 3));
}
}

View File

@ -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);
}
}

View File

@ -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 CalculatorTest {
Calculator calculator = new Calculator();
@Test
public void testAddition() {
assertEquals("addition", 8, calculator.add(5, 3));
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.junit;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class SubstractionTest {
Calculator calculator = new Calculator();
@Test
public void substraction() {
assertEquals("substraction", 2, calculator.sub(5, 3));
}
}

View File

@ -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({
AdditionTest.class,
SubstractionTest.class})
public class SuiteTest {
}

View File

@ -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);
}
}
}