[JAVA] Introduction memo of automatic test using Jenkins

Thing you want to do

Leverage Jenkins to introduce automated testing into your Java project. -When pushed to the master branch, the test will be executed automatically. -The coverage rate is displayed as the test result.

environment

macOS Big Sur 11.1 Homebrew 2.7.1-56-gff931c2 Jenkins 2.273 Java 1.8 ngrok 2.3.35

The samples used for this article can be found on Github. https://github.com/koyi2016/JenkinsDemo

Install Jenkins

Open the termiral on your mac and run the command.

brew install jenkins

It will be installed immediately, so start Jenkins.

java -jar /usr/local/opt/jenkins/libexec/jenkins.war

If it starts normally, the following message will be displayed.

Running from: /usr/local/Cellar/jenkins/2.273/libexec/jenkins.war
webroot: $user.home/.jenkins
2020-12-30 04:38:41.989+0000 [id=1]	INFO	org.eclipse.jetty.util.log.Log#initialized: Logging initialized @1700ms to org.eclipse.jetty.util.log.JavaUtilLog
2020-12-30 04:38:42.407+0000 [id=1]	INFO	winstone.Logger#logInternal: Beginning extraction from war file
2020-12-30 04:38:47.104+0000 [id=1]	WARNING	o.e.j.s.handler.ContextHandler#setContextPath: Empty contextPath
2020-12-30 04:38:47.309+0000 [id=1]	INFO	org.eclipse.jetty.server.Server#doStart: jetty-9.4.35.v20201120; built: 2020-11-20T21:17:03.964Z; git: bdc54f03a5e0a7e280fab27f55c3c75ee8da89fb; jvm 1.8.0_271-b09
2020-12-30 04:38:48.529+0000 [id=1]	INFO	o.e.j.w.StandardDescriptorProcessor#visitServlet: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
2020-12-30 04:38:48.744+0000 [id=1]	INFO	o.e.j.s.s.DefaultSessionIdManager#doStart: DefaultSessionIdManager workerName=node0
2020-12-30 04:38:48.744+0000 [id=1]	INFO	o.e.j.s.s.DefaultSessionIdManager#doStart: No SessionScavenger set, using defaults
2020-12-30 04:38:48.756+0000 [id=1]	INFO	o.e.j.server.session.HouseKeeper#startScavenging: node0 Scavenging every 600000ms
2020-12-30 04:38:50.356+0000 [id=1]	INFO	hudson.WebAppMain#contextInitialized: Jenkins home directory: /Users/user/.jenkins found at: $user.home/.jenkins
2020-12-30 04:39:01.536+0000 [id=1]	INFO	o.e.j.s.handler.ContextHandler#doStart: Started w.@4e9658b5{Jenkins v2.273,/,file:///Users/user/.jenkins/war/,AVAILABLE}{/Users/user/.jenkins/war}
2020-12-30 04:39:01.752+0000 [id=1]	INFO	o.e.j.server.AbstractConnector#doStart: Started ServerConnector@798162bc{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
2020-12-30 04:39:01.753+0000 [id=1]	INFO	org.eclipse.jetty.server.Server#doStart: Started @21464ms
2020-12-30 04:39:01.754+0000 [id=23]	INFO	winstone.Logger#logInternal: Winstone Servlet Engine running: controlPort=disabled
2020-12-30 04:39:05.365+0000 [id=30]	INFO	jenkins.InitReactorRunner$1#onAttained: Started initialization
2020-12-30 04:39:05.425+0000 [id=34]	INFO	jenkins.InitReactorRunner$1#onAttained: Listed all plugins
2020-12-30 04:39:10.521+0000 [id=34]	INFO	jenkins.InitReactorRunner$1#onAttained: Prepared all plugins
2020-12-30 04:39:10.537+0000 [id=31]	INFO	jenkins.InitReactorRunner$1#onAttained: Started all plugins
2020-12-30 04:39:10.605+0000 [id=30]	INFO	jenkins.InitReactorRunner$1#onAttained: Augmented all extensions
2020-12-30 04:39:12.522+0000 [id=28]	INFO	jenkins.InitReactorRunner$1#onAttained: System config loaded
2020-12-30 04:39:12.522+0000 [id=35]	INFO	jenkins.InitReactorRunner$1#onAttained: System config adapted
2020-12-30 04:39:12.523+0000 [id=28]	INFO	jenkins.InitReactorRunner$1#onAttained: Loaded all jobs
2020-12-30 04:39:12.532+0000 [id=28]	INFO	jenkins.InitReactorRunner$1#onAttained: Configuration for all jobs updated
2020-12-30 04:39:12.552+0000 [id=48]	INFO	hudson.model.AsyncPeriodicWork#lambda$doRun$0: Started Download metadata
2020-12-30 04:39:12.568+0000 [id=48]	INFO	hudson.util.Retrier#start: Attempt #1 to do the action check updates server
2020-12-30 04:39:13.625+0000 [id=34]	INFO	jenkins.install.SetupWizard#init: 

*************************************************************
*************************************************************
*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

68fc731c3b5f457ab6e7731dc7ce1215

This may also be found at: /Users/user/.jenkins/secrets/initialAdminPassword

*************************************************************
*************************************************************
*************************************************************

