1. 概要

Spring BootアプリケーションでのBeanのハイブリッド定義は、アノテーションベースの構成とXMLベースの構成の両方を含むものです。 この環境では、テストクラスでXMLベースの構成を使用したい場合があります。 ただし、この状況では、アプリケーションコンテキストの読み込みエラー「 ApplicationContextの読み込みに失敗しました。」が発生する場合があります。このエラーは、アプリケーションコンテキストがテストコンテキストに読み込まれていないため、テストクラスに表示されます。

このチュートリアルでは、XMLアプリケーションコンテキストをSpring Bootアプリケーションのテストに統合する方法について説明します。

2. 「ApplicationContextのロードに失敗しました」エラー

XMLベースのアプリケーションコンテキストをSpring Bootアプリケーションに統合して、エラーを再現してみましょう。

まず、サービスbeanの定義を含むapplication-context.xmlファイルがあるとします。

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
	  
    <bean id="employeeServiceImpl" class="com.baeldung.xmlapplicationcontext.service.EmployeeServiceImpl" />
</beans>

これで、application-context.xmlファイルをwebapp/ WEB-INF/の場所に追加できます。

また、サービスインターフェイスとクラスを作成します。

public interface EmployeeService {
    Employee getEmployee();
}

public class EmployeeServiceImpl implements EmployeeService {

    @Override
    public Employee getEmployee() {
        return new Employee("Baeldung", "Admin");
    }
}

最後に、アプリケーションコンテキストから EmployeeServicebeanを取得するためのテストケースを作成します。

@RunWith(SpringRunner.class)
@ContextConfiguration(locations={"classpath:WEB-INF/application-context.xml"})
public class EmployeeServiceAppContextIntegrationTest {

    @Autowired
    private EmployeeService service;

    @Test
    public void whenContextLoads_thenServiceISNotNull() {
        assertThat(service).isNotNull();
    }

}

このテストを実行しようとすると、エラーが発生します。

java.lang.IllegalStateException: Failed to load ApplicationContext

アプリケーションコンテキストがテストコンテキストにロードされていないため、このエラーはテストクラスに表示されます。 さらに、根本的な原因は、WEB-INFがクラスパスに含まれていないことです。

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

3. テストでのXMLベースのApplicationContextの使用

テストクラスでXMLベースのApplicationContextを使用する方法を見てみましょう。 テストでXMLベースのApplicationContextを使用するための2つのオプションがあります:@SpringBootTestおよび@ContextConfigurationアノテーション。

3.1. @SpringBootTestおよび@ImportResourceを使用してテストします

Spring Bootは、 @SpringBootTestアノテーションを提供します。これを使用して、テストで使用するアプリケーションコンテキストを作成できます。 さらに、 XML Beans を読み取るには、SpringBootメインクラスで@ImportResourceを使用する必要があります。 このアノテーションにより、Bean定義を含む1つ以上のリソースをインポートできます。

まず、メインクラスで@ImportResourceアノテーションを使用しましょう。

@SpringBootApplication
@ImportResource({"classpath*:application-context.xml"})

次に、アプリケーションコンテキストから EmployeeServicebeanを取得するためのテストケースを作成しましょう。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = XmlBeanApplication.class)
public class EmployeeServiceAppContextIntegrationTest {

    @Autowired
    private EmployeeService service;

    @Test
    public void whenContextLoads_thenServiceISNotNull() {
        assertThat(service).isNotNull();
    }

}

@ImportResource アノテーションは、resourceディレクトリにあるXMLBeanをロードします。 さらに、 @SpringBootTest アノテーションは、アプリケーションのBean全体をテストクラスにロードします。 したがって、テストクラスの EmployeeServicebeanにアクセスできます。

3.2. @ContextConfigurationresourcesを使用したテスト

テスト構成ファイルをsrc/ test / resources ディレクトリに配置することで、Beanのさまざまな構成でテストコンテキストを作成できます

この場合、 @ContextConfigurationアノテーションを使用して、src / test/resourcesディレクトリからテストコンテキストをロードします。

まず、EmployeeServiceインターフェイスから別のbeanを作成しましょう。

public class EmployeeServiceTestImpl implements EmployeeService {

    @Override
    public Employee getEmployee() {
        return new Employee("Baeldung-Test", "Admin");
    }
}

次に、test-context.xmlファイルをsrc/ test /resourcesディレクトリに作成します。

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
	
    <bean id="employeeServiceTestImpl" class="process.service.EmployeeServiceTestImpl" />
</beans>

最後に、テストケースを作成します。

@SpringBootTest
@ContextConfiguration(locations = "/test-context.xml")
public class EmployeeServiceTestContextIntegrationTest {

    @Autowired
    @Qualifier("employeeServiceTestImpl")
    private EmployeeService serviceTest;

    @Test
    public void whenTestContextLoads_thenServiceTestISNotNull() {
        assertThat(serviceTest).isNotNull();
    }

}

ここでは、 @ContextConfiguration アノテーションを使用して、test-context.xmlからemployeeServiceTestImplをロードしました。

3.3. @ContextConfigurationWEB-INFを使用したテスト

WEB-INFディレクトリからテストクラスのアプリケーションコンテキストをインポートすることもできます。 これを行うには、ファイルのURLを使用してアプリケーションコンテキストをアドレス指定できます。

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/application-context.xml")

4. 結論

この記事では、Spring BootアプリケーションのテストクラスでXMLベースの構成ファイルを使用する方法を学びました。 いつものように、この記事で使用されているソースコードは、GitHubからで入手できます。