{"id":5205,"date":"2024-12-10T21:11:30","date_gmt":"2024-12-10T13:11:30","guid":{"rendered":"http:\/\/xinblog.ltd\/?p=5205"},"modified":"2024-12-16T21:14:17","modified_gmt":"2024-12-16T13:14:17","slug":"%e5%a6%82%e4%bd%95%e5%86%99%e6%b5%8b%e8%af%95-%e5%85%ab","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=5205","title":{"rendered":"\u5982\u4f55\u5199\u6d4b\u8bd5 \u516b"},"content":{"rendered":"<p>\u5982\u4f55\u5199\u6d4b\u8bd5 \u516b<\/p>\n<p>\u5bf9\u4e8e\u4e00\u822c\u7684\u5f00\u53d1\u4eba\u5458\u6765\u8bf4\uff0cSpring\u662f\u4e00\u4e2a\u89e3\u51b3\u4e86\u5f88\u591a\u95ee\u9898\uff0c\u975e\u5e38\u4f18\u79c0\u7684\u6846\u67b6\uff0c\u5176\u89e3\u51b3\u4e86Java\u670d\u52a1\u5668\u7aef\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u8fdb\u884c\u7684\u6253\u5305\u5e76\u90e8\u7f72\u5230\u5bb9\u5668\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u800c\u5728Spring\u8fd9\u4e2a\u6846\u67b6\u4e4b\u4e2d\uff0c\u4e00\u65b9\u9762\u63d0\u4f9b\u4e86\u5f00\u53d1\u4e0a\u7684\u7b80\u6613\uff0c\u53e6\u4e00\u65b9\u9762\u4e5f\u63d0\u4f9b\u4e86\u6d4b\u8bd5\u4e0a\u7684\u65b9\u4fbf\uff0c\u65e0\u8bba\u662f\u5355\u5143\u6d4b\u8bd5\u548c\u96c6\u6210\u6d4b\u8bd5\u90fd\u53ef\u4ee5\u5b8c\u7f8e\u652f\u6301\u3002<\/p>\n<p>\u66f4\u52a0\u5177\u4f53\u7684\uff0c\u5219\u53ef\u4ee5\u4f53\u73b0\u5728Spring\u63d0\u4f9b\u7684\u6838\u5fc3\u529f\u80fd\uff0c\u4e5f\u5c31\u662f\u4f9d\u8d56\u6ce8\u5165\uff0c\u5728\u5b9e\u9645\u5f00\u53d1\u4e4b\u4e2d\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u4f9d\u8d56\u6ce8\u5165\u6765\u5b8c\u6210\u76f8\u5173\u7ec4\u4ef6\u7684\u7ec4\u88c5\u3002\u5bf9\u4e8eSpring\u7684\u6d4b\u8bd5\uff0c\u4e5f\u662f\u57fa\u4e8e\u4e86\u4f9d\u8d56\u6ce8\u5165\uff0c\u53ea\u8981\u662f\u4f9d\u8d56\u6ce8\u5165\u7684\u7ec4\u4ef6\uff0cSpring\u90fd\u53ef\u4ee5\u5f88\u597d\u7684\u63d0\u4f9b\u6d4b\u8bd5\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u7ed9\u51fa\u4e24\u4e2a\u53cd\u4f8b<\/p>\n<p>\u6bd4\u5982\u4f7f\u7528\u4e0d\u57fa\u4e8e\u5b57\u6bb5\u7684\u6ce8\u5165<\/p>\n<table>\n<tbody>\n<tr>\n<td>@Service<\/p>\n<p>public class TodoItemService {<\/p>\n<p>@Autowired<\/p>\n<p>private TodoItemRepository repository;<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u91cc\u6211\u4eec\u901a\u8fc7\u5b57\u6bb5\u7684\u65b9\u5f0f\u8fdb\u884c\u4e86\u6ce8\u5165\uff0c\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u8ba9\u8fd9\u4e2aService\u5728\u9879\u76ee\u542f\u52a8\u7684\u65f6\u5019\u81ea\u52a8\u521d\u59cb\u5316\u5e76\u5c06\u4ed3\u5e93\u7c7b\u8fdb\u884c\u6ce8\u5165\u3002<\/p>\n<p>\u5176\u652f\u6301\u548cSpring\u7684\u96c6\u6210\u6d4b\u8bd5\uff0c\u4f46\u662f\u5bf9\u4e8e\u5355\u5143\u6d4b\u8bd5\u5374\u4e0d\u662f\u5f88\u53cb\u597d\u3002\u8fd9\u91cc\u6211\u4eec\u5982\u679c\u662f\u6539\u4e3a\u5c06Autowired\u6ce8\u89e3\u653e\u5728\u6784\u9020\u51fd\u6570\u4e0a\uff0c\u8ba9Spring\u53bb\u521d\u59cb\u5316<\/p>\n<table>\n<tbody>\n<tr>\n<td>@Service<\/p>\n<p>public class TodoItemService {<\/p>\n<p>private final TodoItemRepository repository;<\/p>\n<p>@Autowired<\/p>\n<p>public TodoItemService(final TodoItemRepository repository) {<\/p>\n<p>this.repository = repository;<\/p>\n<p>}<\/p>\n<p>&#8230;<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u91cc\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u5355\u5143\u6d4b\u8bd5\u7684\u65f6\u5019\u50cf\u4e00\u4e2a\u666e\u901a\u5bf9\u8c61\u8fdb\u884c\u6d4b\u8bd5\u4e86\u3002<\/p>\n<p>\u5176\u6b21\u662f\u4e0d\u8981\u4f9d\u8d56ApplicationContext<\/p>\n<p>\u5728\u67d0\u4e9b\u5de5\u7a0b\u4e4b\u4e2d\uff0c\u6709\u4e9b\u4ee3\u7801\u4f1a\u5c1d\u8bd5\u5229\u7528ApplicationContext\u4e2d\u7684\u9759\u6001\u65b9\u6cd5getBean\u6765\u83b7\u53d6\u5230Spring\u5bb9\u5668\u6c60\u4e2d\u7684Bean\u5bf9\u8c61\u3002<\/p>\n<p>\u4f46\u662f\u8fd9\u79cd\u4ee3\u7801\u7684\u4f7f\u7528\uff0c\u662f\u4e00\u79cd\u9519\u8bef\u7684\u505a\u6cd5\uff0c\u5176\u6253\u7834\u4e86DI\u5bb9\u5668\u7684\u539f\u672c\u8bbe\u8ba1\uff0c\u53e6\u4e00\u65b9\u9762\u8fd8\u8ba9\u6838\u5fc3\u4ee3\u7801\u5bf9\u7b2c\u4e09\u65b9\u4ee3\u7801\u8fdb\u884c\u4e86\u4f9d\u8d56\u3002<\/p>\n<p>\u6240\u4ee5\u603b\u7ed3\u4e00\u4e0b\uff0c\u5bf9\u4e8eSpring\u7684\u4f7f\u7528\u5e94\u8be5\u5728\u4ee3\u7801\u5c42\u9762\u4e0d\u4f9d\u8d56\u4e8eSpring\uff0c\u800c\u4e0d\u662f\u6df1\u6df1\u7684\u4f9d\u8d56Spring\u3002<\/p>\n<p>\u800cSpring\u5bf9\u4e8e\u6d4b\u8bd5\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u4e13\u5c5e\u7684\u6ce8\u89e3<\/p>\n<p>\u6bd4\u5982@MockBean\uff0c\u5176\u5c31\u7c7b\u4f3c<\/p>\n<table>\n<tbody>\n<tr>\n<td>@BeforeEach<\/p>\n<p>public void setUp() {<\/p>\n<p>this.repository = mock(TodoItemRepository.class);<\/p>\n<p>&#8230;<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4f46\u662f\u5176\u5728\u96c6\u6210\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u4f1a\u63d0\u4f9b\u4f9d\u8d56\u6ce8\u5165\u7684\u80fd\u529b\u3002<\/p>\n<p>\u5bf9\u4e8eSpring\u63d0\u4f9b\u7684\u96c6\u6210\u6d4b\u8bd5\uff0c\u6211\u4eec\u5219\u53ef\u4ee5\u4ece\u4e24\u4e2a\u89d2\u5ea6\u6765\u770b\uff0c\u5c31\u662f\u4e4b\u524d\u8bf4\u8fc7\u7684\u6570\u636e\u5e93\u548cWeb\u89d2\u5ea6\u3002<\/p>\n<p>\u5bf9\u4e8e\u6570\u636e\u5e93\u7684\u89d2\u5ea6\uff0c\u6211\u4eec\u4e4b\u524d\u66fe\u7ecf\u7ed9\u8fc7\u4e00\u4e2a\u4f8b\u5b50<\/p>\n<p>\u9996\u5148\u662f\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528@TestPropertySource\u6765\u7ed9\u51fa\u4e00\u4e2a\u4e0d\u540c\u7684\u914d\u7f6e\uff0c\u5728\u91cc\u9762\u58f0\u660e\u4e0d\u540c\u914d\u7f6e\u3002\u4ece\u800c\u8ba9\u6211\u4eec\u65e0\u7f1d\u8fde\u63a5\u5230\u4e0d\u540c\u7684\u6570\u636e\u5e93\u3002<\/p>\n<p>\u518d\u4e4b\u540e\u6211\u4eec\u53ef\u4ee5\u51c6\u5907\u4e00\u4e2a\u548c\u672a\u6765\u7ebf\u4e0a\u6570\u636e\u5e93\u4e00\u81f4\u7684\u6d4b\u8bd5\u6570\u636e\u5e93\u3002\u8fd9\u91cc\u5e76\u4e0d\u63a8\u8350\u4f7f\u7528\u5d4c\u5165\u5f0f\u7684\u5185\u5b58\u6570\u636e\u5e93\uff0c\u8fd9\u662f\u56e0\u4e3a\u5982\u679c\u4f7f\u7528\u4e0d\u540c\u7684\u6570\u636e\u5e93\u7c7b\u578b\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\u51fa\u73b0sql\u6216\u8005\u51fd\u6570\u65e0\u6cd5\u5e94\u7528\u7684\u95ee\u9898\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u8fd8\u662f\u8003\u8651\u4f7f\u7528\u4e00\u4e2a\u76f8\u540c\u7248\u672c\u7684\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u8003\u8651\u51c6\u5907\u4e00\u4e2a\u914d\u7f6e\u66f4\u4f4e\u7684\u6570\u636e\u5e93\u3002<\/p>\n<p>\u7136\u540e\u4e4b\u540e\u5229\u7528@DataJpaTest\uff0c\u914d\u7f6e\u6570\u636e\u5e93\u56de\u6eda\u80fd\u529b\u3002<\/p>\n<p>\u5f53\u7136\u5982\u679c\u4e0d\u662f\u4f7f\u7528\u7684JPA\uff0c\u800c\u662f\u5176\u4ed6\u7684Mybatis\u7b49\u3002\u5219\u4e5f\u6709\u5bf9\u5e94\u7684\u6ce8\u89e3\u3002<\/p>\n<p>\u6bd4\u5982Spring\u63d0\u4f9b\u4e86@JdbcTest\u3002\u5219\u5bf9\u5e94\u7684\u662f\u4f7f\u7528DataSource\u3002<\/p>\n<p>\u518d\u4e4b\u540e\u662fWeb\u7684\u6d4b\u8bd5\uff0c\u5bf9\u4e8eSpring\u4e5f\u652f\u6301\u7684\u5f88\u597d<\/p>\n<p>\u6211\u4eec\u4e4b\u524d\u5229\u7528@SpringBootTest\u6765\u5c06\u6846\u67b6\u96c6\u6210\u5230\u4e86\u6d4b\u8bd5\u4e4b\u4e2d\uff0c\u505a\u4e86\u96c6\u6210\u6d4b\u8bd5<\/p>\n<p>\u5176\u4e5f\u6709\u4e00\u4e2a\u7b80\u5316\u7248\u672c@WebMvcTest<\/p>\n<table>\n<tbody>\n<tr>\n<td>@WebMvcTest(TodoItemResource.class)<\/p>\n<p>public class TodoItemResourceTest {<\/p>\n<p>&#8230;<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u6837\u5c31\u5c31\u53ea\u96c6\u6210TodoItemResource\u76f8\u5173\u7684\u90e8\u5206\u4e86\u3002<\/p>\n<p>\u9664\u6b64\u5916\uff0cSpring\u8fd8\u63d0\u4f9b\u4e86\u6a21\u62df\u7684Web\u8bbf\u95ee<\/p>\n<p>\u5c31\u662f\u5728SpringBootTest\u6ce8\u89e3\u6807\u8bb0\u540c\u65f6\u58f0\u660e@AutoConfigureMockMvc\uff0c\u5e76\u5728\u7c7b\u4e2d\u6ce8\u5165MockMvc\u5bf9\u8c61\u5373\u53ef\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td>@SpringBootTest<\/p>\n<p>@AutoConfigureMockMvc<\/p>\n<p>@Transactional<\/p>\n<p>public class TodoItemResourceTest {<\/p>\n<p>@Autowired<\/p>\n<p>private MockMvc mockMvc;<\/p>\n<p>&#8230;<\/p>\n<p>@Test<\/p>\n<p>public void should_add_item() throws Exception {<\/p>\n<p>String todoItem = &#8220;{ &#8221; +<\/p>\n<p>&#8220;\\&#8221;content\\&#8221;: \\&#8221;foo\\&#8221;&#8221; +<\/p>\n<p>&#8220;}&#8221;;<\/p>\n<p>mockMvc.perform(MockMvcRequestBuilders.post(&#8220;\/todo-items&#8221;)<\/p>\n<p>.contentType(MediaType.APPLICATION_JSON)<\/p>\n<p>.content(todoItem))<\/p>\n<p>.andExpect(status().isCreated());<\/p>\n<p>assertThat(repository.findAll()).anyMatch(item -&gt; item.getContent().equals(&#8220;foo&#8221;));<\/p>\n<p>}<\/p>\n<p>}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u91cc\u7684MockMvc\u5e76\u4e0d\u662f\u771f\u7684\u53bb\u8fdb\u884c\u4e86\u4e00\u6b21\u7f51\u7edc\u8bf7\u6c42\uff0c\u800c\u652f\u6301\u6a21\u62df\u8bf7\u6c42\u53bb\u5b9e\u9645\u8c03\u7528\u4e86\u4ee3\u7801\uff0c\u53ea\u4e0d\u8fc7\u8c03\u7528\u94fe\u8def\u4e0a\u7684\u5404\u79cdFilter\uff0c\u62e6\u622a\u5668\u90fd\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u770b\u4e86\u5982\u4f55\u5728Spring\u9879\u76ee\u4e4b\u4e2d\u8fdb\u884c\u96c6\u6210\u6d4b\u8bd5\u3002\u4e3b\u8981\u662f\u5982\u4f55\u505a\u6570\u636e\u5e93\u548cWeb\u63a5\u53e3\u7684\u96c6\u6210\u6d4b\u8bd5\u3002<\/p>\n<p>\u6570\u636e\u5e93\u6211\u4eec\u8bf4\u4e86\u5982\u4f55\u96c6\u6210\u4e0d\u540c\u7684\u914d\u7f6e\uff0c\u4ee5\u53ca\u914d\u7f6e\u6570\u636e\u56de\u6eda<\/p>\n<p>Web\u6d4b\u8bd5\u5219\u662f\u6a21\u62df\u7f51\u7edc\u8bf7\u6c42\u3002\u8fdb\u884c\u76f8\u5173\u7684\u6d4b\u8bd5\u3002<\/p>\n<p>Spring\u7684\u6d4b\u8bd5\u76f8\u5173\u7ec4\u4ef6\u8fd8\u6709\u5f88\u591a\uff0c\u8fd8\u53ef\u4ee5\u53bb\u67e5\u770b\u5176\u5b98\u65b9\u6587\u6863\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5199\u6d4b\u8bd5 \u516b \u5bf9\u4e8e\u4e00\u822c\u7684\u5f00\u53d1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5205"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5205"}],"version-history":[{"count":1,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5205\/revisions"}],"predecessor-version":[{"id":5206,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5205\/revisions\/5206"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5205"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}