{"id":657037,"date":"2025-11-10T05:12:54","date_gmt":"2025-11-10T04:12:54","guid":{"rendered":"https:\/\/blog.jetbrains.com\/?post_type=kotlin&#038;p=657037"},"modified":"2025-11-10T05:13:00","modified_gmt":"2025-11-10T04:13:00","slug":"getting-started-with-kotlin-for-java-developers","status":"publish","type":"kotlin","link":"https:\/\/blog.jetbrains.com\/zh-hans\/kotlin\/2025\/11\/getting-started-with-kotlin-for-java-developers\/","title":{"rendered":"\u9762\u5411 Java \u5f00\u53d1\u8005\u7684 Kotlin \u4f7f\u7528\u5165\u95e8"},"content":{"rendered":"<p><em>\u5ba2\u5ea7\u6587\u7ae0\u4f5c\u8005\uff1a<a href=\"https:\/\/www.linkedin.com\/in\/urs-peter-70a2882\/\" target=\"_blank\" rel=\"noreferrer noopener\" data-type=\"link\" data-id=\"https:\/\/bit.ly\/urs-peter-linked-in\">Urs Peter<\/a>\uff0c\u9ad8\u7ea7\u8f6f\u4ef6\u5de5\u7a0b\u5e08\u517c JetBrains \u8ba4\u8bc1 Kotlin \u57f9\u8bad\u5e08\u3002 \u5bf9\u4e8e\u5e0c\u671b\u4ee5\u66f4\u7cfb\u7edf\u7684\u65b9\u5f0f\u63d0\u5347 Kotlin \u6280\u80fd\u7684\u8bfb\u8005\uff0cUrs \u8fd8\u5728 <\/em><em>Xebia Academy<\/em> \u4e3b\u529e\u4e86 <a href=\"https:\/\/academy.xebia.com\/upskilling\/kotlin-academy\/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Kotlin \u6280\u80fd\u63d0\u5347\u8ba1\u5212<\/em><\/a>\u3002<\/p>\n<p><em>\u672c\u6587\u4e3a<\/em><strong><em>\u5728\u4ee5 Java \u4e3a\u4e3b\u7684\u73af\u5883\u4e2d\u6210\u529f\u91c7\u7528 Kotlin \u7684\u7ec8\u6781\u6307\u5357<\/em><\/strong><em>\u7cfb\u5217\u8bfe\u7a0b\u7684\u5f00\u7bc7\uff0c\u8bfe\u7a0b\u5c06\u4ece\u4e00\u4f4d\u5f00\u53d1\u8005\u4ea7\u751f\u597d\u5947\u5fc3\u5230\u5728\u516c\u53f8\u8303\u56f4\u5185\u5b9e\u73b0\u8f6c\u578b\u8fd9\u4e00\u53d1\u5c55\u8fc7\u7a0b\uff0c\u9010\u6b65\u8bb2\u8ff0 Kotlin \u5982\u4f55\u5728\u771f\u5b9e\u56e2\u961f\u4e2d\u5f97\u5230\u5e94\u7528\u3002<\/em><\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n<p class=\"has-text-align-left\">\u201c<em>\u8f6c\u578b\u91c7\u7528 Kotlin \u5e76\u975e\u4e00\u8e74\u800c\u5c31\u4e4b\u4e8b\u3002<\/em><\/p>\n<p class=\"has-text-align-left\"><em>\u6392\u9664\u98ce\u9669\u3001\u5ba1\u614e\u8bc4\u4f30\u3001\u6536\u83b7\u6210\u6548\uff0c<\/em><\/p>\n<p class=\"has-text-align-left\"><em>\u7136\u540e\u518d\u52a0\u500d\u6295\u5165\u3002<\/em>\u201d<\/p>\n<p>\u5728\u6210\u719f\u7684 Java \u73af\u5883\u4e2d\u91c7\u7528 Kotlin \u4e0d\u4ec5\u4ec5\u662f\u4e00\u9879\u6280\u672f\u51b3\u7b56\uff0c\u800c\u662f\u4e00\u4e2a\u9700\u8981\u7cbe\u5fc3\u8ba1\u5212\u3001\u6218\u7565\u601d\u8003\uff0c\u6700\u91cd\u8981\u7684\u662f\u8d62\u5f97\u540c\u4e8b\u4eec\u652f\u6301\u548c\u7406\u89e3\u7684\u8fc7\u7a0b\u3002\u00a0<\/p>\n<p>\u5728\u57f9\u8bad\u8d85\u8fc7 1000 \u540d\u5f00\u53d1\u8005\u5e76\u5e2e\u52a9\u4f17\u591a\u7ec4\u7ec7\u6210\u529f\u8fc7\u6e21\u5230 Kotlin \u4e4b\u540e\uff0c\u6211\u89c1\u8bc1\u4e86\u54ea\u4e9b\u65b9\u5f0f\u6709\u6548\uff0c\u54ea\u4e9b\u65e0\u6548\u3002 \u672c\u6307\u5357\u5c06\u5e26\u60a8\u4e86\u89e3\u6211\u591a\u5e74\u6765\u6536\u96c6\u7684 Kotlin \u6210\u529f\u91c7\u7528\u6848\u4f8b\uff0c\u4ece\u60a8\u7684\u7b2c\u4e00\u4e2a\u5b9e\u9a8c\u6027\u6f14\u7ec3\u573a\u5230\u5927\u89c4\u6a21\u7684\u7ec4\u7ec7\u8f6c\u578b\u3002<\/p>\n<p>\u672c\u7cfb\u5217\u8bfe\u7a0b\u5c06\u5e26\u60a8\u7ecf\u5386\u4ee5\u4e0b\u65c5\u7a0b\uff1a<\/p>\n<ul>\n<li><a href=\"#it-always-starts-with-a-spark,-initiated-by-you\" data-type=\"link\" data-id=\"https:\/\/blog.jetbrains.com\/#it-always-starts-with-a-spark,-initiated-by-you\">\u8f6c\u578b\u661f\u706b\uff0c\u7531\u60a8\u5f15\u71c3\uff01<\/a><\/li>\n<li><a href=\"#the-play-around-stage-start-small-\u2013-with-tests\">\u8bd5\u7528\u9636\u6bb5\uff1a\u4ece\u5c0f\u89c4\u6a21\u6d4b\u8bd5\u5f00\u59cb\u3002<\/a><\/li>\n<li>\u8bc4\u4f30\u9636\u6bb5\uff1a\u5728\u6f14\u7ec3\u573a\u5ef6\u7eed Kotlin \u5e94\u7528<\/li>\n<li>\u5e7f\u800c\u544a\u4e4b\uff1a\u8d62\u5f97\u5f00\u53d1\u8005\u540c\u50da\u7684\u652f\u6301\u4e0e\u8ba4\u53ef<\/li>\n<li>\u8bf4\u670d\u7ba1\u7406\u5c42\uff1a\u6784\u5efa Kotlin \u7684\u5546\u4e1a\u6848\u4f8b<\/li>\n<li>\u5927\u89c4\u6a21\u91c7\u7528 Kotlin \u7684\u6210\u529f\u56e0\u7d20<\/li>\n<li>\u662f\u5426\u91c7\u7528 Kotlin\uff1a\u8d35\u516c\u53f8\u60f3\u6210\u4e3a\u54ea\u79cd\u7c7b\u578b\u7684\u516c\u53f8\uff1f<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\"><strong>\u8f6c\u578b\u661f\u706b\uff0c\u7531\u60a8\u5f15\u71c3\uff01<\/strong><\/h2>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-647072\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/10\/unnamed-12.png\" alt=\"\u8f6c\u578b\u661f\u706b\uff0c\u7531\u60a8\u5f15\u71c3\uff01\" width=\"1586\" height=\"526\" \/><\/figure>\n<p><em>\u4e3a\u4ec0\u4e48\u8981\u82b1\u7cbe\u529b\u8f6c\u6362\u5230 Kotlin\uff1f \u4e3a\u4f55\u4e0d\u6cbf\u7528 Java \u5462\uff1f\u00a0<\/em><\/p>\n<p>\u7b54\u6848\u53d6\u51b3\u4e8e\u591a\u79cd\u56e0\u7d20\u3002 \u867d\u7136\u6570\u636e\u53ef\u4ee5\u6e05\u695a\u8868\u660e Kotlin \u5728\u591a\u4e2a\u9886\u57df\u90fd\u9887\u5177\u4f18\u52bf\uff0c\u4f46\u51b3\u7b56\u5e76\u975e\u7eaf\u7cb9\u51fa\u4e8e\u6280\u672f\u5c42\u9762\u3002 \u4eba\u4eec\u7684\u4e3b\u89c2\u504f\u597d\uff08\u201c\u6211\u559c\u6b22\u6211\u7684\u8bed\u8a00\uff0c\u65e0\u9700\u7406\u7531\u201d\uff09\u4ee5\u53ca\u5bf9\u65b0\u4e8b\u7269\u7684\u6000\u7591\u6001\u5ea6\uff08\u8fd9\u901a\u5e38\u662f\u4e00\u4ef6\u597d\u4e8b\uff09\u53d1\u6325\u7740\u91cd\u8981\u4f5c\u7528\u3002\u00a0<\/p>\n<p>\u4e0d\u8fc7\uff0c\u7f16\u7a0b\u8bed\u8a00\u7684\u53d1\u5c55\u5386\u7a0b\u8868\u660e\uff0c\u6211\u4eec\u7684\u504f\u597d\u548c\u9700\u6c42\u4f1a\u968f\u7740\u65f6\u95f4\u800c\u6539\u53d8\u3002 \u81f3\u5173\u91cd\u8981\u7684\u662f\uff0c\u6bcf\u4e00\u4ee3\u65b0\u8bed\u8a00\u90fd\u878d\u5165\u4e86\u65b0\u7684\u8303\u5f0f\uff08null \u5b89\u5168\u3001\u7b80\u6d01\u8f7b\u91cf\u5316\u8bed\u6cd5\u3001\u51fd\u6570\u4f5c\u4e3a\u4e00\u7b49\u516c\u6c11\u3001\u4e30\u5bcc\u7684\u6807\u51c6\u5e93\u3001\u5f02\u6b65\u5e76\u53d1\u3001\u591a\u5e73\u53f0\u652f\u6301\u3001\u751f\u6210\u5f0f AI \u53cb\u597d\u7b49\uff09\uff0c\u8fd9\u4e9b\u90fd\u4f1a\u4e3a\u5f00\u53d1\u8005\u548c\u7ec4\u7ec7\u5e26\u6765\u51b3\u5b9a\u6027\u4f18\u52bf\u3002\u00a0<\/p>\n<p>\u5982\u679c\u672a\u7ecf\u5386\u8fd9\u79cd\u81ea\u7136\u53d1\u5c55\uff0c\u6211\u4eec\u73b0\u5728\u53ef\u80fd\u4ecd\u5728\u7528 COBOL \u6216\u5176\u4ed6\u53e4\u8001\u8bed\u8a00\u7f16\u5199\u6240\u6709\u4ee3\u7801\uff0c\u52bf\u5fc5\u65e0\u6cd5\u6ee1\u8db3\u5f53\u4eca\u7684\u9700\u6c42\u3002 \u56e0\u6b64\uff0c\u8fdb\u5316\u5e76\u975e\u53ef\u6709\u53ef\u65e0\uff0c\u800c\u662f\u6211\u4eec\u884c\u4e1a\u7684\u5386\u53f2\u5fc5\u7136\u3002<\/p>\n<p>\u7136\u800c\uff0c\u5982\u679c\u53ea\u662f\u6280\u672f\u4e0a\u7684\u4f18\u52bf\uff0c\u8fd8\u4e0d\u8db3\u4ee5\u8ba9\u8fd9\u79cd\u53d8\u9769\u5728\u516c\u53f8\u5185\u90e8\u624e\u6839\u3002 \u8fd9\u9700\u8981\u63a8\u52a8\u8005 \u2013 \u613f\u610f\u63a2\u7d22\u3001\u5021\u5bfc\u5e76\u5728\u5b9e\u8df5\u4e2d\u5c55\u73b0\u8fd9\u4e9b\u65b0\u8303\u5f0f\u4ef7\u503c\u7684\u4eba\u3002 \u6839\u636e\u6211\u7684\u7ecf\u9a8c\uff0c\u901a\u5e38\u6709\u4e09\u79cd\u7c7b\u578b\u7684\u5de5\u7a0b\u5e08\u4f1a\u6210\u4e3a\u91c7\u7528 Kotlin \u7684\u50ac\u5316\u5242\uff1a<\/p>\n<ol>\n<li><strong>\u52a1\u5b9e\u3001\u6ce8\u91cd\u6548\u7387\u7684 Java \u5de5\u7a0b\u5e08<\/strong>\uff1a\u5c06 Java \u89c6\u4e3a\u5de5\u5177\u800c\u975e\u4fe1\u4ef0\u7684\u7ecf\u9a8c\u4e30\u5bcc\u4e14\u7684\u5f00\u53d1\u8005\u3002 \u4ed6\u4eec\u4e00\u76f4\u5728\u5bfb\u627e\u80fd\u591f\u66f4\u5feb\u5b8c\u6210\u5de5\u4f5c\u7684\u66f4\u597d\u65b9\u5f0f\u3002<\/li>\n<li><strong>\u6ce8\u91cd\u8d28\u91cf\u7684\u73b0\u4ee3\u8bed\u8a00\u7231\u597d\u8005<\/strong>\uff1a\u6ce8\u91cd\u4ee3\u7801\u53ef\u8bfb\u6027\u3001\u7b80\u6d01\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u7684\u5de5\u7a0b\u5e08\u3002 \u4ed6\u4eec\u5f80\u5f80\u4e5f\u662f\u66fe\u7ecf\u4f1a\u5411 Scala \u8f6c\u578b\u7684\u4eba\u3002<\/li>\n<li><strong>\u521d\u7ea7\u5f00\u53d1\u8005<\/strong>\uff1a\u4f1a\u63d0\u51fa\u7b80\u5355\u800c\u76f4\u51fb\u8981\u5bb3\u7684\u95ee\u9898\u7684\u65b0\u624b\uff1a\u201c\u5982\u679c\u53ef\u4ee5\u4f7f\u7528 Kotlin\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u8981\u4f7f\u7528 Java \u5462\uff1f\u201d \u5bf9\u4e8e\u6ca1\u6709\u591a\u5e74 Java \u7ecf\u9a8c\u5305\u88b1\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0cKotlin \u901a\u5e38\u662f\u4e00\u4e2a\u4e0d\u4e8c\u4e4b\u9009\u3002<\/li>\n<\/ol>\n<p>\u60a8\u5c5e\u4e8e\u54ea\u4e2a\u7fa4\u4f53\uff1f\u00a0<\/p>\n<p>\u8fd9\u4e9b\u65e9\u671f\u91c7\u7528\u8005\u70b9\u71c3\u4e86\u8fdb\u5165\u7b2c\u4e00\u9636\u6bb5\u7684\u661f\u706b\u3002 \u4f46\u8be5\u5982\u4f55\u5f00\u59cb\u5462\uff1f \u8bf7\u7ee7\u7eed\u9605\u8bfb\u2026 ;-)\u00a0<\/p>\n<h2 class=\"wp-block-heading\"><strong>\u8bd5\u7528\u9636\u6bb5\uff1a\u4ece\u5c0f\u89c4\u6a21\u6d4b\u8bd5\u5f00\u59cb\u3002<\/strong><\/h2>\n<p>\u60a8\u542c\u8bf4\u8fc7 Kotlin\uff0c\u60f3\u5c1d\u8bd5\u4e00\u4e0b\uff0c\u4f46\u53c8\u4e0d\u60f3\u7acb\u5373\u6295\u5165\u4f7f\u7528\u3002\u00a0<\/p>\n<p>\u90a3\u4e48\uff0c\u60a8\u9996\u5148\u9700\u8981\u7684\u662f\u4e00\u6b3e\u80fd\u591f\u64ad\u4e0b\u7b2c\u4e00\u7c92 Kotlin \u79cd\u5b50\u7684\u5f00\u53d1\u8005\u5de5\u5177\u3002 \u8fd9\u91cc\u6709\u4e00\u4e9b\u9009\u9879\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/play.kotlinlang.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/play.kotlinlang.org\/<\/a> \u662f\u4e00\u4e2a\u5f88\u68d2\u7684\u5728\u7ebf\u6f14\u7ec3\u573a\uff0c\u53ea\u9700\u8f93\u5165\u5e76\u8fd0\u884c Kotlin \u4ee3\u7801\u5373\u53ef\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u5728 JVM \u4e0a\u8fd0\u884c\uff0c\u8fd8\u53ef\u4ee5\u5728\u5404\u79cd\u5176\u4ed6\u5e73\u53f0\uff08JS\u3001WASM \u7b49\uff09\u4e0a\u8fd0\u884c\u3002<img decoding=\"async\" loading=\"lazy\" class=\"wp-image-647084\" style=\"width: 650px;\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/10\/unnamed-13.png\" alt=\"Kotlin Playground\" width=\"1462\" height=\"660\" \/><\/li>\n<\/ul>\n<ul>\n<li><a href=\"https:\/\/kotlinlang.org\/docs\/kotlin-notebook-overview.html\" target=\"_blank\" rel=\"noreferrer noopener\">Kotlin Notebook<\/a> \u662f IntelliJ IDEA \u7684\u4e00\u9879\u5f3a\u5927\u529f\u80fd\uff0c\u53ef\u8ba9\u60a8\u8f7b\u677e\u5bfc\u5165\u4f9d\u8d56\u9879\u3001\u6267\u884c\u4ee3\u7801\uff0c\u751a\u81f3\u5904\u7406\u6570\u636e\u3001\u7ed8\u5236\u56fe\u8868\u7b49\u3002 \u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u4f7f\u7528 Spring \u7684 RestClient \u8fdb\u884c REST \u8c03\u7528\u6709\u591a\u8f7b\u677e\uff1a<img decoding=\"async\" loading=\"lazy\" class=\"wp-image-647095\" style=\"width: 650px;\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/10\/unnamed-11.png\" alt=\"Kotlin Notebook\" width=\"1600\" height=\"1035\" \/><\/li>\n<\/ul>\n<ul>\n<li>IntelliJ IDEA \u63d0\u4f9b\u4e86\u5bf9 Kotlin \u7684\u4e00\u6d41\u652f\u6301\u3002 \u8fd9\u4e0d\u8db3\u4e3a\u5947\uff0c\u56e0\u4e3a JetBrains \u662f Kotlin \u7684\u521b\u9020\u8005\uff0c\u800c IntelliJ IDEA \u7684\u5927\u90e8\u5206\u4ee3\u7801\u90fd\u662f\u7528 Kotlin \u7f16\u5199\u7684\u3002 \u90a3\u5c31\u5f00\u59cb\u5728 <a href=\"https:\/\/www.jetbrains.com\/help\/idea\/get-started-with-kotlin.html#add-kotlin-dependency\" target=\"_blank\" rel=\"noreferrer noopener\">IntelliJ IDEA<\/a> \u4e2d\u4f7f\u7528 Kotlin \u5427 \u2013 \u5373\u4f7f\u5728\u60a8\u73b0\u6709\u7684 Java \u9879\u76ee\u4e2d\uff0c\u4e5f\u8f7b\u800c\u6613\u4e3e\uff1a\n<ul>\n<li>\u5bf9\u4e8e <a href=\"https:\/\/kotlinlang.org\/docs\/maven.html\" target=\"_blank\" rel=\"noreferrer noopener\">Maven<\/a>\uff0c\u53ea\u9700\u914d\u7f6e <code>kotlin-maven-plugin<\/code> \u548c Kotlin \u6807\u51c6\u5e93 <code>kotlin-stdlib<\/code> \u5373\u53ef\u3002<\/li>\n<li>\u5bf9\u4e8e <a href=\"https:\/\/kotlinlang.org\/docs\/gradle-configure-project.html\" target=\"_blank\" rel=\"noreferrer noopener\">Gradle<\/a>\uff0c\u60a8\u9700\u8981\u914d\u7f6e Kotlin \u63d2\u4ef6\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u2026\u53ef\u4ee5\u5f00\u59cb\u4e86\uff01<\/p>\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-647106\" style=\"aspect-ratio: 1.1216216216216217; width: 320px; height: auto;\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/10\/unnamed-14.png\" alt=\"\" width=\"498\" height=\"444\" \/><\/figure>\n<ul>\n<li>\u8fd8\u6709\u66f4\u591a\uff01 JetBrains \u6700\u8fd1\u53d1\u5e03\u4e86 Kotlin \u8bed\u8a00\u670d\u52a1\u5668\uff0c\u4e3a IntelliJ IDEA \u4ee5\u5916\u7684\u5176\u4ed6 IDE\uff08\u4f8b\u5982 VS Code\uff09\u5e26\u6765\u529f\u80fd\u9f50\u5168\u7684 Kotlin \u5f00\u53d1\u4f53\u9a8c\u3002 \u8bf7\u53c2\u9605\uff1a<a href=\"https:\/\/github.com\/Kotlin\/kotlin-lsp\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/Kotlin\/kotlin-lsp<\/a>\u00a0<\/li>\n<\/ul>\n<p>\u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u5728\u60a8\u559c\u6b22\u7684\u5f00\u53d1\u73af\u5883\u4e2d\u7f16\u5199 Kotlin \u4ee3\u7801\u3002 \u5982\u4f55\u5728\u771f\u5b9e\u73af\u5883\u4e2d\u4ee5\u6700\u5c0f\u7684\u5f71\u54cd\u548c\u6700\u9ad8\u7684\u6d1e\u5bdf\u529b\u8bc4\u4f30\u8bed\u8a00\uff1f \u5728\u73b0\u6709 Java \u9879\u76ee\u7684\u6d4b\u8bd5\u5957\u4ef6\u4e2d\uff01\u00a0<\/p>\n<p>\u8fd9\u79cd\u5b89\u5168\u4e14\u5b9e\u9645\u7684 Kotlin \u5b9e\u9a8c\u65b9\u5f0f\u5177\u6709\u4ee5\u4e0b\u51e0\u9879\u4f18\u52bf\uff1a<\/p>\n<ul>\n<li><strong>\u4f4e\u98ce\u9669<\/strong>\uff1a\u6d4b\u8bd5\u4e0d\u4f1a\u5f71\u54cd\u751f\u4ea7\u4ee3\u7801\u3002<\/li>\n<li><strong>\u5b66\u4e60\u673a\u4f1a<\/strong>\uff1a\u60a8\u53ef\u4ee5\u5728\u719f\u6089\u7684\u73af\u5883\u4e2d\u63a2\u7d22\u8bed\u8a00\u529f\u80fd\u3002<\/li>\n<li><strong>\u5faa\u5e8f\u6e10\u8fdb\u5730\u5f15\u5165<\/strong>\uff1a\u56e2\u961f\u6210\u5458\u53ef\u4ee5\u5728\u6ca1\u6709\u538b\u529b\u7684\u60c5\u51b5\u4e0b\u719f\u6089 Kotlin \u8bed\u6cd5\u3002<\/li>\n<\/ul>\n<h3 class=\"wp-block-heading\"><strong>\u63d0\u793a<\/strong><\/h3>\n<ol>\n<li>\u5c1d\u8bd5 Kotest + MockK\uff1a\u7acb\u5373\u611f\u53d7 Kotlin \u6d4b\u8bd5 DSL\uff08\u9886\u57df\u7279\u5b9a\u8bed\u8a00\uff09\u7684\u8868\u8fbe\u80fd\u529b\uff0c\u4f8b\u5982\u529f\u80fd\u4e30\u5bcc\u7684\u65ad\u8a00 (<code>shouldHaveSize(...)<\/code>\u3001\u4e2d\u7f00 (<code>value shouldBe 1<\/code>) \u7b49\u3002<\/li>\n<li>\u4f7f\u7528\u7b80\u6d01\u800c\u5f3a\u5927\u7684 Kotlin \u96c6\u5408\uff0c\u800c\u975e Java \u6d41\u3002<\/li>\n<li>\u4f53\u9a8c\u5404\u79cd\u8bed\u8a00\u529f\u80fd\uff0c\u4f8b\u5982\u53ef\u4e3a null \u7684\u7c7b\u578b\u3001\u6790\u6784\u3001\u4e0d\u53ef\u53d8\u6027\uff08<code>val<\/code>\u3001<code>data classes<\/code>\uff09\u3001\u8868\u8fbe\u5f0f\u6784\u9020\uff08<code>when<\/code>\u3001<code>try-catch<\/code>\u3001<code>if else<\/code>\uff09\u7b49\u3002<\/li>\n<\/ol>\n<p>\u60a8\u5c06\u5f97\u5230\uff1a<\/p>\n<p><strong>Java<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\nvoid shouldGetAverageRating() {\n   when(productRepository.findAll()).thenReturn(products);\n\n   Map ratings = productService.averageRatings();\n\n   assertAll(\n           () -&gt; assertThat(ratings).hasSize(4),\n           () -&gt; assertEquals(ratings, products\n                   .stream()\n                   .collect(Collectors.groupingBy(\n                           Product::getName,\n                           Collectors.flatMapping(\n                                   p -&gt; p.getRatings()\n                                    .stream()\n                                    .mapToDouble(Integer::doubleValue)\n                                    .boxed(),   \n                              Collectors.averagingDouble(Double::doubleValue)\n                           )))\n           )\n   );\n   verify(productRepository).findAll();\n\n}\n<\/pre>\n<p><strong>Kotlin<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\nfun `should get average rating`() { \/\/descriptive tests using ``\n   every { productRepository.findAll() } returns products\n\n   val ratings = productService.averageRatings()\n\n   assertSoftly(ratings) { \t\t\/\/powerful testing DSLs (Kotest)\n       shouldHaveSize(4)\n       this shouldBe productRepository.findAll()\n           .groupBy { it.name }   \t\t\t\/\/concise collections\n           .mapValues { (_, products) -&gt; \t\t\/\/destructuring\n                  products.flatMap { it.ratings }.average() }\n   }\n    verify { productRepository.findAll() }\n}<\/pre>\n<h3 class=\"wp-block-heading\">\u7cfb\u5217\u8bfe\u7a0b\u540e\u7eed\u5185\u5bb9<\/h3>\n<p>Kotlin \u7684\u91c7\u7528\u901a\u5e38\u59cb\u4e8e\u4e2a\u4eba\u63a2\u7d22\u548c\u4e00\u4e9b\u7528\u4e8e\u9a8c\u8bc1\u5b9e\u7528\u6027\u7684\u6d4b\u8bd5\u3002 \u8fd9\u4e9b\u521d\u6b65\u63a2\u7d22\u5c06\u81ea\u7136\u800c\u7136\u5730\u5f15\u5411\u66f4\u8fdc\u5927\u7684\u76ee\u6807\uff1a\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u8bc4\u4f30 Kotlin\u3002<\/p>\n<p>\u672c\u7cfb\u5217\u8bfe\u7a0b\u7684\u4e0b\u4e00\u7bc7\u6587\u7ae0\u5c06\u4ecb\u7ecd\u8be5\u9636\u6bb5\uff0c\u5e76\u8bf4\u660e\u5982\u4f55\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u6d4b\u8bd5 Kotlin\u3002<\/p>\n<div class=\"about-author \">\n<div class=\"about-author__box\">\n<div class=\"row\">\n<div class=\"about-author__box-img\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/10\/a3f7-400o400o1-QYUavRYyARnAVJLCpM1m8M.webp\" alt=\"\" \/><\/div>\n<div class=\"about-author__box-text\">\n<h4>Urs Peter<\/h4>\n<p>Urs \u662f\u4e00\u4f4d\u7ecf\u9a8c\u4e30\u5bcc\u7684\u8f6f\u4ef6\u5de5\u7a0b\u5e08\u3001\u89e3\u51b3\u65b9\u6848\u67b6\u6784\u5e08\u3001\u4f1a\u8bae\u6f14\u8bb2\u5609\u5bbe\u548c\u57f9\u8bad\u5e08\uff0c\u5728\u6784\u5efa\u5f39\u6027\u3001\u53ef\u6269\u7f29\u548c\u4efb\u52a1\u5173\u952e\u578b\u7cfb\u7edf\u65b9\u9762\u62e5\u6709 20 \u4f59\u5e74\u7684\u4e30\u5bcc\u7ecf\u9a8c\uff0c\u4e3b\u8981\u6d89\u53ca Kotlin \u548c Scala\u3002<\/p>\n<p>\u9664\u4e86\u62c5\u4efb\u987e\u95ee\u4e4b\u5916\uff0c\u4ed6\u8fd8\u662f\u4e00\u4f4d\u5145\u6ee1\u70ed\u60c5\u7684\u57f9\u8bad\u5e08\uff0c\u5e76\u64b0\u5199\u4e86\u4e30\u5bcc\u591a\u6837\u7684\u8bfe\u7a0b\uff0c\u6d89\u53ca\u4ece Kotlin \u548c Scala \u8bed\u8a00\u8bfe\u7a0b\u5230\u5fae\u670d\u52a1\u548c\u4e8b\u4ef6\u9a71\u52a8\u578b\u67b6\u6784\u7b49\u67b6\u6784\u57f9\u8bad\u7684\u4f17\u591a\u5185\u5bb9\u3002<\/p>\n<p>\u4f5c\u4e3a\u5929\u751f\u793e\u4ea4\u8fbe\u4eba\uff0c\u4ed6\u559c\u6b22\u5728\u805a\u4f1a\u548c\u4f1a\u8bae\u4e0a\u5206\u4eab\u77e5\u8bc6\uff0c\u542f\u53d1\u540c\u884c\u5e76\u83b7\u5f97\u540c\u884c\u542f\u53d1\u3002 Urs \u662f\u4e00\u4f4d JetBrains \u8ba4\u8bc1 Kotlin \u57f9\u8bad\u5e08\u3002<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<p><\/p>\n","protected":false},"author":1297,"featured_media":652320,"comment_status":"closed","ping_status":"closed","template":"","categories":[],"tags":[],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin\/657037"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/types\/kotlin"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/users\/1297"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/comments?post=657037"}],"version-history":[{"count":1,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin\/657037\/revisions"}],"predecessor-version":[{"id":657572,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin\/657037\/revisions\/657572"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media\/652320"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media?parent=657037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/categories?post=657037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/tags?post=657037"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/cross-post-tag?post=657037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}