0
0

More than 1 year has passed since last update.

JenkinsでJUnit5のテストを含んだプロジェクトを扱う備忘録

Posted at

概要

タイトルの通りです。
JenkinsでJUnit5のテストを含んだプロジェクトをテストしてからビルドする際の備忘録です。
主に自分用。

開発環境

OS : Windows 7
Java : 17.0.2
IDE : STS 4.10.0
Spring Boot : 2.6.5
Apache Tomcat : 9.0.17
Maven: 3.8.5
Jenkins: 2.332.1

JUnit5を含んだプロジェクト

プロジェクトはSTSからSpring Boot Projectとして作成しました。
フォルダ構成は次の通りです。

プロジェクト構成
プロジェクト構成
(project root)
|   .classpath
|   .factorypath
|   .gitignore
|   .project
|   HELP.md
|   mvnw
|   mvnw.cmd
|   pom.xml
|   
+---.mvn
|   \---wrapper
|           maven-wrapper.jar
|           maven-wrapper.properties
|           
+---.settings
|       org.eclipse.core.resources.prefs
|       org.eclipse.jdt.apt.core.prefs
|       org.eclipse.jdt.core.prefs
|       org.eclipse.m2e.core.prefs
|       org.eclipse.wst.common.component
|       org.eclipse.wst.common.project.facet.core.xml
|       org.eclipse.wst.validation.prefs
|       org.springframework.ide.eclipse.prefs
|       
+---src
|   +---main
|   |   +---java
|   |   |   \---jp
|   |   |       \---co
|   |   |           \---illmatics
|   |   |               \---app
|   |   |                   \---sample
|   |   |                           FibonacciNumberCreator.java
|   |   |                           JenkinsSampleApplication.java
|   |   |                           ServletInitializer.java
|   |   |                           
|   |   +---resources
|   |   |       application.properties
|   |   \---webapp
|   \---test
|       \---java
|           \---jp
|               \---co
|                   \---illmatics
|                       \---app
|                           \---sample
|                                   FibonacciNumberCreatorTest.java
|                                   JUnit5ParameterizedTests.java
|                                   JUnit5Tests.java
\---target
    +---classes
    |   |   application.properties
    |   |   
    |   \---jp
    |       \---co
    |           \---illmatics
    |               \---app
    |                   \---sample
    |                           FibonacciNumberCreator.class
    |                           JenkinsSampleApplication.class
    |                           ServletInitializer.class
    |                           
    +---m2e-wtp
    |   \---web-resources
    |       \---META-INF
    |           |   MANIFEST.MF
    |           |   
    |           \---maven
    |               \---com.example
    |                   \---JenkinsSample
    |                           pom.properties
    |                           pom.xml
    |                           
    +---maven-status
    |   \---maven-compiler-plugin
    |       +---compile
    |       |   \---default-compile
    |       |           inputFiles.lst
    |       |           
    |       \---testCompile
    |           \---default-testCompile
    |                   inputFiles.lst
    |                   
    +---surefire-reports
    |       jp.co.illmatics.app.sample.FibonacciNumberCreatorTest.txt
    |       jp.co.illmatics.app.sample.JUnit5ParameterizedTests.txt
    |       jp.co.illmatics.app.sample.JUnit5Tests.txt
    |       TEST-jp.co.illmatics.app.sample.FibonacciNumberCreatorTest.xml
    |       TEST-jp.co.illmatics.app.sample.JUnit5ParameterizedTests.xml
    |       TEST-jp.co.illmatics.app.sample.JUnit5Tests.xml
    |       
    \---test-classes
        \---jp
            \---co
                \---illmatics
                    \---app
                        \---sample
                                FibonacciNumberCreatorTest.class
                                JUnit5ParameterizedTests.class
                                JUnit5Tests.class

pom.xmlについて

