summaryrefslogtreecommitdiffstats
path: root/junit4/src/test/java/org/junit/tests/running/core/CommandLineTest.java
blob: cdf8b5547db5a8e6a5a60b2112d95be460701a3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package org.junit.tests.running.core;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.internal.RealSystem;
import org.junit.runner.JUnitCore;

public class CommandLineTest {
	private ByteArrayOutputStream results;
	private PrintStream oldOut;
	private static boolean testWasRun;

	@Before public void before() { 
		oldOut= System.out;
		results= new ByteArrayOutputStream();
		System.setOut(new PrintStream(results));
	}

	@After public void after() {
		System.setOut(oldOut);
	}

	static public class Example {
		@Test public void test() { 
			testWasRun= true; 
		}
	}

	@Test public void runATest() {
		testWasRun= false; // todo create a TestSystem instead
		new JUnitCore().runMain(new RealSystem(), new String[]{"org.junit.tests.running.core.CommandLineTest$Example"});
		assertTrue(testWasRun);
	}
	
	@Test public void runAClass() {
		testWasRun= false;
		JUnitCore.runClasses(Example.class);
		assertTrue(testWasRun);		
	}

	private static int fCount;

	static public class Count {
		@Test public void increment() {
			fCount++;
		}
	}
	
	@Test public void runTwoClassesAsArray() {
		fCount= 0;
		JUnitCore.runClasses(new Class[] {Count.class, Count.class});
		assertEquals(2, fCount);		
	}

	@Test public void runTwoClasses() {
		fCount= 0;
		JUnitCore.runClasses(Count.class, Count.class);
		assertEquals(2, fCount);		
	}
}