Подтвердить что ты не робот

Тестирование с помощью Spring и Maven: applicationContext

Кажется, этот вопрос старый как мир, но я до сих пор не могу найти решение.

Я пытаюсь запустить простой тест:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/applicationContext.xml", "/PersonsPopulateTest-context.xml"})
@Transactional
public class PersonsPopulateTest {

Файлы находятся по адресу:

src
   main
      resources
           applicationContext.xml

и

src        
   test
      resources
          PersonsPopulateTest-context.xml 

Поэтому после создания эти файлы находятся в target/classes и целевых/тестовых классах

Но команда mvn test все еще говорит: Не удалось загрузить ApplicationContext

Какие официальные документы говорят:

@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from "/applicationContext.xml" and "/applicationContext-test.xml"
// in the root of the classpath
@ContextConfiguration(locations={"/applicationContext.xml", "/applicationContext-test.xml"})
public class MyTest {
    // class body...
}

Где я ошибся?

Спасибо, Vlaidimir

UPDATE. Surefire-отчеты:

java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: java.lang.IllegalArgumentException: Can not load an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration.
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:117)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
... 30 more
4b9b3361

Ответ 1

Я думаю, что Maven просто не включил XML файл из main/resources.

Вы можете попытаться явно указать, что включить в pom.xml.

Сообщите мне, работает ли следующая конфигурация:

    <!-- Add this directly after the <build>-opening tag -->
    <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/test/resources</directory>
            <includes>
                <include>**/*.properties</include>
            </includes>
            <excludes>
                <exclude>**/*local.properties</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>

Это то, что я использую в вашем случае. Вы можете отредактировать это, если у вас нет файлов свойств, которые необходимо включить.

Ответ 2

Мой тестовый контекстный файл находится в папке src\test\resources\spring. Мне удалось загрузить контекст с помощью

@ContextConfiguration(locations={"classpath:**/test-context.xml"})

Но ссылка (в test-context.xml) на application-context.xml, которая находится под папкой src\main\resources\spring, не удалось

Мне удалось загрузить контекст приложения, создав класс ClassPaспасибоmlApplicationContext в тестовом классе с помощью

ClassPathXmlApplicationContext appContext=new ClassPathXmlApplicationContext(new String[]{"classpath:spring/application-context.xml","classpath:spring/model-context.xml"});

Сообщите мне, если это поможет или может создать любые другие проблемы.

Ответ 3

Я думаю, что наилучшей практикой является размещение вашего файла контекста приложения для тестирования PersonPopulateTest-context.xml в разделе src/test/resources. Этот файл будет скопирован в target/test-classes, и вы можете обратиться к нему в своем тестовом классе, как показано ниже:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:**/PersonsPopulateTest-context.xml"})
@Transactional
public class PersonsPopulateTest {

}

Если вы все еще хотите обратиться к applicationContext.xml в разделе src/main/resources, вам необходимо включить его следующим образом:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/resources/applicationContext.xml"})
@Transactional
public class PersonsPopulateTest {

}

Это сработало для меня.

Ответ 4

<!-- Add this directly after the <build>-opening tag in the your pom-->
            <testResources>
                <testResource>
                    <directory>src/test/resources</directory>
                    <filtering>true</filtering>
                    <includes>
                        <include>**/*.xml</include>
                        <include>**/*.properties</include>
                    </includes>
                </testResource>
            </testResources>

Ответ 5

ваш контекст приложения должен быть включен в classpath и поместить *:

@ContextConfiguration(locations = { "classpath:*/application-context.xml" })

Ответ 6

По какой-то причине у меня была такая же проблема, и она работала, когда я запускал тест maven с JDK6 вместо JDK8 (в моем случае это устаревшее приложение)

Если это кому-то помогает.

Ответ 7

У меня была та же проблема, все файлы были успешно скопированы в target/classes и целевые/тестовые классы, все еще spring не смогли найти их.

Проблема исчезла, когда я явно указал версии для maven-surefire-plugin и maven-resources-plugin в pom

Ответ 8

ОБНОВЛЕНО: На самом деле в моем случае проблема была включена в опции Compile on Save. Я использую Netbeans, а опция была установлена ​​на For test execution only. Это значение компилирует измененные файлы и заменяет ресурсы новыми файлами. Однако из-за использования ресурсов приложения дополнительно для тестирования ресурсов For test execution only создает неправильно созданные ресурсы в папке target.

Изменение Compile on Save=For test execution only

до Compile on Save=Disable устраняет проблему.

Текст ниже также верен, но иногда он не работает. Он работал только до перезапуска Netbeans IDE. Однако детали причины проблемы правильны, поэтому я предпочитаю оставить тест.


OLD: В моей ситуации я appContext-test.xml в src/main/resources. Когда я меняю код и запускаю один unit test (не все), он перекомпилирует и правильно выполнит. Но если я снова запустил такой же unit test, он не сработал с java.lang.IllegalStateException: Failed to load ApplicationContext.

Я изменил pom.xml из

<build>
    <resources>
        <resource>
            <directory>${project.basedir}/src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>${project.basedir}/src/test/java/resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
</build>

to

<build>
    <resources>
        <resource>
            <directory>${project.basedir}/src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>${project.basedir}/src/main/resources</directory>
            <filtering>true</filtering>
        </testResource>
        <testResource>
            <directory>${project.basedir}/src/test/java/resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
</build>

и теперь все работает нормально.

Ошибка appContext-test.xml использует src/main/resources/my.properties файл с большим количеством переменных, таких как

database.url = ${database.url}
database.username = ${database.username}
database.password = ${database.password}

которые заполняются во время сборки. Однако, если вы пропустите src/main/resources в testResource, то my.properties добавляется к target/classes/my.properties как есть, т.е. без замены. Этот файл, конечно, нарушает контекст.

PS: Вы можете удалить ${project.basedir}/ - это моя обычная вещь.

Ответ 9

Я столкнулся с той же проблемой. Для меня тест прошел успешно через eclipse. Однако, когда я запускал mvn test, это давало мне ошибку: Не удалось загрузить ApplicationContext

Исправлено: Добавлен каталог src/test/resources в classpath of surefire плагин as -

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
            <configuration>
                <additionalClasspathElements>
                    <additionalClasspathElement>${project.basedir}/src/test/resources</additionalClasspathElement>
                </additionalClasspathElements>      
            </configuration>

Добавление пути к SureFire

Надеюсь, что это поможет.