wancoro blog

学んだことをアウトプットしていきたいと思います

【Junit5】SpringプロジェクトにJacocoを実装する

作業環境

Jacocoとは

テスト時のカバレッジを取得し、HTMLファイルに出力してくれるツールです。 https://www.eclemma.org/jacoco/

実行手順

pom.xml にjacocoのプラグインを追加する。

<dependency>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.3</version>
  <scope>test</scope>
</dependency>

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.3</version>
     <executions>
       <execution>
         <id>prepare-agent</id>
         <goals>
           <goal>prepare-agent</goal>
         </goals>
       </execution>
       <execution>
         <id>report</id>
         <phase>test</phase>
         <goals>
           <goal>report</goal>
         </goals>
       </execution>
     </executions>
</plugin>

// これがないとカバレッジが反映されなかった
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M4</version>
</plugin>

以下コマンドを実行すると、target/site/jacoco/index.htmlが生成される。

mvn clean jacoco:prepare-agent test jacoco:report

IntteliJでindex.htmlを右クリック--> Open in Browserからブラウザを選択 f:id:wancoromochi:20200418150426p:plain すると、ブラウザ上でカバレッジが確認できる。 f:id:wancoromochi:20200418150514p:plain

そのほか

IntelliJRunを実行するとエラーが出るようになってしまった。 f:id:wancoromochi:20200418150818p:plain pom.xmlに以下を追加して解決。

<dependency>
  <groupId>org.junit.vintage</groupId>
  <artifactId>junit-vintage-engine</artifactId>
  <scope>test</scope>
</dependency>

【Tomcat】At least one JAR was scanned for TLDs yet contained no TLDs

Tomcatサーバ起動時にエラー

At least one JAR was scanned for TLDs yet contained no TLDs. 
Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. 
Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.

tomcat/conf/catalina.propertiesファイルに以下を追記して解決

tomcat.util.scan.StandardJarScanFilter.jarsToSkip=\*.jar

参考リンク: stackoverflow.com

2020/4/18更新

上記設定をすると、おそらくtomcatがjarファイルを読んでくれなくてdeployできなくなったのでダメでした。 なので未解決・・・。

Circular view path [login]: would dispatch back to the current handler URL [/login] again.

Junit5でのテスト時に丸一日ハマっためも

javax.servlet.ServletException: Circular view path [login]: 
    would dispatch back to the current handler URL [/login] again. 
    Check your ViewResolver setup!
    (Hint: This may be the result of an unspecified view, due to default view name generation.)

以下方法で解決。 原因は理解できてない。 www.programmersought.com

DockerのRailsアプリにfirefoxをインストールする

apt-getだとパッケージが見つからないとエラーが出てインストールできなかったので
以下URLを参考に、wgetでインストールした。

geckoドライバーも必要。

github.com

インストール自体はできているはずが、まだエラーがでる。

$ firefox -v
XPCOMGlueLoad error for file /opt/firefox-73.0.1/libxul.so:
libdbus-glid-1.so.2: cannot open shared object file: No such file or directory
Couldn't load XPCOM.

今回の場合、libdbus-glid-1.so.2が見つからずエラーが出ているようなので、 libdbus-glib-1-2パッケージをインストールするように追記して解決。

【Selenium】Chromeがheadlessモードにならない

chrome_optionsheadlessdisable-gpuを追加しても
GUIが起動してしまう・・・。

どうやらChromeDriverのバージョンが75以上の場合は
w3c: falseを追加する必要があるらしい

qiita.com

これでheadlessモードで動かすことができました

require 'capybara/rspec'
require 'selenium-webdriver'

Capybara.default_driver = :selenium
Capybara.default_max_wait_time = 10

Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(
    app,
    browser: :chrome,
    desired_capabilities: Selenium::WebDriver::Remote::Capabilities.chrome(
      chrome_options: {
        args: %w(headless disable-gpu no-sandbox window-size=1680,1050),
        w3c: false
      }
    )
  )
end

Capybara.javascript_driver = :selenium

E2Eテストで使用したCapybaraのマッチャまとめ

開発環境

Ubuntu 18.04
Rails 5.2

Rspec
Capybara
Selenium

have_selector

特定のクラス内のテキストを検証

<div class="w2ui-col-header" >id</div>
expect(page).to have_selector ".w2ui-col-header", text: "id"

have_content

特定の文字列が表示されていることを検証

expect(page).to have_content "Create New Task"

have_field

テキストボックスに特定の値が入っていることを検証

<label for="task_name">Name</label>
<input type="text" id="task_name" value="test1">
expect(page).to have_field "Name", with: "test1"

have_select

セレクトボックスで特定の値が選択されていることを検証

expect(page).to have_select("Priority", selected: "high")

have_css

特定のCSSが存在することを検証

 expect(page).to have_css ".w2ui-sort-up"

have_no_content

特定の文字列が表示されていないことを検証

expect(page).to have_no_content "test1"

have_no_css

特定のCSSが存在しないことを検証

 expect(page).to have_no_css ".w2ui-sort-up"

find(要素).click

特定の要素を取得してクリックする その要素がボタンやリンクじゃなくてもOK

<div class="w2ui-button">Add</div>
find(".w2ui-button").click

first(要素).click

最初に見つけた要素をクリックする

<div class="w2ui-sort-up"></div>
first(:css, ".w2ui-sort-up").click

click_on

ボタンもしくはリンクをクリックする

click_on "登録する"

fill_in

フォームにテキストを入力

<label for="task_name">Name</label>
<input type="text" id="task_name">
fill_in "Name", with: "test"

select

セレクトボックスを選択

<label for="task_priority">Priority</label>
<select id="task_priority">
  <option>high</option>
  <option>low</option>
</select>
select "high", from: "Priority"

end_with

文字列の末尾を検証

expect(current_url).to end_with "tasks/new"

confirmのテキストを検証する

expect(page.driver.browser.switch_to.alert.text).to eq "Are you sure?"

acceptもしくはdissmissを使ってポップアップを閉じないと ブラウザがうまく閉じられなくてエラーが出ます

confirmでOKを選択する

 page.driver.browser.switch_to.alert.accept

confirmでキャンセルを選択する

 page.driver.browser.switch_to.alert.dismiss

within

特定の要素を中を検証/操作する

<div class="w2ui-button">

<div id="tasks">
  <div class="w2ui-button"></div> # こっちを操作したい
</div>
within find("#tasks") do
  find(".w2ui-button").click
end

新しいタブへ遷移する

handle = page.driver.browser.window_handles.last
page.driver.browser.switch_to.window(handle) do
end

タブを閉じる

閉じたあとページを移動する必要がある

page.driver.browser.close
page.driver.browser.switch_to.window(windows.first) ##これがないとエラーになる

参考資料

使えるRSpec入門・その4「どんなブラウザ操作も自由自在!逆引きCapybara大辞典」 - Qiita 【Rails】Selenium/RSpecでconfirmダイアログのテストをする - Qiita

リモートワーク時のスケジュール

リモートワークが開始となった場合に備えて、
1日のスケジュールを立てておく。

07:00 起床。朝の準備

08:00 散歩

08:30 朝ごはん

09:00 勉強

10:00 業務

12:00 昼休憩

13:00 業務

18:30 散歩。必要があれば買い物

19:00 晩ごはん

20:00 お風呂

21:00 勉強

23:00 就寝

どれだけ時間を有意義に使えるかが勝負。