Я пытаюсь настроить проект с помощью Spring-Test, используя TestNg в Maven. Код похож на:
@ContextConfiguration(locations={"test-context.xml"})
public class AppTest extends AbstractTestNGSpringContextTests {
@Test
public void testApp() {
assert true;
}
}
test-context.xml просто определил bean-компонент:
<bean id="app" class="org.sonatype.mavenbook.simple.App"/>
Я получил сообщение об ошибке Не удалось загрузить ApplicationContext при запуске mvn test из командной строки, похоже, он не может найти файл test-context.xml; Тем не менее, я могу заставить его работать правильно в Eclipse (с плагином TestNg).
Итак, test-context.xml находится в каталоге src / test / resources /, как мне указать это в файле pom.xml, чтобы команда mvn test работала?
Спасибо,
UPDATE:
Спасибо за ответ. Невозможно загрузить файл контекста. Ошибка была вызвана тем, что я переместил файл в другое место, так как я считал, что проблема с classpath Теперь я обнаружил, что файл контекста кажется загруженным из выходных данных Maven, но тест не пройден:
Running TestSuite
May 25, 2010 9:55:13 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [test-context.xml]
May 25, 2010 9:55:13 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericApplicationContext@171bbc9: display name [org.springframework.context.support.GenericApplicationContext@171bbc9]; startup date [Tue May 25 09:55:13 PDT 2010]; root of context hierarchy
May 25, 2010 9:55:13 AM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.GenericApplicationContext@171bbc9]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1df8b99
May 25, 2010 9:55:13 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1df8b99: defining beans [app,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor]; root of factory hierarchy
Tests run: 3, Failures: 2, Errors: 0, Skipped: 1, Time elapsed: 0.63 sec <<< FAILURE!
Results :
Failed tests:
springTestContextBeforeTestMethod(org.sonatype.mavenbook.simple.AppTest)
springTestContextAfterTestMethod(org.sonatype.mavenbook.simple.AppTest)
Tests run: 3, Failures: 2, Errors: 0, Skipped: 1
Если я использую Spring-test версии 3.0.2. RELEASE, ошибка становится такой:
org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance() is depending on nonexistent method null
Вот структура проекта:
simple
|-- pom.xml
`-- src
|-- main
| `-- java
`-- test
|-- java
`-- resources
|-- test-context.xml
`-- testng.xml
testng.xml:
<suite name="Suite" parallel="false">
<test name="Test">
<classes>
<class name="org.sonatype.mavenbook.simple.AppTest"/>
</classes>
</test>
</suite>
тест-context.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-lazy-init="true">
<bean id="app" class="org.sonatype.mavenbook.simple.App"/>
</beans>
В pom.xml я добавляю артефакты testng, spring и spring-test и плагин:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.1</version>
<classifier>jdk15</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>2.5.6</version>
<scope>test</scope>
</dependency>
<build>
<finalName>simple</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
По сути, я заменил Junit «Простого проекта Maven» на TestNg, надеюсь, он работает.
UPDATE:
Я думаю, у меня проблема (все еще не знаю почему) -
Всякий раз, когда я расширяю AbstractTestNGSpringContextTests или AbstractTransactionalTestNGSpringContextTests, тест завершается ошибкой:
Failed tests:
springTestContextBeforeTestMethod(org.sonatype.mavenbook.simple.AppTest)
springTestContextAfterTestMethod(org.sonatype.mavenbook.simple.AppTest)
Итак, в конце концов ошибка исчезла, когда я переопределил два метода.
Я не думаю, что это правильный путь, я не нашел много информации в весеннем тесте.
Если вы знакомы с рамками весенних испытаний, пожалуйста, расскажите немного об этом.