JUnit5はプロジェクト作成時にdependencyを指定できないので、
pom.xmlに追記します。(記事を作成した2022/3/29時点)
また、spring-boot-starter-testが競合するため、削除します。
※説明用にコメントアウトにしています。

pom.xml
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.5</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>JenkinsSample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>JenkinsSample</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
<!-- spring-boot-starter-testにJUnit4が含まれるためコメントアウト -->
<!-- 	
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
-->
		<dependency>
		    <groupId>org.junit.jupiter</groupId>
		    <artifactId>junit-jupiter</artifactId>
		    <version>5.8.2</version>
		    <scope>test</scope>
		</dependency>
		<dependency>
		    <groupId>org.junit.jupiter</groupId>
		    <artifactId>junit-jupiter-params</artifactId>
		    <version>5.8.2</version>
		    <scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

作ったテストケース

JUnit5を試した記事で作ったケースをそのまま流用しています。
テストクラスはFibonacciNumberCreatorTest.java, JUnit5ParameterizedTests.java, JUnit5Tests.javaです。

Jenkinsのセットアップ

Jenkinsのセットアップはこちらで確認できます。
かいつまんで以下の通りです。

以下、ブラウザ表示結果です。
warningが出ていますが、今回は無視します。
image.png

ジョブ作成

  • 新規ジョブ作成 -> ジョブ名入力
  • フリースタイル・プロジェクトのビルドを選択 -> OKボタンでジョブを作成
  • ビルド環境タブ -> ビルド -> Windowsバッチコマンドの実行から下記を入力
入力内容
入力内容
@echo off
setlocal
cd C:\work\eclipse-workspace\workspace-STS4-4.10.0\JenkinsSample
mvn test
echo error level = %ERRORLEVEL%
if not %ERRORLEVEL% == 0 (
    endlocal
    exit /b %ERRORLEVEL%
)

mvn compile
echo error level = %ERRORLEVEL%
if not %ERRORLEVEL% == 0 (
    endlocal
    exit /b %ERRORLEVEL%
)

endlocal
exit /b

実行結果

ジョブを選択した状態からジョブ実行すると
その結果がコンソール出力に記録されています。

実行結果
実行結果
ユーザーjenkinsが実行
Running as SYSTEM
ビルドします。 ワークスペース: C:\Users\************\.jenkins\workspace\test
[test] $ cmd /c call C:\apache-tomcat-9.0.17\temp\jenkins3164356649722298275.bat
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< com.example:JenkinsSample >----------------------
[INFO] Building JenkinsSample 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ JenkinsSample ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ JenkinsSample ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ JenkinsSample ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory C:\work\eclipse-workspace\workspace-STS4-4.10.0\JenkinsSample\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ JenkinsSample ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ JenkinsSample ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running jp.co.illmatics.app.sample.FibonacciNumberCreatorTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.041 s - in jp.co.illmatics.app.sample.FibonacciNumberCreatorTest
[INFO] Running jp.co.illmatics.app.sample.JUnit5ParameterizedTests
[INFO] Tests run: 7, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.1 s - in jp.co.illmatics.app.sample.JUnit5ParameterizedTests
[INFO] Running jp.co.illmatics.app.sample.JUnit5Tests
DefaultTestInfo [displayName = 'testAboutTemporaryFile(TestInfo)', tags = [], testClass = class jp.co.illmatics.app.sample.JUnit5Tests, testMethod = public void jp.co.illmatics.app.sample.JUnit5Tests.testAboutTemporaryFile(org.junit.jupiter.api.TestInfo) throws java.lang.Exception] is started.
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.073 s - in jp.co.illmatics.app.sample.JUnit5Tests
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 9, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.255 s
[INFO] Finished at: 2022-03-30T19:04:27+09:00
[INFO] ------------------------------------------------------------------------
Finished: SUCCESS

JUnit5を使っても問題なくJenkinsでテストしてからビルドできました。

最後に

お読みいただき、ありがとうございました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0