上两篇学习Junit的文章,主要说了一些Junit的基本运行类,这篇文章博主认为才是走进Junit的开山篇。博主年龄比较大,也比较愚钝,学习这款大湿们认为最简单的开源框架,博主学习很费力,但是学习Junit的时候不得不承认收获不少,所以,只有用黑白文字用说不太清楚的方式记录下来整个学习路程,虽然今天想放弃继续更新blog,但是,还是坚持为好,步入正题。
还是老习惯以一个Junit自带案例入手,作为开山篇,我们简略了解一下JunitCore这个Junit的核心类,案例如下:
package org.junit.tests.deprecated;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
/**
* @deprecated This is a simple smoke test to make sure the old JUnit4ClassRunner basically works.
* Delete this test when JUnit4ClassRunner goes to the Great Heap In The Sky.
*/
@Deprecated
public class JUnit4ClassRunnerTest {
@SuppressWarnings("deprecation")
@RunWith(JUnit4ClassRunner.class)
public static class Example {
@Test
public void success() {
}
@Test
public void failure() {
fail();
}
}
@Test
public void runWithOldJUnit4ClassRunner() {
Result result = JUnitCore.runClasses(Example.class);
assertThat(result.getRunCount(), is(2));
assertThat(result.getFailureCount(), is(1));
}
@SuppressWarnings("deprecation")
@RunWith(JUnit4ClassRunner.class)
public static class UnconstructableExample {