0
0

More than 1 year has passed since last update.

役立つlombok入門(java)

Last updated at Posted at 2022-08-12

駆け出しITエンジニアあ~さんです。
lombokの機能に関して疑問持つ方が多いという認識から、サンプルを以下に掲載いたします。

論より証拠!!
<題材のソースコード掲載>

AccountBean.java
package com.lombok.sample.model;

import lombok.AllArgsConstructor;
import lombok.Data;

/** アカウントモデル */
@Data
@AllArgsConstructor
public class AccountBean {

	/** ID */
	private String id;

	/** 名前 */
	private String name;
}

@Data → getter&setterを自動生成
@AllArgsConstructor → インスタンスに関するコンストラクター生成
※詳しくはググってね☆

<以上のソースコードがコンパイル時に自動生成される内容>

AccountBean.class
package com.lombok.sample.model;

public class AccountBean {
  private String id;
  
  private String name;
  
  public String getId() {
    return this.id;
  }
  
  public String getName() {
    return this.name;
  }
  
  public void setId(String id) {
    this.id = id;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
  public boolean equals(Object o) {
    if (o == this)
      return true; 
    if (!(o instanceof AccountBean))
      return false; 
    AccountBean other = (AccountBean)o;
    if (!other.canEqual(this))
      return false; 
    Object this$id = getId(), other$id = other.getId();
    if ((this$id == null) ? (other$id != null) : !this$id.equals(other$id))
      return false; 
    Object this$name = getName(), other$name = other.getName();
    return !((this$name == null) ? (other$name != null) : !this$name.equals(other$name));
  }
  
  protected boolean canEqual(Object other) {
    return other instanceof AccountBean;
  }
  
  public int hashCode() {
    int PRIME = 59;
    result = 1;
    Object $id = getId();
    result = result * 59 + (($id == null) ? 43 : $id.hashCode());
    Object $name = getName();
    return result * 59 + (($name == null) ? 43 : $name.hashCode());
  }
  
  public String toString() {
    return "AccountBean(id=" + getId() + ", name=" + getName() + ")";
  }
  
  public AccountBean(String id, String name) {
    this.id = id;
    this.name = name;
  }
}

lombokが動いているか怪しいからテストコードも書いてみたよ(^▽^)/

AccountBeanTest.java
package com.lombok.sample.model;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class AccountBeanTest {

	@Test
	@DisplayName("インスタンス化で初期化した値の取得テスト_Id取得")
	void testAccountBean_001() {

		// テストデータ
		AccountBean AccountBean = new AccountBean("001", "test1");

		// 期待値
		String expected = "001";

		// 実効値
		String actual = AccountBean.getId();

		// 検証
		assertEquals(expected, actual);
	}

	@Test
	@DisplayName("インスタンス化で初期化した値の取得テスト_Name取得")
	void testAccountBean_002() {

		// テストデータ
		AccountBean AccountBean = new AccountBean("002", "test2");

		// 期待値
		String expected = "test2";

		// 実効値
		String actual = AccountBean.getName();

		// 検証
		assertEquals(expected, actual);
	}
}

●参考サイト
『【Java】デコンパイルを試してみよう!classファイルからJavaコードを確認する方法』
https://style.potepan.com/articles/34787.html#Java-2
『SpringBootのプロジェクトにlombok導入する』
https://qiita.com/kihara-takahiro/items/f616d01be6bf7384dbfc
『Lombok 使い方メモ』
https://qiita.com/opengl-8080/items/671ffd4bf84fe5e32557
『Lombokが便利だなと感じたので書いてみた』
https://qiita.com/supreme0110/items/391505da8f4321736421
『JUnit 5 + Gradle による Java の自動テスト導入』
https://qiita.com/niwasawa/items/cfcd37a3c2a795c336ba

●動作確認用の公開ソースコード
https://github.com/sunn-sudo/spring-lombok-sample

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