Arquillian을 사용하여 JavaEE 단위 테스트를 Liberty에서 수행

처음에



JavaEE를 이용한 프로젝트에서 JUnit을 이용한 테스트 코드를 준비할 필요가 있어 eclipse상에서 유닛 테스트를 실시할 때까지를 모아 보겠습니다.

Arquillian이란?



JavaEE 어플리케이션 서버의 컨테이너 관리와 제휴해, JUnit등의 테스팅 프레임워크상에서 테스트 코드를 실행할 수 있기 때문에, JPA나 JAX-RS등의 JavaEE의 컴퍼넌트의 테스트를 할 수 있다.
참고 : ㅡㅡㅡㅜㅜㅜㅜㅜㅜㅜ 오 rg

구현 환경



· macOS 10.14.6
· Eclipse 2019-06
· WebSphere Application Server 버전 19.0.0.7 Liberty
· Java SE 1.8

전제 조건



Arquillian의 GettingStarted 실시



본가 Arquillian의 GettingStarted를 실시해 보아, 한가지의 Arquillian의 동작을 실시할 수 있는 것을 확인한 후, 어플리케이션 서버를 Liberty로 변경해 실시해 본다.
참고 : ㅡㅡㅡㅜㅜㅜㅜㅜㅜㅜ 오 rg/구이데 s/갓친g_s rdd_자/

Liberty용 원격 컨테이너 사용



위의 GettingStarted에서는, WeldEE, GlassFish의 임베디드 컨테이너, JBoss의 매니지드 컨테이너를 이용할 수 있을 때까지가 망라되어 있기 때문에, 이번은 Liberty의 리모트 컨테이너를 이용할 수 있는 곳까지를 확인한다.

Liberty의 원격 컨테이너 문서는 다음과 같습니다.
참고 : htps : // 기주 b. 코 m/오페 rty/ぃべ rty- r 쿠 ぃ 앙/bぉb/마s테 r/ぃ베 rty레테테/레아 D메. md

테스트 실시



이클립스 프로젝트 만들기



앞의 Arquillian의 GenttingStarted에서는 maven 명령에서 Java 프로젝트를 만들었지만 이번에는 eclipse에서 Maven Project를 만들어 전용 maven 프로젝트를 만들었습니다.
또, Maven의 프로파일로 컨테이너의 전환을 실현하고 있지만, 이번은 전용의 프로젝트를 준비한다.

샘플 코드를 만든 프로젝트로 복사



GettingStarted에서 이용하고 있는 Greeter.java,PhraseBuilder.java와 GreeterTest.java를 작성한 프로젝트에 배치한다.


pom.xml 편집



GettingStarted에서 이용한 pom.xml을 유용하게 작성한다.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.arquillian.example</groupId>
    <artifactId>arquillian-liberty-remote</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jboss.arquillian</groupId>
                <artifactId>arquillian-bom</artifactId>
                <version>1.4.0.Final</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.17</version>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.openliberty.arquillian</groupId>
            <artifactId>arquillian-liberty-remote</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>tools</artifactId>
            <version>1.6</version>
            <scope>system</scope>
            <systemPath>/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/tools.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

tools.jar에 종속성을 추가하는 이유는 m2eclipse의 종속성 해결 때문입니다.

arquillian.xml 배치



원격 컨테이너에 대한 구성 파일을 준비합니다. Arquillian Liberty Remote Documentation 를 참고한다.

arquillian.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<arquillian
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://jboss.org/schema/arquillian"
    xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

    <engine>
        <property name="deploymentExportPath">target/</property>
    </engine>

    <container qualifier="liberty-remote" default="true">
        <configuration>
            <property name="hostName">localhost</property>
            <property name="serverName">defaultServer</property>
            <property name="username">admin</property>
            <property name="password">admin</property>
            <property name="httpPort">9080</property>
            <property name="httpsPort">9443</property>
        </configuration>
    </container>
</arquillian>


server.xml에 추가



Liberty에 설정 정보를 추가합니다. 이쪽도 Arquillian Liberty Remote Documentation 를 참고로 한다.

server.xml
<server description="new server">

    <!-- Enable features -->
    <featureManager>
        <feature>restConnector-2.0</feature>
        <feature>webProfile-8.0</feature>
    </featureManager>
    <!-- To access this server from a remote client add a host attribute to 
        the following element, e.g. host="*" -->
    <httpEndpoint httpPort="9080" httpsPort="9443"
        id="defaultHttpEndpoint" />

    <quickStartSecurity userName="admin"
        userPassword="admin" />

    <keyStore id="defaultKeyStore" password="password" />


    <!-- This section is needed to allow upload of files to the dropins directory, 
        the remote container adapter relies on this configuration -->
    <remoteFileAccess>
        <writeDir>${server.config.dir}/dropins</writeDir>
    </remoteFileAccess>

</server>

주의점으로서는
- 후술하는 GreeterTest에서 CDI를 이용하고 있다. → webProfile-8.0의 feature를 이용
- restConnector의 feature를 webProfile-8.0에 맞추어 2.0을 선택
상기를 가미한 feature를 선택하고 있다.

인증서 가져오기



이 단계에서 Liberty를 시작하고 GreeterTest의 should_create_greeting 테스트를 실행해보십시오.

GreeterTest.java
    // 中略 ...
    @Test
    public void should_create_greeting() {
        Assert.assertEquals("Hello, Earthling!", greeter.createGreeting("Earthling"));
        greeter.greet(System.out, "Earthling");
    }


실행하면 아래와 같은 에러로 실패한다.
org.jboss.arquillian.container.spi.client.container.LifecycleException: Could not determine remote server status : sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at io.openliberty.arquillian.remote.WLPRemoteContainer.start(WLPRemoteContainer.java:87)
    at org.jboss.arquillian.container.impl.ContainerImpl.start(ContainerImpl.java:179)
// 中略 ...


Liberty에 SSL 접속을 실시하려고 하고 있지만, 필요한 증명서가 없기 때문에, 에러가 되고 있다.
아래의 기사를 참고로 증명서의 임포트를 실시한다.
참고 : Java에서 SSL 인증서 오류를 해결 / 피하고 HTTPS로 연결하는 방법
$ openssl s_client -connect localhost:9443 < /dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /tmp/cacert.crt
$ sudo /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/bin/keytool -import -alias localhost -keystore /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/security/cacerts -file /tmp/cacert.crt 

파일 경로를 지정하는 부분은 적절하게 환경에 맞게 변경한다.

JUnit 구현



Liberty가 시작되었는지 확인한 다음 다시 GreeterTest should_create_greeting 테스트를 실행해보십시오.



테스트가 성공했으며 콘솔에 Hello, Earthling!이 표시됩니다.

결론



리모트 컨테이너를 이용한 Arquillian의 유닛 테스트는, 사전에 어플리케이션 서버를 기동해 두는 것이 필요하게 되지만, 테스트의 실시마다 어플리케이션 서버의 기동·정지가 불필요하기 때문에, 거기의 스피드로 유닛 테스트 를 실시할 수 있다.

좋은 웹페이지 즐겨찾기