第1バネ輸入依存するすべてのパッケージ
2テスト・インタフェースを確立
パブリック インターフェイスCompactDisc { 無効遊び(); }
インタフェースクラス3の特定の実施
インポートorg.springframework.stereotype.Component; インポートmorningstar.spring.CompactDisc; / * *クラスのノートと@Componentクラスはコンポーネントクラスとして使用されることを示しており、このクラスのためのSpring Beanを作成するように言わ* / @Component パブリック クラス Stgpepers 実装コンパクトディスク{ プライベート文字列のタイトル=「sgt.peper APOS lnnelyハーツ・クラブ・バンド」; プライベート文字列=アーティスト「ビートルズ」; @Override 公共 ボイドプレイ(){ // TODO自動生成されたスタブメソッド のSystem.out.printlnを( "で"+ +タイトルを"再生「+ アーティスト); } }
4コンフィギュレーション・クラスを作成します。
輸入org.springframework.context.annotation.ComponentScan; 輸入org.springframework.context.annotation.Configuration。 輸入morningstar.spring.entity.Stgpepers。 @Configuration @ComponentScan(basePackagesが = { "morningstar.spring.entity" }) パブリック クラスCDPlayerConfig { / * * @構成: * @ ComponentScan:在ばね中启用组件扫描* / }
5テストを作成します。
// SpringJUnit4ClassRunner:試験はSpringアプリケーション・コンテキストの作成時に始まる (SpringJUnit4ClassRunnerの@RunWith クラス) // @ContextConfigurationアノテーションはCDPlayerConfigクラスにバネ負荷設定クラスを指示 @ContextConfiguration(= CDPlayerConfigクラスクラス) パブリック クラスCDPlayerTest { @Autowired プライベートコンパクトディスクディスク; @rule 公共 決勝 StandardErrorStreamLogログ= 新しい新StandardErrorStreamLog(); @Autowired プライベートMediaPlayerのプレーヤー、 @Test 公共 無効cdShouldNotBeNull() { assertNotNull(ディスク) System.out.println( "成功" ); } @Test 公共 ボイドプレーヤー() { player.play()。 } }
6つの結果:
によってテスト