Close

JUnit - JUnitCore Examples

JUnit 

This example shows how to run JUnit test programmatically by using JUnitCore

package com.logicbig.example;

import org.junit.*;

public class LifeCycleTest {

public LifeCycleTest() {
System.out.printf("Constructor invoked. Instance: %s%n", this);
}

@BeforeClass
public static void beforeClassMethod() {
System.out.println("@BeforeClass static method invoked.");
}

@Test
public void test1() {
System.out.printf("@Test method 1 invoked. Instance: %s%n", this);
}

@Test
public void test2() {
System.out.printf("@Test method 2 invoked. Instance: %s%n", this);
}

@Before
public void beforeMethod() {
System.out.printf("@Before method invoked. Instance: %s%n", this);
}

@After
public void afterMethod() {
System.out.printf("@After method invoked. Instance: %s%n", this);
}

@AfterClass
public static void afterClassMethod() {
System.out.printf("@AfterClass static method invoked.%n");
}
}
package com.logicbig.example;

import org.junit.runner.JUnitCore;

public class ProgrammaticTestRunner {

public static void main(String[] args) {
JUnitCore junit = new JUnitCore();
junit.run(LifeCycleTest.class);
System.out.println("----------------");
junit.run(LifeCycleTest.class);
}
}
Original Post




package com.logicbig.example;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;

public class RunClassesExample {

public static void main(String[] args) {
JUnitCore jUnitCore = new JUnitCore();
Result result = jUnitCore.run(TestExample.class);
Util.printResult(result);
}
}
Original Post




package com.logicbig.example;

import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;

public class RunRequestExample {

public static void main(String[] args) {
JUnitCore jUnitCore = new JUnitCore();
Request request = Request.aClass(TestExample.class);
Result result = jUnitCore.run(request);
Util.printResult(result);
}
}
Original Post




package com.logicbig.example;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;

public class RunClassesStaticExample {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestExample.class);
Util.printResult(result);
}
}
Original Post
package com.logicbig.example;

import org.junit.experimental.ParallelComputer;
import org.junit.runner.Computer;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;

public class RunComputerExample {
public static void main(String[] args) {
runSerial();
runParallel();
}

private static void runSerial() {
System.out.println("--------\nRunning Serial");
JUnitCore jUnitCore = new JUnitCore();
Result run = jUnitCore.run(Computer.serial(), TestExample2.class);
Util.printResult(run);
}

private static void runParallel() {
System.out.println("--------\nRunning parallel");
JUnitCore jUnitCore = new JUnitCore();
Result result = jUnitCore.run(ParallelComputer.methods(),
TestExample2.class);
Util.printResult(result);
}
}
Original Post




See Also