JUnit5 の例外のテスト

今更、JUnit4 → 5 の差異のメモです。
未だに JUnit4 を使い続けてるプロジェクトも多いのですが、
JUnit5 の例外のテストは、変わったのを改めてメモ、どこにでも解説があるので、
なんで、今さら。。。とは言え、書き留めます。

JUnit 4 では、、

@Test(expected = Exception.class)
public void testThrowsException() throws Exception {
    // ...
}

JUnit 5 では、、、

@Test
publicvoid testThrowsException() throws Exception {
    Assertions.assertThrows(Exception.class, () -> {
        //...
    });
}

例)NumberFormatException を起こす処理

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.hamcrest.MatcherAssert;
import org.hamcrest.CoreMatchers;
@Test
publicvoid testThrowsException() {
    NumberFormatException ex = Assertions.assertThrows(NumberFormatException.class, ()->{
        // NumberFormatException を起こす処理
    });
    Assertions.assertEquals("For input string: \"A\"", ex.getMessage());
    MatcherAssert.assertThat(ex.getMessage(), CoreMatchers.is("For input string: \"A\""));
}

pom.xml の記述

<dependency>
   <groupId>org.junit.jupiter</groupId>
   <artifactId>junit-jupiter</artifactId>
   <version>5.9.1</version>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>org.junit.jupiter</groupId>
   <artifactId>junit-jupiter-engine</artifactId>
   <version>5.9.1</version>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>org.assertj</groupId>
   <artifactId>assertj-core</artifactId>
   <version>3.23.1</version>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>org.hamcrest</groupId>
   <artifactId>hamcrest-library</artifactId>
   <version>2.2</version>
   <scope>test</scope>
</dependency>

Eclipseなど開発IDEJunit実行ではなく、mvn でテスト実行の為に、、、

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>
</build>

Maven リポジトリにないライブラリを取り込む方法

Maven Central repository や、他の公開リポジトリに存在しなくて、自プロジェクトで抱えて
pom.xml で指定する方法

maven-install-plugin を使う

例)プロジェクトの直下に、lib というフォルダを用意して JAR を置いた例

${project.basedir}/lib/custom-1.2.jar

の場合、

<build>
   <pluginManagement>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <inherited>false</inherited>
            <version>2.5.2</version>
            <executions>
               <execution>
                  <id>custom</id>
                  <phase>clean</phase>
                  <goals>
                     <goal>install-file</goal>
                  </goals>
                  <configuration>
                     <file>${project.basedir}/lib/custom-1.2.jar</file>
                     <groupId>org.cusotm</groupId>
                     <artifactId>custom</artifactId>
                     <version>1.2</version>
                     <packaging>jar</packaging>
                  </configuration>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </pluginManagement>
</build>

dependency は、上に書いた groupId , artifactId に沿って

<dependencies>

   <dependency>
      <groupId>org.cusotm</groupId>
      <artifactId>custom</artifactId>
      <version>1.2</version>
   </dependency>

</dependencies>

もし、プロジェクトーMavenサブモジュールで使う場合、
${project.basedir}/lib/custom-1.2.jar
という指定ではなく、
${maven.multiModuleProjectDirectory}/lib/custom-1.2.jar
という指定で、configuration は記述すつ必要がある

<configuration>
   <file>${maven.multiModuleProjectDirectory}/lib/custom-1.2.jar</file>
   <groupId>org.cusotm</groupId>
   <artifactId>custom</artifactId>
   <version>1.2</version>
   <packaging>jar</packaging>
</configuration>

PyScript を試す

pyscript.net

GIt-Hub : GitHub - pyscript/pyscript: Home Page: https://pyscript.net Examples: https://pyscript.net/examples


紹介記事
https://ops.jig-saw.com/tech-cate/new-tech-pyscript

💘HTML上でPythonコードが使える、PyScriptを用いて静的サイトを作成しました - 立命館大学情報理工学部サイバーセキュリティ研究室

PyScriptとは?使ってみた感想とメリットデメリットを紹介 | kajiblo ITブログ

「PyScript」はJavaScriptのようにPythonコードをHTML内に記述して実行可能、Anacondaがオープンソースで公開 - Publickey

最近話題のPyScriptを使ってみる - Qiita

PyScriptで線形回帰してみた | ⬢ Appirits spirits


PyCharm で、、、
PyScript | PyCharm ドキュメント

Hellow World

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
    <script defer src="https://pyscript.net/latest/pyscript.js"></script>
  </head>
  <body>
    <py-script>
        print('Hello, World!')
    </py-script>
  </body>
</html>

でも、まだ遅いなあ。

MySQL で、PostgreSQL の generate_series関数と同じことをする。

MySQLPostgreSQL の generate_series 関数と同じことをしようとすると、

SET @num:= 0;
SELECT @num:=@num+1 FROM `information_schema`.COLUMNS LIMIT 4;


でもこれでは SET 文と2つの文になってしまって mybatis での実行では都合が悪い。

SELECT a.VC
FROM (
    SELECT 0 AS VC FROM DUAL WHERE (@num:=1-1) * 0
    UNION ALL
    SELECT @num:=@num+1 FROM information_schema.COLUMNS LIMIT 4
)  a
;

これで、PostgreSQL

SELECT GENERATE_SERIES(1, 4)

と同じことができる。

これを'0'埋めの文字列にしたければ、

SELECT LPAD(a.VC, 4, '0') AS UCD
FROM (
    SELECT 0 AS VC FROM DUAL WHERE (@num:=1-1) * 0
    UNION ALL
    SELECT @num:=@num+1 FROM information_schema.COLUMNS LIMIT 4
)  a
;

先日投稿の PostgreSQL の generate_series - Oboe吹きプログラマの黙示録
で書いたように、、
日付 2022-12-15 から 2022-12-25 までDATE型で結果を出力する

SELECT date_add('2020-12-15', interval a.VC - 1  day) AS V_DATE
FROM (
    SELECT 0 AS VC FROM DUAL WHERE (@num:=1-1) * 0
    UNION ALL
    SELECT @num:=@num+1 FROM information_schema.COLUMNS LIMIT 11
)  a
;