2020-12-30 04:39:35.353+0000 [id=34]	INFO	jenkins.InitReactorRunner$1#onAttained: Completed initialization
2020-12-30 04:39:35.377+0000 [id=22]	INFO	hudson.WebAppMain$3#run: Jenkins is fully up and running
2020-12-30 04:39:35.936+0000 [id=48]	INFO	h.m.DownloadService$Downloadable#load: Obtained the updated data file for hudson.tasks.Maven.MavenInstaller
2020-12-30 04:39:35.938+0000 [id=48]	INFO	hudson.util.Retrier#start: Performed the action check updates server successfully at the attempt #1
2020-12-30 04:39:35.950+0000 [id=48]	INFO	hudson.model.AsyncPeriodicWork#lambda$doRun$0: Finished Download metadata. 23,396 ms

Jenkins default settings

After the above installation is completed, start the browser and enter the following URL to open Jenkins. http://localhost:8080/

Sign_in__Jenkins__png.png

Enter the password that is automatically generated when the installation is successful in the password entry field. 「68fc731c3b5f457ab6e7731dc7ce1215」

Then select "Install suggested plugins". Setup_Wizard__Jenkins_.png

After the plugin is installed, set up an admin user. Setup_Wizard__Jenkins_.png

This completes the initial setup. ダッシュボード__Jenkins_.png

Settings for automated testing

ngrok installation

ngrok is a tool that allows you to access a server running on localhost from outside the LAN. I added ngrok so that I can communicate Jenkins built locally this time from the webhook on GitHub.

Open the termiral on your mac and run the command.

brew install ngrok

After installation, use the following command to expose the port 8080 used by Jenkins. The valid time seems to be 8 hours.

ngrok http 8080

KO_—ngrok_http_8080—_142×37.png

The URL of "http://b8dd86e8f647.ngrok.io" has been made available from the outside. This URL is used for webhooks on GitHub.

Github settings

From the target repository, click Settings> Webhooks> Add webhooks and set as follows.

スクリーンショット 2021-01-03 23.32.27.png

If you can communicate well with Jenkins, you can get a good response at "Recent Deliveries". Webhook_·http___b8dd86e8f647_ngrok_io_github-webhook-2.png

Jenkins settings

Installation of required plugins

Find and install the following plugins from Jenkins Administration> Plugins Administration> Available.

Available_Plugins__Jenkins_.png

・ GitHub Plugin (already installed when installing Jenkins)  https://plugins.jenkins.io/github/

・ GitHub Integration  https://plugins.jenkins.io/github-pullrequest/

・ Jacoco  https://plugins.jenkins.io/jacoco/

You should restart Jenkins after installing the plugin. You can restart by entering the following URL in the URL of the browser.

http://localhost:8080/safeRestart

Jenkin system settings

From Manage Jenkins> System Settings, do the following:

-Set the URL of Jenkins. システムの設定__Jenkins_.png

-Check if the "Override Hook URL" of Jenkins matches the "Payload URL" of the webhook of GitHub set above. システムの設定__Jenkins_.png

Configure the JDK from Manage Jenkins> Global Tool Configuration. Leave the other settings at their defaults.

Global_Tool_Configuration__Jenkins_.png

Creating a build job

Create a new job.

新規ジョブ作成__Jenkins_.png

Check General's GitHub project and enter the URL of the target repository.

Jenkins-demo_Config__Jenkins_.png

Check Git in Source Code Control and enter your credentials and target branch. Your credentials are an account that can access GitHub.

スクリーンショット_2021-01-04_0_17_16_png_と_Webhook_·http___b8dd86e8f647_ngrok_io_github-webhook-2_png_と_「Jenkinsを用いた自動テストの導入メモ」を編集_-_Qiita.png

"GitHub hook trigger for GITScm polling" in the build trigger Is checked.

Jenkins-demo_Config__Jenkins_.png

Select the Maven call in your build and enter clean jacoco: prepare-agent test.

Jenkins-demo_Config__Jenkins__と_「Jenkinsを用いた自動テストの導入メモ」を編集_-_Qiita.png

Select "Aggregate Junit test results" in the post-build process.

Jenkin-demo_Config__Jenkins_.png

Finally, select "Record JaCoCo coverage report" and save.

Jenkin-demo_Config__Jenkins_-2.png

You have now created a new job.

Automatic test

-Make changes to the master branch of the target repository and push.

-Detects the push from Jenkins and performs an automatic build. The result of the build is as follows. スクリーンショット_2021-01-04_0_36_43_png__2558×1372_.png

You can see the coverage details by clicking on the build history number.

Jenkins.png

Recommended Posts

Introduction memo of automatic test using Jenkins
Test the integrity of the aggregation using ArchUnit ②
Test the integrity of the aggregation using ArchUnit ③
[CircleCI] I was addicted to the automatic test of CircleCI (rails + mysql) [Memo]
Introduction of pay.jp
Introduction of milkode
Automatic creation of Java unit test result report
Story of test automation using Appium [Android / java]
Json Request in Unit Test of Controller using MockMvc
[Rails] Test of star evaluation function using Raty [Rspec]
Introduction of Docker --Part 1--
Introduction to RSpec 1. Test, RSpec
[Rails] Introduction of PAY.JP
Example of using vue.config.js
List of beginners (List) memo
Summary of using FragmentArgs
Introduction of user authentication
Summary of using DBFlow
[Rails] Implementation of automatic address input using jpostal and jp_prefecture
[Rails] Integration test using Capybara (from introduction to simple test execution)