{"id":10006650,"date":"2022-10-08T19:36:47","date_gmt":"2022-10-08T11:36:47","guid":{"rendered":"https:\/\/ityet.com\/?p=10006650"},"modified":"2022-10-08T19:36:47","modified_gmt":"2022-10-08T11:36:47","slug":"%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%e8%bf%90%e8%a1%8c%e5%8e%9f%e7%90%86%e6%8e%a2%e7%a9%b6","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006650","title":{"rendered":"\u5355\u5143\u6d4b\u8bd5\u8fd0\u884c\u539f\u7406\u63a2\u7a76"},"content":{"rendered":"<p>\u7b80\u4ecb\uff1a\u00a0\u5355\u5143\u6d4b\u8bd5\u662f\u8f6f\u4ef6\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7684\u91cd\u8981\u4e00\u73af\uff0c\u597d\u7684\u5355\u6d4b\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u65e9\u7684\u53d1\u73b0\u95ee\u9898\uff0c\u4e3a\u7cfb\u7edf\u7684\u7a33\u5b9a\u8fd0\u884c\u63d0\u4f9b\u4fdd\u969c\u3002\u5355\u6d4b\u8fd8\u662f\u5f88\u597d\u7684\u8bf4\u660e\u6587\u6863\uff0c\u6211\u4eec\u5f80\u5f80\u770b\u5355\u6d4b\u7528\u4f8b\u5c31\u80fd\u591f\u4e86\u89e3\u5230\u4f5c\u8005\u5bf9\u7c7b\u7684\u8bbe\u8ba1\u610f\u56fe\u3002\u4ee3\u7801\u91cd\u6784\u65f6\u4e5f\u79bb\u4e0d\u5f00\u5355\u6d4b\uff0c\u4e30\u5bcc\u7684\u5355\u6d4b\u7528\u4f8b\u4f1a\u4f7f\u6211\u4eec\u91cd\u6784\u4ee3\u7801\u65f6\u4fe1\u5fc3\u6ee1\u6ee1\u3002\u867d\u7136\u5355\u6d4b\u5982\u6b64\u91cd\u8981\uff0c\u4f46\u662f\u4e00\u76f4\u6765\u90fd\u4e0d\u662f\u5f88\u6e05\u695a\u5176\u8fd0\u884c\u539f\u7406\uff0c\u4e5f\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\u8981\u505a\u8fd9\u6837\u6216\u90a3\u6837\u7684\u914d\u7f6e\uff0c\u8fd9\u6837\u7ec8\u7a76\u662f\u4e0d\u884c\u7684\uff0c\u4e8e\u662f\u51c6\u5907\u82b1\u65f6\u95f4\u63a2\u7a76\u4e0b\u5355\u6d4b\u539f\u7406\uff0c\u5e76\u5728\u6b64\u8bb0\u5f55\u3002\u524d\u8a00\u5355\u5143\u6d4b\u8bd5\u662f\u8f6f\u4ef6\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7684\u91cd\u8981\u4e00\u73af\uff0c\u597d\u7684\u5355\u6d4b\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u65e9\u7684\u53d1\u73b0\u95ee\u9898\uff0c\u4e3a\u7cfb\u7edf\u7684\u7a33\u5b9a\u8fd0\u884c\u63d0\u4f9b\u4fdd\u969c\u3002\u5355\u6d4b\u8fd8\u662f\u5f88\u597d\u7684\u8bf4\u660e\u6587\u6863\uff0c\u6211\u4eec\u5f80\u5f80\u770b\u5355\u6d4b\u7528\u4f8b\u5c31\u80fd\u591f\u4e86\u89e3\u5230\u4f5c\u8005\u5bf9\u7c7b\u7684\u8bbe\u8ba1\u610f\u56fe\u3002\u4ee3\u7801\u91cd\u6784\u65f6\u4e5f\u79bb\u4e0d\u5f00\u5355\u6d4b\uff0c\u4e30\u5bcc\u7684\u5355\u6d4b\u7528\u4f8b\u4f1a\u4f7f\u6211\u4eec\u91cd\u6784\u4ee3\u7801\u65f6\u4fe1\u5fc3\u6ee1\u6ee1\u3002\u867d\u7136\u5355\u6d4b\u5982\u6b64\u91cd\u8981\uff0c\u4f46\u662f\u4e00\u76f4\u6765\u90fd\u4e0d\u662f\u5f88\u6e05\u695a\u5176\u8fd0\u884c\u539f\u7406\uff0c\u4e5f\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\u8981\u505a\u8fd9\u6837\u6216\u90a3\u6837\u7684\u914d\u7f6e\uff0c\u8fd9\u6837\u7ec8\u7a76\u662f\u4e0d\u884c\u7684\uff0c\u4e8e\u662f\u51c6\u5907\u82b1\u65f6\u95f4\u63a2\u7a76\u4e0b\u5355\u6d4b\u539f\u7406\uff0c\u5e76\u5728\u6b64\u8bb0\u5f55\u3002\u5f53\u5728IDEA\u4e2dRun\u5355\u5143\u6d4b\u8bd5\u65f6\u53d1\u751f\u4e86\u4ec0\u4e48\uff1f<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042582646\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u9996\u5148\uff0c\u6765\u770b\u4e00\u4e0b\u5f53\u6211\u4eec\u76f4\u63a5\u901a\u8fc7IDEA\u8fd0\u884c\u5355\u4f8b\u65f6\uff0cIDEA\u5e2e\u5fd9\u505a\u4e86\u54ea\u4e9b\u4e8b\u60c5\uff1a\u5c06\u5de5\u7a0b\u6e90\u7801\u548c\u6d4b\u8bd5\u6e90\u7801\u8fdb\u884c\u7f16\u8bd1\uff0c\u8f93\u51fa\u5230\u4e86target\u76ee\u5f55\u901a\u8fc7java\u547d\u4ee4\u8fd0\u884ccom.intellij.rt.junit.JUnitStarter\uff0c\u53c2\u6570\u4e2d\u6307\u5b9a\u4e86junit\u7684\u7248\u672c\u4ee5\u53ca\u5355\u6d4b\u7528\u4f8b\u540d\u79f0java com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 fung.MyTest,test\u8fd9\u91cc\u7740\u91cd\u8ffd\u4e0bJUnitStarter\u7684\u4ee3\u7801\uff0c\u8be5\u7c7b\u5728IDEA\u63d0\u4f9b\u7684junit-rt.jar\u63d2\u4ef6\u5305\u4e2d\uff0c\u5177\u4f53\u76ee\u5f55\uff1a\/Applications\/IntelliJ IDEA.app\/Contents\/plugins\/junit\/lib\/junit-rt.jar\u3002\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u5305\u5f15\u5165\u5230\u6211\u4eec\u81ea\u5df1\u7684\u5de5\u7a0b\u9879\u76ee\u4e2d\uff0c\u65b9\u4fbf\u9605\u8bfb\u6e90\u7801\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042582647\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>JUnitStarter\u7684main\u51fd\u6570public static void main(String[] args) {<\/p>\n<pre><code>List&lt;String&gt; argList = new ArrayList(Arrays.asList(args));\nArrayList&lt;String&gt; listeners = new ArrayList();\nString[] name = new String[1];\nString agentName = processParameters(argList, listeners, name);\nif (!&quot;com.intellij.junit5.JUnit5IdeaTestRunner&quot;.equals(agentName) &amp;&amp; !canWorkWithJUnitVersion(System.err, agentName)) {\n    System.exit(-3);\n}\n\nif (!checkVersion(args, System.err)) {\n    System.exit(-3);\n}\nString[] array = (String[])argList.toArray(new String[0]);\nint exitCode = prepareStreamsAndStart(array, agentName, listeners, name[0]);\nSystem.exit(exitCode);<\/code><\/pre>\n<p>}\u8fd9\u91cc\u4e3b\u8981\u6709\u4e24\u4e2a\u6838\u5fc3\u65b9\u6cd5&#8230;<br \/>\/\/ \u5904\u7406\u53c2\u6570\uff0c\u4e3b\u8981\u7528\u6765\u786e\u5b9a\u4f7f\u7528\u54ea\u4e2a\u7248\u672c\u7684junit\u6846\u67b6\uff0c\u540c\u65f6\u6839\u636e\u5165\u53c2\u586b\u5145listeners<br \/>String agentName = processParameters(argList, listeners, name);<br \/>&#8230;<br \/>\/\/ \u542f\u52a8\u6d4b\u8bd5<br \/>int exitCode = prepareStreamsAndStart(array, agentName, listeners, name[0]);<br \/>&#8230;\u63a5\u4e0b\u6765\u770b\u4e0bprepareStreamsAndStart\u65b9\u6cd5\u8fd0\u884c\u7684\u65f6\u5e8f\u56fe\uff0c\u8fd9\u91cc\u4ee5JUnit4\u4e3a\u4f8b\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042582648\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u5f53IDEA\u786e\u8ba4\u597d\u8981\u542f\u52a8\u7684\u6846\u67b6\u7248\u672c\u540e\uff0c\u4f1a\u901a\u8fc7\u7c7b\u7684\u5168\u9650\u5b9a\u540d\u79f0\u53cd\u5c04\u521b\u5efaIdeaTestRunner&lt;?&gt;\u7684\u5b9e\u4f8b\u3002\u8fd9\u91cc\u4ee5JUnit4\u4e3a\u4f8b\uff0cIDEA\u4f1a\u5b9e\u4f8b\u5316com.intellij.junit4.JUnit4IdeaTestRunner\u7c7b\u5bf9\u8c61\u5e76\u8c03\u7528\u5176startRunnerWithArgs\u65b9\u6cd5\uff0c\u5728\u8be5\u65b9\u6cd5\u4e2d\u4f1a\u901a\u8fc7buildRequest\u65b9\u6cd5\u6784\u5efaorg.junit.runner.Request\uff0c\u901a\u8fc7getDescription\u65b9\u6cd5\u83b7\u53d6org.junit.runner.Description\uff0c\u6700\u540e\u521b\u5efaorg.junit.runner.JUnitCore\u5b9e\u4f8b\u5e76\u8c03\u7528\u5176run\u65b9\u6cd5\u3002\u7b80\u800c\u8a00\u4e4b\u5c31\u662f\uff0cIDEA\u6700\u7ec8\u4f1a\u501f\u52a9Junit4\u6846\u67b6\u7684\u80fd\u529b\u542f\u52a8\u5e76\u8fd0\u884c\u5355\u6d4b\u7528\u4f8b\uff0c\u6240\u4ee5\u63a5\u4e0b\u6765\u6709\u5fc5\u8981\u5bf9Junit4\u6846\u67b6\u7684\u6e90\u7801\u505a\u4e9b\u6df1\u5165\u7684\u63a2\u7a76\u3002Junit4\u6e90\u7801\u63a2\u7a76Junit\u662f\u4e00\u4e2a\u7531Java\u8bed\u8a00\u7f16\u5199\u7684\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\uff0c\u5df2\u5728\u4e1a\u754c\u88ab\u5e7f\u6cdb\u8fd0\u7528\uff0c\u5176\u4f5c\u8005\u662f\u5927\u540d\u9f0e\u9f0e\u7684Kent Beck\u548cErich Gamma\uff0c\u524d\u8005\u662f\u300a\u91cd\u6784\uff1a\u6539\u5584\u65e2\u6709\u4ee3\u7801\u7684\u8bbe\u8ba1\u300b\u548c\u300a\u6d4b\u8bd5\u9a71\u52a8\u5f00\u53d1\u300b\u7684\u4f5c\u8005\uff0c\u540e\u8005\u5219\u662f\u300a\u8bbe\u8ba1\u6a21\u5f0f\u300b\u7684\u4f5c\u8005\uff0cEclipse\u4e4b\u7236\u3002Junit4\u53d1\u5e03\u4e8e2006\u5e74\uff0c\u867d\u7136\u662f\u8001\u53e4\u8463\u4e86\uff0c\u4f46\u5176\u4e2d\u6240\u8574\u542b\u7684\u8bbe\u8ba1\u7406\u5ff5\u548c\u601d\u60f3\u5374\u5e76\u4e0d\u8fc7\u65f6\uff0c\u6709\u5fc5\u8981\u8ba4\u771f\u63a2\u7a76\u4e00\u756a\u3002\u9996\u5148\u6211\u4eec\u8fd8\u662f\u4ece\u4e00\u4e2a\u7b80\u5355\u7684\u5355\u6d4b\u7528\u4f8b\u5f00\u59cb\uff1apublic class MyTest {<\/p>\n<pre><code>public static void main(String[] args) {\n    JUnitCore runner = new JUnitCore();\n    Request request = Request.aClass(MyTest.class);\n    Result result = runner.run(request.getRunner());\n    System.out.println(JSON.toJSONString(result));\n}\n@Test\npublic void test1() {\n    System.out.println(&quot;test1&quot;);\n}\n@Test\npublic void test2() {\n    System.out.println(&quot;test2&quot;);\n}\n@Test\npublic void test3() {\n    System.out.println(&quot;test3&quot;);\n}<\/code><\/pre>\n<p>}\u8fd9\u91cc\u6211\u4eec\u4e0d\u518d\u901a\u8fc7IDEA\u7684\u63d2\u4ef6\u542f\u52a8\u5355\u5143\u6d4b\u8bd5\uff0c\u800c\u662f\u76f4\u63a5\u901a\u8fc7main\u51fd\u6570\uff0c\u6838\u5fc3\u4ee3\u7801\u5982\u4e0b\uff1apublic static void main(String[] args) {<br \/>  \/\/ 1. \u521b\u5efaJUnitCore\u7684\u5b9e\u4f8b<br \/>  JUnitCore runner = new JUnitCore();<br \/>  \/\/ 2. \u901a\u8fc7\u5355\u6d4b\u7c7b\u7684Class\u5bf9\u8c61\u6784\u5efaRequest<br \/>  Request request = Request.aClass(MyTest.class);<br \/>  \/\/ 3. \u8fd0\u884c\u5355\u5143\u6d4b\u8bd5<br \/>  Result result = runner.run(request.getRunner());<br \/>  \/\/ 4. \u6253\u5370\u7ed3\u679c<br \/>  System.out.println(JSON.toJSONString(result));<br \/>}\u7740\u91cd\u770b\u4e0brunner.run(request.getRunner())\uff0c\u5148\u770brun\u51fd\u7684\u4ee3\u7801\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042582649\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u53ef\u4ee5\u770b\u5230\u6700\u7ec8\u8fd0\u884c\u54ea\u79cd\u7c7b\u578b\u7684\u6d4b\u8bd5\u6d41\u7a0b\u53d6\u51b3\u4e8e\u4f20\u5165\u7684runner\u5b9e\u4f8b\uff0c\u5373\u4e0d\u540c\u7684Runner\u51b3\u5b9a\u4e86\u4e0d\u540c\u7684\u8fd0\u884c\u6d41\u7a0b\uff0c\u901a\u8fc7\u5b9e\u73b0\u7c7b\u7684\u540d\u5b57\u53ef\u4ee5\u5927\u6982\u731c\u4e00\u731c\uff0cJUnit4ClassRunner\u5e94\u8be5\u662fJUnit4\u57fa\u672c\u7684\u6d4b\u8bd5\u6d41\u7a0b\uff0cMockitoJUnitRunner\u5e94\u8be5\u662f\u5f15\u5165\u4e86Mockito\u7684\u80fd\u529b\uff0cSpringJUnit4ClassRunner\u5e94\u8be5\u548cSpring\u6709\u4e9b\u8054\u7cfb\uff0c\u53ef\u80fd\u4f1a\u542f\u52a8Spring\u5bb9\u5668\u3002\u73b0\u5728\uff0c\u6211\u4eec\u56de\u8fc7\u5934\u6765\u770b\u770brunner.run(request.getRunner())\u4e2drequest.getRunner()\u7684\u4ee3\u7801\uff1apublic Runner getRunner() {<br \/>  if (runner == null) {<\/p>\n<pre><code>synchronized (runnerLock) {\n  if (runner == null) {\n    runner = new AllDefaultPossibilitiesBuilder(canUseSuiteMethod).safeRunnerForClass(fTestClass);\n  }\n}<\/code><\/pre>\n<p>}<br \/>  return runner;<br \/>}<br \/>public Runner safeRunnerForClass(Class&lt;?&gt; testClass) {<br \/>  try {<\/p>\n<pre><code>return runnerForClass(testClass);<\/code><\/pre>\n<p>} catch (Throwable e) {<\/p>\n<pre><code>return new ErrorReportingRunner(testClass, e);<\/code><\/pre>\n<p>}<br \/>}<br \/>public Runner runnerForClass(Class&lt;?&gt; testClass) throws Throwable {<br \/>  List&lt;RunnerBuilder&gt; builders = Arrays.asList(<\/p>\n<pre><code>ignoredBuilder(),\nannotatedBuilder(),\nsuiteMethodBuilder(),\njunit3Builder(),\njunit4Builder()<\/code><\/pre>\n<p>);<br \/>  for (RunnerBuilder each : builders) {<\/p>\n<pre><code>Runner runner = each.safeRunnerForClass(testClass);\nif (runner != null) {\n  return runner;\n}<\/code><\/pre>\n<p>}<br \/>  return null;<br \/>}\u53ef\u4ee5\u770b\u5230Runner\u662f\u57fa\u4e8e\u4f20\u5165\u7684\u6d4b\u8bd5\u7c7b\uff08testClass\uff09\u7684\u4fe1\u606f\u9009\u62e9\u7684\uff0c\u8fd9\u91cc\u7684\u89c4\u5219\u5982\u4e0b\uff1a\u5982\u679c\u89e3\u6790\u5931\u8d25\u4e86\uff0c\u5219\u8fd4\u56deErrorReportingRunner\u5982\u679c\u6d4b\u8bd5\u7c7b\u4e0a\u6709@Ignore\u6ce8\u89e3\uff0c\u5219\u8fd4\u56deIgnoredClassRunner\u5982\u679c\u6d4b\u8bd5\u7c7b\u4e0a\u6709@RunWith\u6ce8\u89e3\uff0c\u5219\u4f7f\u7528@RunWith\u7684\u503c\u5b9e\u4f8b\u5316\u4e00\u4e2aRunner\u8fd4\u56de\u5982\u679ccanUseSuiteMethod=true\uff0c\u5219\u8fd4\u56deSuiteMethod\uff0c\u5176\u7ee7\u627f\u81eaJUnit38ClassRunner\uff0c\u662f\u6bd4\u8f83\u65e9\u671f\u7684JUnit\u7248\u672c\u4e86\u5982\u679cJUnit\u7248\u672c\u57284\u4e4b\u524d\uff0c\u5219\u8fd4\u56deJUnit38ClassRunner\u5982\u679c\u4e0a\u9762\u90fd\u4e0d\u6ee1\u8db3\uff0c\u5219\u8fd4\u56deBlockJUnit4ClassRunner\uff0c\u5176\u8868\u793a\u7684\u662f\u4e00\u4e2a\u6807\u51c6\u7684JUnit4\u6d4b\u8bd5\u6a21\u578b\u6211\u4eec\u5148\u524d\u4e3e\u7684\u90a3\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u8fd4\u56de\u7684\u5c31\u662fBlockJUnit4ClassRunner\uff0c\u90a3\u4e48\u5c31\u4ee5BlockJUnit4ClassRunner\u4e3a\u4f8b\uff0c\u770b\u4e0b\u5b83\u7684run\u65b9\u6cd5\u662f\u600e\u4e48\u6267\u884c\u7684\u5427\u3002\u9996\u5148\u4f1a\u5148\u8d70\u5230\u5176\u7236\u7c7bParentRunner\u4e2d\u7684run\u65b9\u6cd5@Override<br \/>public void run(final RunNotifier notifier) {<br \/>  EachTestNotifier testNotifier = new EachTestNotifier(notifier,<\/p>\n<pre><code>                                                   getDescription());<\/code><\/pre>\n<p>try {<\/p>\n<pre><code>Statement statement = classBlock(notifier);\nstatement.evaluate();<\/code><\/pre>\n<p>} catch (AssumptionViolatedException e) {<\/p>\n<pre><code>testNotifier.addFailedAssumption(e);<\/code><\/pre>\n<p>} catch (StoppedByUserException e) {<\/p>\n<pre><code>throw e;<\/code><\/pre>\n<p>} catch (Throwable e) {<\/p>\n<pre><code>testNotifier.addFailure(e);<\/code><\/pre>\n<p>}<br \/>}\u8fd9\u91cc\u6709\u5fc5\u8981\u5c55\u5f00\u8bf4\u4e0bStatement\uff0c\u5b98\u65b9\u7684\u89e3\u91ca\u662f\uff1aRepresents one or more actions to be taken at runtime in the course of running a JUnit test suite.Statement\u53ef\u4ee5\u7b80\u5355\u7406\u89e3\u4e3a\u5bf9\u53ef\u6267\u884c\u65b9\u6cd5\u7684\u5c01\u88c5\u548c\u62bd\u8c61\uff0c\u5982RunBefores\u5c31\u662f\u4e00\u4e2aStatement\uff0c\u5b83\u5c01\u88c5\u4e86\u6240\u6709\u6807\u8bb0\u4e86@BeforeClass\u6ce8\u89e3\u7684\u65b9\u6cd5\uff0c\u5728\u8fd0\u884c\u5355\u4f8b\u7c7b\u7684\u7528\u4f8b\u4e4b\u524d\u4f1a\u6267\u884c\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u8fd0\u884c\u5b8c\u540eRunBefores\u8fd8\u4f1a\u901a\u8fc7next.evaluate()\u8fd0\u884c\u540e\u7eed\u7684Statement\u3002\u8fd9\u91cc\u5217\u4e3e\u4e00\u4e0b\u5e38\u89c1\u7684Statement\uff1aRunBefores\uff0c\u4f1a\u5148\u8fd0\u884cbefores\u91cc\u5c01\u88c5\u7684\u65b9\u6cd5\uff08\u4e00\u822c\u662f\u6807\u8bb0\u4e86@BeforeClass\u6216@Before\uff09\uff0c\u518d\u8fd0\u884cnext.evaluate()RunAfters\uff0c\u4f1a\u5148\u8fd0\u884cnext.evaluate()\uff0c\u518d\u8fd0\u884cafters\u91cc\u5c01\u88c5\u7684\u65b9\u6cd5\uff08\u4e00\u822c\u662f\u6807\u8bb0\u4e86@AfterClass\u6216@After\uff09InvokeMethod\uff0c\u76f4\u63a5\u8fd0\u884ctestMethod\u4e2d\u5c01\u88c5\u7684\u65b9\u6cd5\u7531\u6b64\u53ef\u89c1\uff0c\u6574\u4e2a\u5355\u6d4b\u7684\u8fd0\u884c\u8fc7\u7a0b\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u4e00\u7cfb\u5217Statement\u7684\u8fd0\u884c\u8fc7\u7a0b\uff0c\u4ee5\u4e4b\u524d\u7684MyTest\u4e3a\u4f8b\uff0c\u5b83\u7684Statement\u7684\u6267\u884c\u8fc7\u7a0b\u5927\u81f4\u53ef\u4ee5\u6982\u51b5\u5982\u4e0b\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042582650\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u8fd8\u5269\u4e00\u4e2a\u6700\u540e\u95ee\u9898\uff0c\u5b9e\u9645\u88ab\u6d4b\u8bd5\u65b9\u6cd5\u662f\u5982\u4f55\u88ab\u8fd0\u884c\u7684\u5462\uff1f\u7b54\u6848\u662f\u53cd\u5c04\u8c03\u7528\u3002\u6838\u5fc3\u4ee3\u7801\u5982\u4e0b\uff1a@Override<br \/>public void evaluate() throws Throwable {<br \/>  testMethod.invokeExplosively(target);<br \/>}<br \/>public Object invokeExplosively(final Object target, final Object&#8230; params)<br \/>  throws Throwable {<br \/>  return new ReflectiveCallable() {<\/p>\n<pre><code>@Override\nprotected Object runReflectiveCall() throws Throwable {\n  return method.invoke(target, params);\n}<\/code><\/pre>\n<p>}.run();<br \/>}\u81f3\u6b64\u4e00\u4e2a\u6807\u51c6Junit4\u7684\u5355\u6d4b\u7528\u4f8b\u7684\u6267\u884c\u8fc7\u7a0b\u5c31\u5206\u6790\u5b8c\u4e86\uff0c\u90a3\u4e48\u50cfSpring\u8fd9\u79cd\u9700\u8981\u8d77\u5bb9\u5668\u7684\u5355\u6d4b\u53c8\u662f\u5982\u4f55\u8fd0\u884c\u7684\u5462\uff1f\u63a5\u4e0b\u6765\u5c31\u6765\u63a2\u7a76\u4e00\u4e0b\u3002Spring\u5355\u6d4b\u7684\u63a2\u7a76\u6211\u4eec\u8fd8\u662f\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5f00\u59cb\u5427@RunWith(SpringRunner.class)<br \/>@ContextConfiguration(locations = { &#8220;\/spring\/spring-mybeans.xml&#8221; })<br \/>public class SpringRunnerTest {<\/p>\n<pre><code>@Autowired\nprivate MyTestBean myTestBean;\n@Test\npublic void test() {\n    myTestBean.test();\n}<\/code><\/pre>\n<p>}\u8fd9\u91cc\u5148\u7c97\u6ee4\u7684\u6982\u62ec\u4e0b\u8fd0\u884c\u5355\u6d4b\u65f6\u53d1\u751f\u4e86\u4ec0\u4e48\u3002\u9996\u5148\uff0c@RunWith\u6ce8\u89e3\u4e86\u8be5\u6d4b\u8bd5\u7c7b\uff0c\u6240\u4ee5Junit\u6846\u67b6\u4f1a\u5148\u7528SpringRunnerTest.class\u4f5c\u4e3a\u53c2\u6570\u521b\u5efaSpringRunner\u7684\u5b9e\u4f8b\uff0c\u7136\u540e\u8c03\u7528SpringRunner\u7684run\u65b9\u6cd5\u8fd0\u884c\u6d4b\u8bd5\uff0c\u8be5\u65b9\u6cd5\u4e2d\u4f1a\u542f\u52a8Spring\u5bb9\u5668\uff0c\u52a0\u8f7d@ContextConfiguration\u6ce8\u89e3\u6307\u5b9a\u7684Bean\u914d\u7f6e\u6587\u4ef6\uff0c\u540c\u65f6\u4e5f\u4f1a\u5904\u7406@Autowired\u6ce8\u89e3\u4e3aSpringRunnerTest\u7684\u5b9e\u4f8b\u6ce8\u5165myTestBean\uff0c\u6700\u540e\u8fd0\u884ctest()\u6d4b\u8bd5\u7528\u4f8b\u3002\u7b80\u8a00\u4e4b\u5c31\u662f\u5148\u901a\u8fc7SpringRunner\u542f\u52a8Spring\u5bb9\u5668\uff0c\u7136\u540e\u8fd0\u884c\u6d4b\u8bd5\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\u63a2\u7a76\u4e00\u4e0bSpringRunner\u542f\u52a8Spring\u5bb9\u5668\u7684\u8fc7\u7a0b\u3002public final class SpringRunner extends SpringJUnit4ClassRunner {<br \/>  public SpringRunner(Class&lt;?&gt; clazz) throws InitializationError {<\/p>\n<pre><code>super(clazz);<\/code><\/pre>\n<p>}<br \/>}<br \/>public class SpringJUnit4ClassRunner extends BlockJUnit4ClassRunner {<br \/>  &#8230;<br \/>}SpringRunner\u548cSpringJUnit4ClassRunner\u5b9e\u9645\u662f\u7b49\u4ef7\u7684\uff0c\u53ef\u4ee5\u8ba4\u4e3aSpringRunner\u662fSpringJUnit4ClassRunner\u7684\u4e00\u4e2a\u522b\u540d\uff0c\u8fd9\u91cc\u7740\u91cd\u770b\u4e0bSpringJUnit4ClassRunner\u7c7b\u7684\u5b9e\u73b0\u3002SpringJUnit4ClassRunner\u7ee7\u627f\u4e86BlockJUnit4ClassRunner\uff0c\u524d\u9762\u7740\u91cd\u5206\u6790\u8fc7BlockJUnit4ClassRunner\uff0c\u5b83\u8fd0\u884c\u7684\u662f\u4e00\u4e2a\u6807\u51c6\u7684JUnit4\u6d4b\u8bd5\u6a21\u578b\uff0cSpringJUnit4ClassRunner\u5219\u662f\u5728\u6b64\u57fa\u7840\u4e0a\u505a\u4e86\u4e00\u4e9b\u6269\u5c55\uff0c\u6269\u5c55\u7684\u5185\u5bb9\u4e3b\u8981\u5305\u62ec\uff1a\u6269\u5c55\u4e86\u6784\u9020\u51fd\u6570\uff0c\u591a\u521b\u5efa\u4e86\u4e00\u4e2aTestContextManager\u5b9e\u4f8b\u3002\u6269\u5c55\u4e86createTest()\u65b9\u6cd5\uff0c\u4f1a\u989d\u5916\u8c03\u7528TestContextManager\u7684prepareTestInstance\u65b9\u6cd5\u3002\u6269\u5c55\u4e86beforeClass\uff0c\u5728\u6267\u884c@BeforeClass\u6ce8\u89e3\u7684\u65b9\u6cd5\u524d\uff0c\u4f1a\u5148\u8c03\u7528TestContextManager\u7684beforeTestClass\u65b9\u6cd5\u3002\u6269\u5c55\u4e86before\uff0c\u5728\u6267\u884c@Before\u6ce8\u89e3\u7684\u65b9\u6cd5\u524d\uff0c\u4f1a\u5148\u8c03\u7528TestContextManager\u7684beforeTestMethod\u65b9\u6cd5\u3002\u6269\u5c55\u4e86afterClass\uff0c\u5728\u6267\u884c@AfterClass\u6ce8\u89e3\u7684\u65b9\u6cd5\u4e4b\u540e\uff0c\u4f1a\u518d\u8c03\u7528TestContextManager\u7684afterTestClass\u65b9\u6cd5\u3002\u6269\u5c55\u4e86after\uff0c\u5728\u6267\u884c@After\u6ce8\u89e3\u7684\u65b9\u6cd5\u4e4b\u540e\uff0c\u4f1a\u518d\u8c03\u7528TestContextManager\u7684after\u65b9\u6cd5\u3002TestContextManager\u662fSpring\u6d4b\u8bd5\u6846\u67b6\u7684\u6838\u5fc3\u7c7b\uff0c\u5b98\u65b9\u7684\u89e3\u91ca\u662f\uff1aTestContextManager is the main entry point into the Spring TestContext Framework. Specifically, a TestContextManager is responsible for managing a single TestContext.TestContextManager\u7ba1\u7406\u7740TestContext\uff0c\u800cTestContext\u5219\u662f\u5bf9ApplicationContext\u7684\u4e00\u4e2a\u518d\u5c01\u88c5\uff0c\u53ef\u4ee5\u628aTestContext\u7406\u89e3\u4e3a\u589e\u52a0\u4e86\u6d4b\u8bd5\u76f8\u5173\u529f\u80fd\u7684Spring\u5bb9\u5668\u3002 TestContextManager\u540c\u65f6\u4e5f\u7ba1\u7406\u7740TestExecutionListeners\uff0c\u8fd9\u91cc\u4f7f\u7528\u89c2\u5bdf\u8005\u6a21\u5f0f\u63d0\u4f9b\u4e86\u5bf9\u6d4b\u8bd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u5173\u952e\u8282\u70b9\uff08\u5982beforeClass, afterClass\u7b49\uff09\u7684\u76d1\u542c\u80fd\u529b\u3002\u6240\u4ee5\u901a\u8fc7\u7814\u7a76TestContextManager\uff0cTestContext\u548cTestExecutionListeners\u7684\u76f8\u5173\u5b9e\u73b0\u7c7b\u7684\u4ee3\u7801\uff0c\u5c31\u4e0d\u96be\u53d1\u73b0\u6d4b\u8bd5\u65f6Spring\u5bb9\u5668\u7684\u542f\u52a8\u79d8\u5bc6\u4e86\u3002\u5173\u952e\u4ee3\u7801\u5982\u4e0b\uff1apublic class DefaultTestContext implements TestContext {<br \/>  &#8230;<br \/>  public ApplicationContext getApplicationContext() {<\/p>\n<pre><code>ApplicationContext context = this.cacheAwareContextLoaderDelegate.loadContext(this.mergedContextConfiguration);\nif (context instanceof ConfigurableApplicationContext) {\n  @SuppressWarnings(&quot;resource&quot;)\n  ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context;\n  Assert.state(cac.isActive(), () -&gt;\n               &quot;The ApplicationContext loaded for [&quot; + this.mergedContextConfiguration +\n               &quot;] is not active. This may be due to one of the following reasons: &quot; +\n               &quot;1) the context was closed programmatically by user code; &quot; +\n               &quot;2) the context was closed during parallel test execution either &quot; +\n               &quot;according to @DirtiesContext semantics or due to automatic eviction &quot; +\n               &quot;from the ContextCache due to a maximum cache size policy.&quot;);\n}\nreturn context;<\/code><\/pre>\n<p>}<br \/>  &#8230;<br \/>}\u5728DefaultTestContext\u7684getApplicationContext\u65b9\u6cd5\u4e2d\uff0c\u8c03\u7528\u4e86cacheAwareContextLoaderDelegate\u7684loadContext\uff0c\u6700\u7ec8\u8f97\u8f6c\u8c03\u5230Context\u7684refresh\u65b9\u6cd5\uff0c\u4ece\u800c\u6784\u7b51\u8d77Spring\u5bb9\u5668\u4e0a\u4e0b\u6587\u3002\u65f6\u5e8f\u56fe\u5982\u4e0b\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042582651\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u90a3\u4e48getApplicationContext\u65b9\u6cd5\u53c8\u662f\u5728\u54ea\u91cc\u88ab\u8c03\u7528\u7684\u5462\uff1f\u524d\u9762\u4ecb\u7ecd\u8fc7\uff0cTestContextManager\u6269\u5c55\u4e86createTest()\u65b9\u6cd5\uff0c\u4f1a\u989d\u5916\u8c03\u7528\u5176prepareTestInstance\u65b9\u6cd5\u3002public void prepareTestInstance(Object testInstance) throws Exception {<br \/>  if (logger.isTraceEnabled()) {<\/p>\n<pre><code>logger.trace(&quot;prepareTestInstance(): instance [&quot; + testInstance + &quot;]&quot;);<\/code><\/pre>\n<p>}<br \/>  getTestContext().updateState(testInstance, null, null);<br \/>  for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) {<\/p>\n<pre><code>try {\n  testExecutionListener.prepareTestInstance(getTestContext());\n}\ncatch (Throwable ex) {\n  if (logger.isErrorEnabled()) {\n    logger.error(&quot;Caught exception while allowing TestExecutionListener [&quot; + testExecutionListener +\n                 &quot;] to prepare test instance [&quot; + testInstance + &quot;]&quot;, ex);\n  }\n  ReflectionUtils.rethrowException(ex);\n}<\/code><\/pre>\n<p>}<br \/>}prepareTestInstance\u65b9\u6cd5\u4e2d\u4f1a\u8c03\u7528\u6240\u6709TestExecutionListener\u7684prepareTestInstance\u65b9\u6cd5\uff0c\u5176\u4e2d\u6709\u4e00\u4e2a\u53eb\u505aDependencyInjectionTestExecutionListener\u7684\u76d1\u542c\u5668\u4f1a\u8c03\u5230TestContext\u7684getApplicationContext\u65b9\u6cd5\u3002public void prepareTestInstance(TestContext testContext) throws Exception {<br \/>  if (logger.isDebugEnabled()) {<\/p>\n<pre><code>logger.debug(&quot;Performing dependency injection for test context [&quot; + testContext + &quot;].&quot;);<\/code><\/pre>\n<p>}<br \/>  injectDependencies(testContext);<br \/>}<br \/>protected void injectDependencies(TestContext testContext) throws Exception {<br \/>   Object bean = testContext.getTestInstance();<br \/>   Class&lt;?&gt; clazz = testContext.getTestClass();<\/p>\n<p>\/\/ \u8fd9\u91cc\u8c03\u7528TestContext\u7684getApplicationContext\u65b9\u6cd5\uff0c\u6784\u5efaSpring\u5bb9\u5668<br \/>   AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();<\/p>\n<p>beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);<br \/>   beanFactory.initializeBean(bean, clazz.getName() + AutowireCapableBeanFactory.ORIGINAL_INSTANCE_SUFFIX);<br \/>   testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);<br \/>}\u8fd8\u5269\u6700\u540e\u4e00\u4e2a\u95ee\u9898\uff0cDependencyInjectionTestExecutionListener\u662f\u5982\u4f55\u88ab\u6dfb\u52a0\u7684\u5462\uff1f\u7b54\u6848\u662fspring.factories<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042582652\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u81f3\u6b64Spring\u5355\u6d4b\u7684\u542f\u52a8\u8fc7\u7a0b\u5c31\u63a2\u7a76\u660e\u767d\u4e86\uff0c\u63a5\u4e0b\u6765\u770b\u4e0bSpringBoot\u7684\u3002SpringBoot\u5355\u6d4b\u7684\u63a2\u7a76\u4e00\u4e2a\u7b80\u5355\u7684SpringBoot\u5355\u6d4b\u4f8b\u5b50@RunWith(SpringRunner.class)<br \/>@SpringBootTest(classes = Application.class)<br \/>public class MySpringBootTest {<\/p>\n<pre><code>@Autowired\nprivate MyTestBean myTestBean;\n@Test\npublic void test() {\n    myTestBean.test();\n}<\/code><\/pre>\n<p>}<\/p>\n<p>@Target(ElementType.TYPE)<br \/>@Retention(RetentionPolicy.RUNTIME)<br \/>@Documented<br \/>@Inherited<br \/>@BootstrapWith(SpringBootTestContextBootstrapper.class)<br \/>public @interface SpringBootTest {<br \/>  &#8230;<br \/>}\u7c97\u6ee4\u8bf4\u660e\u4e00\u4e0b\uff0c\u8fd9\u91cc\u8fd8\u662f\u901a\u8fc7SpringRunner\u7684run\u65b9\u6cd5\u542f\u52a8\u6d4b\u8bd5\uff0c\u5176\u4e2d\u4f1a\u542f\u52a8Spring\u5bb9\u5668\uff0c\u800c@SpringBootTest\u5219\u63d0\u4f9b\u4e86\u542f\u52a8\u7c7b\uff0c\u540c\u65f6\u901a\u8fc7@BootstrapWith\u63d0\u4f9b\u7684SpringBootTestContextBootstrapper\u7c7b\u4e30\u5bcc\u4e86TestContext\u7684\u80fd\u529b\uff0c\u4f7f\u5f97\u5176\u652f\u6301\u4e86SpringBoot\u7684\u4e00\u4e9b\u7279\u6027\u3002\u8fd9\u91cc\u7740\u91cd\u63a2\u7a76\u4e0b@BootstrapWith\u6ce8\u89e3\u4ee5\u53caSpringBootTestContextBootstrapper\u3002\u524d\u9762\u5728\u4ecb\u7ecdTestContextManager\u65f6\uff0c\u5e76\u6ca1\u6709\u8bb2\u5230\u5176\u6784\u9020\u51fd\u6570\u4ee5\u53caTestContext\u7684\u5b9e\u4f8b\u5316\u8fc7\u7a0b\uff0c\u8fd9\u91cc\u5c06\u5176\u8865\u4e0apublic TestContextManager(Class&lt;?&gt; testClass) {<br \/> this(BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.createBootstrapContext(testClass)));<br \/>}<br \/>public TestContextManager(TestContextBootstrapper testContextBootstrapper) {<br \/> this.testContext = testContextBootstrapper.buildTestContext();<br \/> registerTestExecutionListeners(testContextBootstrapper.getTestExecutionListeners());<br \/>}<br \/>public abstract class AbstractTestContextBootstrapper implements TestContextBootstrapper {  <br \/>  &#8230;<br \/>  public TestContext buildTestContext() {<\/p>\n<pre><code>return new DefaultTestContext(getBootstrapContext().getTestClass(), buildMergedContextConfiguration(),\n    getCacheAwareContextLoaderDelegate());<\/code><\/pre>\n<p>}<br \/>  &#8230;<br \/>}\u6784\u5efaDefaultTestContext\u9700\u8981\u4f203\u4e2a\u53c2\u6570\uff1atestClass\uff0c\u88ab\u6d4b\u8bd5\u7684\u7c7b\u5143\u6570\u636eMergedContextConfiguration\uff0c\u5c01\u88c5\u4e86\u58f0\u660e\u5728\u6d4b\u8bd5\u7c7b\u4e0a\u7684\u4e0e\u6d4b\u8bd5\u5bb9\u5668\u76f8\u5173\u7684\u6ce8\u89e3\uff0c\u5982@ContextConfiguration, @ActiveProfiles, @TestPropertySourceCacheAwareContextLoaderDelegate\uff0c\u7528\u6765loading\u6216closing\u5bb9\u5668\u90a3\u4e48\u5f53\u6211\u4eec\u9700\u8981\u6269\u5c55TestContext\u7684\u529f\u80fd\uff0c\u6216\u8005\u4e0d\u60f3\u7528DefaultTestContext\u65f6\uff0c\u5e94\u8be5\u600e\u4e48\u529e\u5462\uff1f\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u81ea\u7136\u662f\u65b0\u5199\u4e00\u4e2a\u7c7b\u5b9e\u73b0TestContextBootstrapper\u63a5\u53e3\uff0c\u5e76\u8986\u5199buildTestContext()\u65b9\u6cd5\uff0c\u90a3\u4e48\u5982\u4f55\u544a\u8bc9\u6d4b\u8bd5\u6846\u67b6\u8981\u4f7f\u7528\u65b0\u7684\u5b9e\u73b0\u7c7b\u5462\uff1f@BootstrapWith\u5c31\u6d3e\u4e0a\u7528\u573a\u4e86\u3002\u8fd9\u91cc\u6765\u770b\u4e0bBootstrapUtils.resolveTestContextBootstrapper\u7684\u4ee3\u7801static TestContextBootstrapper resolveTestContextBootstrapper(BootstrapContext bootstrapContext) {<br \/>  Class&lt;?&gt; testClass = bootstrapContext.getTestClass();<br \/>  Class&lt;?&gt; clazz = null;<br \/>  try {<\/p>\n<pre><code>clazz = resolveExplicitTestContextBootstrapper(testClass);\nif (clazz == null) {\n  clazz = resolveDefaultTestContextBootstrapper(testClass);\n}\nif (logger.isDebugEnabled()) {\n  logger.debug(String.format(&quot;Instantiating TestContextBootstrapper for test class [%s] from class [%s]&quot;,\n                             testClass.getName(), clazz.getName()));\n}\nTestContextBootstrapper testContextBootstrapper =\n  BeanUtils.instantiateClass(clazz, TestContextBootstrapper.class);\ntestContextBootstrapper.setBootstrapContext(bootstrapContext);\nreturn testContextBootstrapper;<\/code><\/pre>\n<p>}<br \/>  &#8230;<br \/>}<br \/>private static Class&lt;?&gt; resolveExplicitTestContextBootstrapper(Class&lt;?&gt; testClass) {<br \/>  Set&lt;BootstrapWith&gt; annotations = AnnotatedElementUtils.findAllMergedAnnotations(testClass, BootstrapWith.class);<br \/>  if (annotations.isEmpty()) {<\/p>\n<pre><code>return null;<\/code><\/pre>\n<p>}<br \/>  if (annotations.size() == 1) {<\/p>\n<pre><code>return annotations.iterator().next().value();<\/code><\/pre>\n<p>}<br \/>  \/\/ \u83b7\u53d6@BootstrapWith\u6ce8\u89e3\u7684\u503c<br \/>  BootstrapWith bootstrapWith = testClass.getDeclaredAnnotation(BootstrapWith.class);<br \/>  if (bootstrapWith != null) {<\/p>\n<pre><code>return bootstrapWith.value();<\/code><\/pre>\n<p>}<br \/>  throw new IllegalStateException(String.format(<\/p>\n<pre><code>&quot;Configuration error: found multiple declarations of @BootstrapWith for test class [%s]: %s&quot;,\ntestClass.getName(), annotations));<\/code><\/pre>\n<p>}\u8fd9\u91cc\u4f1a\u901a\u8fc7@BootstrapWith\u6ce8\u89e3\u7684\u503c\uff0c\u5b9e\u4f8b\u5316\u5b9a\u5236\u7684TestContextBootstrapper\uff0c\u4ece\u800c\u63d0\u4f9b\u5b9a\u5236\u7684TestContextSpringBootTestContextBootstrapper\u5c31\u662fTestContextBootstrapper\u7684\u5b9e\u73b0\u7c7b\uff0c\u5b83\u901a\u8fc7\u95f4\u63a5\u7ee7\u627fAbstractTestContextBootstrapper\u7c7b\u6269\u5c55\u4e86\u521b\u5efaTestContext\u7684\u80fd\u529b\uff0c\u8fd9\u4e9b\u6269\u5c55\u4e3b\u8981\u5305\u62ec\uff1a\u5c06ContextLoader\u66ff\u6362\u4e3a\u4e86SpringBootContextLoader\u589e\u52a0\u4e86DefaultTestExecutionListenersPostProcessor\u5bf9TestExecutionListener\u8fdb\u884c\u589e\u5f3a\u5904\u7406\u589e\u52a0\u4e86\u5bf9webApplicationType\u7684\u5904\u7406\u63a5\u4e0b\u6765\u770b\u4e0bSpringBootContextLoader\u7684\u76f8\u5173\u4ee3\u7801public class SpringBootContextLoader extends AbstractContextLoader {<br \/>  @Override<br \/>  public ApplicationContext loadContext(MergedContextConfiguration config)<\/p>\n<pre><code>  throws Exception {\nClass&lt;?&gt;[] configClasses = config.getClasses();\nString[] configLocations = config.getLocations();\nAssert.state(\n    !ObjectUtils.isEmpty(configClasses)\n        || !ObjectUtils.isEmpty(configLocations),\n    () -&gt; &quot;No configuration classes &quot;\n        + &quot;or locations found in @SpringApplicationConfiguration. &quot;\n        + &quot;For default configuration detection to work you need &quot;\n        + &quot;Spring 4.0.3 or better (found &quot; + SpringVersion.getVersion()\n        + &quot;).&quot;);\nSpringApplication application = getSpringApplication();\n\/\/ \u8bbe\u7f6emainApplicationClass\napplication.setMainApplicationClass(config.getTestClass());\n\/\/ \u8bbe\u7f6eprimarySources\napplication.addPrimarySources(Arrays.asList(configClasses));\n\/\/ \u6dfb\u52a0configLocations\napplication.getSources().addAll(Arrays.asList(configLocations));\n\/\/ \u83b7\u53d6environment\nConfigurableEnvironment environment = getEnvironment();\nif (!ObjectUtils.isEmpty(config.getActiveProfiles())) {\n  setActiveProfiles(environment, config.getActiveProfiles());\n}\nResourceLoader resourceLoader = (application.getResourceLoader() != null)\n    ? application.getResourceLoader()\n    : new DefaultResourceLoader(getClass().getClassLoader());\nTestPropertySourceUtils.addPropertiesFilesToEnvironment(environment,\n    resourceLoader, config.getPropertySourceLocations());\nTestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment,\n    getInlinedProperties(config));\napplication.setEnvironment(environment);\n\/\/ \u83b7\u53d6\u5e76\u8bbe\u7f6einitializers\nList&lt;ApplicationContextInitializer&lt;?&gt;&gt; initializers = getInitializers(config,\n    application);\nif (config instanceof WebMergedContextConfiguration) {\n  application.setWebApplicationType(WebApplicationType.SERVLET);\n  if (!isEmbeddedWebEnvironment(config)) {\n    new WebConfigurer().configure(config, application, initializers);\n  }\n}\nelse if (config instanceof ReactiveWebMergedContextConfiguration) {\n  application.setWebApplicationType(WebApplicationType.REACTIVE);\n  if (!isEmbeddedWebEnvironment(config)) {\n    new ReactiveWebConfigurer().configure(application);\n  }\n}\nelse {\n  application.setWebApplicationType(WebApplicationType.NONE);\n}\napplication.setInitializers(initializers);\n\/\/ \u8fd0\u884cSpringBoot\u5e94\u7528\nreturn application.run();<\/code><\/pre>\n<p>}<br \/>}\u53ef\u4ee5\u770b\u5230\u8fd9\u91cc\u6784\u5efa\u4e86SpringApplication\uff0c\u8bbe\u7f6e\u4e86mainApplicationClass\uff0c\u8bbe\u7f6e\u4e86primarySources\uff0c\u8bbe\u7f6e\u4e86initializers\uff0c\u6700\u7ec8\u901a\u8fc7application.run()\u542f\u52a8\u4e86SpringBoot\u5e94\u7528\u3002\u81f3\u6b64SpringBoot\u5355\u6d4b\u7684\u542f\u52a8\u8fc7\u7a0b\u4e5f\u63a2\u7a76\u660e\u767d\u4e86\uff0c\u63a5\u4e0b\u6765\u770b\u4e0bMaven\u63d2\u4ef6\u662f\u5982\u4f55\u8fd0\u884c\u5355\u6d4b\u7684\u3002Maven\u63d2\u4ef6\u5982\u4f55\u8fd0\u884c\u5355\u6d4b\u6211\u4eec\u77e5\u9053maven\u662f\u901a\u8fc7\u4e00\u7cfb\u5217\u7684\u63d2\u4ef6\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u9879\u76ee\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7684\u6784\u5efa\u3001\u6d4b\u8bd5\u3001\u6253\u5305\u3001\u90e8\u7f72\u7b49\u52a8\u4f5c\u7684\uff0c\u5f53\u5728Console\u4e2d\u8fd0\u884cmaven clean test\u547d\u4ee4\u65f6\uff0cmaven\u4f1a\u4f9d\u6b21\u8fd0\u884c\u4ee5\u4e0bgoal:maven-clean-plugin:2.5:clean\uff0c\u7528\u4e8e\u6e05\u7406target\u76ee\u5f55maven-resources-plugin:2.6:resources\uff0c\u5c06\u4e3b\u5de5\u7a0b\u76ee\u5f55\u4e0b\u7684\u8d44\u6e90\u6587\u4ef6\u79fb\u52a8\u5230target\u76ee\u5f55\u4e0b\u7684classes\u76ee\u5f55\u4e2dmaven-compiler-plugin:3.1:compile\uff0c\u5c06\u4e3b\u5de5\u7a0b\u76ee\u5f55\u4e0b\u7684java\u6e90\u7801\u7f16\u8bd1\u4e3a\u5b57\u8282\u7801\uff0c\u5e76\u79fb\u52a8\u5230target\u76ee\u5f55\u4e0b\u7684classes\u76ee\u5f55\u4e2dmaven-resources-plugin:2.6:testResources\uff0c\u5c06\u6d4b\u8bd5\u5de5\u7a0b\u76ee\u5f55\u4e0b\u7684\u8d44\u6e90\u6587\u4ef6\u79fb\u52a8\u5230target\u76ee\u5f55\u4e0b\u7684test-classes\u76ee\u5f55\u4e2dmaven-compiler-plugin:3.1:testCompile\uff0c\u5c06\u6d4b\u8bd5\u5de5\u7a0b\u76ee\u5f55\u4e0b\u7684java\u6e90\u7801\u7f16\u8bd1\u4e3a\u5b57\u8282\u7801\uff0c\u5e76\u79fb\u52a8\u5230target\u76ee\u5f55\u4e0b\u7684classes\u76ee\u5f55\u4e2dmaven-surefire-plugin:2.12.4:test\uff0c\u8fd0\u884c\u5355\u6d4b\u6211\u4eec\u6252\u4e0bmaven-surefire-plugin\u63d2\u4ef6\u7684\u4ee3\u7801\u770b\u4e00\u4e0b\u3002\u9996\u5148\u5f15\u5165\u4e0bmaven-surefire-plugin\u548csurefire-junit4\u5305\uff0c\u65b9\u4fbf\u6211\u4eec\u67e5\u770b\u4ee3\u7801\uff1a&lt;dependency&gt;<br \/>  &lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;<br \/>  &lt;artifactId&gt;maven-surefire-plugin&lt;\/artifactId&gt;<br \/>  &lt;version&gt;2.9&lt;\/version&gt;<br \/>&lt;\/dependency&gt;<br \/>&lt;dependency&gt;<br \/>  &lt;groupId&gt;org.apache.maven.surefire&lt;\/groupId&gt;<br \/>  &lt;artifactId&gt;surefire-junit4&lt;\/artifactId&gt;<br \/>  &lt;version&gt;3.0.0-M7&lt;\/version&gt;<br \/>&lt;\/dependency&gt;\u6838\u5fc3\u4ee3\u7801\u5728org.apache.maven.plugin.surefire.AbstractSurefireMojo#execute\u4e2d\uff0c\u8fd9\u91cc\u5c31\u4e0d\u8d34\u4ee3\u7801\u4e86\uff0c\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u81ea\u5df1\u770b\u4e0b\u3002\u603b\u4e4b\u8fd9\u91cc\u4f1a\u7528JUnit4ProviderInfo\u4e2d\u7684\u4fe1\u606f\u901a\u8fc7\u53cd\u5c04\u5b9e\u4f8b\u5316JUnit4Provider\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528\u5176invoke\u65b9\u6cd5\uff0c\u5728\u6539\u65b9\u6cd5\u4e2d\u4f1a\u6700\u7ec8\u5b9e\u4f8b\u5316Runner\u5e76\u8c03\u7528\u5176run\u65b9\u6cd5\u3002\u6838\u5fc3\u4ee3\u7801\u5982\u4e0b\uff1aprivate static void execute( Class&lt;?&gt; testClass, Notifier notifier, Filter filter )<br \/>{<br \/>  final int classModifiers = testClass.getModifiers();<br \/>  if ( !isAbstract( classModifiers ) &#038;&#038; !isInterface( classModifiers ) )<br \/>  {<\/p>\n<pre><code>Request request = aClass( testClass );\nif ( filter != null )\n{\n  request = request.filterWith( filter );\n}\nRunner runner = request.getRunner();\nif ( countTestsInRunner( runner.getDescription() ) != 0 )\n{\n  runner.run( notifier );\n}<\/code><\/pre>\n<p>}<br \/>}\u603b\u7ed3\u81f3\u6b64\u5355\u5143\u6d4b\u8bd5\u8fd0\u884c\u7684\u76f8\u5173\u539f\u7406\u5c31\u63a2\u7a76\u5b8c\u4e86\uff0c\u6211\u4eec\u6765\u56de\u987e\u4e0b\u6709\u54ea\u4e9b\u5185\u5bb9\u5427\u901a\u8fc7IDEA\u76f4\u63a5\u8fd0\u884c\u5355\u6d4b\u65f6\uff0c\u4f1a\u901a\u8fc7JUnitStarter\u7684main\u65b9\u6cd5\u4f5c\u4e3a\u5165\u53e3\uff0c\u6700\u7ec8\u8c03\u7528Junit\u8fd0\u884c\u5355\u5143\u6d4b\u8bd5\u3002Junit4\u5c06@Before\u3001@Test\u3001@After\u8fd9\u4e9b\u6ce8\u89e3\u6253\u6807\u7684\u65b9\u6cd5\u90fd\u62bd\u8c61\u6210\u4e86Statement\uff0c\u6574\u4e2a\u5355\u6d4b\u7684\u8fd0\u884c\u8fc7\u7a0b\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u4e00\u7cfb\u5217Statement\u7684\u8fd0\u884c\u8fc7\u7a0b\u3002\u65b9\u6cd5\u7684\u8c03\u7528\u662f\u901a\u8fc7\u53cd\u5c04\u7684\u65b9\u5f0f\u5b9e\u73b0\u7684\u3002\u501f\u52a9\u4e8e@RunWith(SpringRunner.class)\u6ce8\u89e3\uff0c\u6d4b\u8bd5\u6846\u67b6\u4f1a\u8fd0\u884cSpringRunner\u5b9e\u4f8b\u7684run\u65b9\u6cd5\uff0c\u901a\u8fc7TestContextManager\u521b\u5efaTestContext\uff0c\u5e76\u542f\u52a8Spring\u5bb9\u5668\u3002SpringRunner\u548cSpringJUnit4ClassRunner\u5b9e\u9645\u4e0a\u662f\u7b49\u4ef7\u7684\u3002\u501f\u52a9\u4e8e@SpringBootTest\u548c@BootstrapWith(SpringBootTestContextBootstrapper.class)\u6ce8\u89e3\uff0c\u6d4b\u8bd5\u6846\u67b6\u901a\u8fc7SpringBootTestContextBootstrapper\u589e\u5f3a\u4e86TestContext\uff0c\u8fbe\u5230\u4e86\u542f\u52a8SpringBoot\u5e94\u7528\u7684\u76ee\u7684\u3002Maven\u901a\u8fc7\u8fd0\u884cmaven-surefire-plugin:2.12.4:test\u542f\u52a8\u5355\u5143\u6d4b\u8bd5\uff0c\u5176\u6838\u5fc3\u662f\u901a\u8fc7JUnit4Provider\u8c03\u7528\u4e86JUnit\u6846\u67b6\u7684\u4ee3\u7801\u3002\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=nK8grqKGUpYcS6DP83TDJw%3D%3D.u4SxoFtBBeR7nQACf7sB1qkyU%2BvJSpq10Ju7PHRW%2FOfeNZRiHDA5lvCqtfnERQFg\" rel=\"nofollow\">https:\/\/click.aliyun.com\/m\/10&#8230;<\/a>\u672c\u6587\u4e3a\u963f\u91cc\u4e91\u539f\u521b\u5185\u5bb9\uff0c\u672a\u7ecf\u5141\u8bb8\u4e0d\u5f97\u8f6c\u8f7d\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042582644\">https:\/\/segmentfault.com\/a\/1190000042582644<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb\uff1a\u00a0\u5355\u5143\u6d4b\u8bd5\u662f\u8f6f\u4ef6\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7684\u91cd\u8981\u4e00\u73af\uff0c\u597d\u7684\u5355\u6d4b\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u65e9\u7684\u53d1\u73b0\u95ee\u9898\uff0c\u4e3a\u7cfb\u7edf\u7684\u7a33\u5b9a\u8fd0\u884c\u63d0\u4f9b\u4fdd\u969c\u3002\u5355\u6d4b [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006650","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006650"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006650\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}