春ベースのアプリケーション atm を単体テストしています。最初の問題は、サーバーで一度アプリを起動していない場合、単体テストがすべて失敗することです。最初にサーバー上でアプリを起動 (および停止) すると、単体テストが機能します。
サーバーを起動しないと、次のエラーが発生します。
... java.io.FileNotFoundException: class path resource [META-INF/spring/applicationContext-test.xml] cannot be opened because it does not exist
私のユニットテストは次のように定義されています:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/META-INF/spring/applicationContext-test.xml" })
@TransactionConfiguration
@Transactional
public class InventoryControllerTest extends AbstractTransactionalJUnit4SpringContextTests {
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private AnnotationMethodHandlerAdapter handlerAdapter;
@Before
public void setUp() throws Exception {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
handlerAdapter = applicationContext
.getBean(AnnotationMethodHandlerAdapter.class);
}
//... tests
}
私が言ったように、アプリを一度起動したことがあれば、すべて正常に動作します。
そのため、構成の場所を locations = { "classpath/META-INF/spring/applicationContext-test.xml" }) に変更しましたが、努力なしで、上記と同じ例外です。
さらに取得する唯一の方法は、この場所です: locations = { "classpath*:applicationContext-test.xml" }) 次に、この例外が発生します: [javax.sql.DataSource] 型の一致する Bean が依存関係で見つかりません: 少なくとも期待されますこの依存関係のオートワイヤー候補として適格な 1 つの Bean。依存関係の注釈: {}
しかし、これは紛らわしいです。なぜなら、テスト コンテキスト ファイルに確実にデータソースがあるからです。
<bean class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" id="dataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:testdb;sql.syntax_ora=true" />
<property name="username" value="some" />
<property name="password" value="some" />
</bean>
EIDT 2
問題が RunWith(...) であることを認識した後、Spring クラスを同時に拡張し、ロケーション パスからすべてのワイルドカードを削除します。私はこの例外を受け取ります:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [META-INF/spring/applicationContext-test.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [META-INF/spring/applicationContext-test.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No persistence unit with name 'persistenceUnitTest' found
... 24 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [META-INF/spring/applicationContext-test.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No persistence unit with name 'persistenceUnitTest' found
... 40 more
Caused by: java.lang.IllegalArgumentException: No persistence unit with name 'persistenceUnitTest' found)
... 47 more
私は本当に助けていただければ幸いです!
前もって感謝します