私は統合テストスイートのプロジェクトに着きます。これらのテストのうちの1つは、第三者のベンダーに多くのネットワーク呼び出しを行ったクラスを自作したものです。テストでこれらの呼び出しを行うことを望んでいなかったので、チームはこのクライアントをtestApplicationContext.xml Spring構成ファイルを使用して試してみました。SpringでMockクラスを設定できません
統合テストクラス:testApplication.xmlの
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/testApplicationContext.xml")
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
public class IntegrationTest {
定義:モックbeans.xmlのの
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.mycompany" />
<context:component-scan base-package="tst.mycompany.mocks"/>
<import resource="mock-beans.xml"/>
定義:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"...>
<!-- Override bean definitions with mocks -->
<bean id="thirdPartyClientService" class="tst.mycompany.mocks.MockThirdPartyClient" />
</beans>
これは私たちの設定でうまく動作し、MockThirdPartyClientのインスタンスは、テストとして実行されたときに私たちのSpring Beanにautowiredされます。
しかし、同じパッケージ内に別のサービス用の新しいモックを最近追加しましたが、ロードできません。私はエラーを取得する:
Cannot find class [tst.mycompany.mocks.MockAdressService] for bean with name 'addressService' defined in class path resource [mock-beans.xml]; nested exception is java.lang.ClassNotFoundException: tst.mycompany.mocks.MockAdressService
はここで更新モックbeans.xmlのさ:
<beans xmlns="http://www.springframework.org/schema/beans">
<!-- Override bean definitions with mocks -->
<bean id="thirdPartyClientService" class="tst.mycompany.mocks.MockThirdPartyClient" />
<bean id="addressService" class="tst.mycompany.mocks.MockAdressService" />
</beans>
は、ここで私は作品の初期の依存関係をautowire方法は次のとおりです。
@Autowired ThirdPartyClientServiceI client;
そうでないもの:
@Autowired AddressServiceI addressService;
私はすべてのスペルをチェックしています。すべてが並んでいますので、これはうまくいきません。私は間違ってキャッシュされているものがあるかもしれないと思っていたかもしれませんが、私はリフレッシュしてすべてを清掃しましたが、ダイスはまだありません。
これらのモックのいずれも注釈FWIWを持っていません。彼らは模倣するように設計されているサービスのインターフェイスを実装しています。それらは両方ともsrc/test/java
フォルダの下にあります。
package tst.mycompany.mocks;
public class MockAddressService implements AddressServiceI {
問題のスタックトレース:
Caused by: java.lang.ClassNotFoundException: tst.mycompany.mocks.MockAdressService
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[?:1.8.0_71]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_71]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[?:1.8.0_71]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_71]
at org.springframework.util.ClassUtils.forName(ClassUtils.java:250) ~[spring-core-4.2.2.RELEASE.jar:4.2.2.RELEASE]
私はbinフォルダをチェックしなかったし、コンパイル済みのクラスがあります。
(「d」を欠落している)MockAddressServiceが、小さくしなければなりません推測:あなたはあなたのモックと "オーバーライド"している既存のアドレスサービス...あなたの本当の豆は同じイドですか? 'bean id =" addressService " – Pras
AddressServiceIを実装する"実際の "BeanはAddressServiceと呼ばれ、' @Autowired AddressServiceI addressService'のように自動的に呼び出されます。私はmock-beans.xmlのidを修正しようとしましたが、修正されませんでした。私はまた、 'addressService'フィールドインスタンス変数の名前を絶望の行為で変更しようとしましたが、無駄にしました。 – IcedDante