logbackの単体テスト

```rubi:AppLog.java

package jp.co.logback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory

public class AppLog {
    private static Logger log = LoggerFactory.getLogger("test");

    public void logOutput() {
        log.debug("ログ出力テスト1");
        log.info("ログ出力テスト2");
        log.warn("ログ出力テスト3");
        log.error("ログ出力テスト4");
    }
}
```

```rubi:AppLogTest.java

package logback;

import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import org.junit.Assert;
import org.junit.Test;

import jp.co.logback.AppLog;

public class AppLogTest {
    private static String PRO_PATH = System.getProperty("user.dir");

    private boolean isOutputLogmsg(String logpath) {
        Scanner sc = null;

        try {
            sc = new Scanner(new File(logpath));

            while (sc.hasNextLine()) {
                String msg = sc.nextLine();
                if (contains(msg)) {
                    continue;
                } else {
                    fail(msg + "出力されていない");
                }
            }

            sc.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (sc != null) {
                sc.close();
            }
        }
        return true;
    }

    private boolean contains(String logmsg) {
        Scanner sc = null;

        try {
            sc = new Scanner(new File(PRO_PATH + "/logs/app-detail.log"));

            while (sc.hasNextLine()) {
                if (logmsg.equals(sc.nextLine())) {
                    return true;
                }
            }

            sc.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (sc != null) {
                sc.close();
            }
        }
        return false;
    }

    @Test
    public void testName() throws Exception {
        AppLog app = new AppLog();

        app.logOutput();

        String logpath = PRO_PATH + "/src/test/resources/AppLog/exp_app-detail.log";
        Assert.assertTrue(isOutputLogmsg(logpath));
    }
}
```

```rubi:exp_app-detail.log

ログ出力テスト1
ログ出力テスト2
ログ出力テスト3
ログ出力テスト4
```

```rubi:logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE logback>
<configuration>
  <appender name="FILE" class="ch.qos.logback.core.FileAppender" >
    <file>logs/app-detail.log</file>
    <encoder>
        <pattern>%msg%n</pattern>
    </encoder>

    <append>false</append>
  </appender>
  
    <root level="DEBUG" additivity="false">
        <appender-ref ref="FILE" />
    </root>

</configuration>
```

```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>lession</groupId>
  <artifactId>logback</artifactId>
  <version>0.0.1-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.3</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.3</version>
        </dependency>

    </dependencies>
</project
```
![スクリーンショット 2023-01-15 184540.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2734236/91ce3aba-0e93-06e7-487e-11c595e2af56.png)