Mockitoをやってみる

## はじめに
[DbUnitをやってみる](https://qiita.com/hirotatsuuu/items/7413ac25d0244a89a792)の続きです。
Mockitoとやらを動かしてみます。

Mockito公式: http://site.mockito.org/

## Mavenの設定ファイルの修正
- pom.xmlに依存ライブラリを追記

```pom.xml
<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-all</artifactId>
  <version>1.9.5</version>
</dependency>
```
- 参考
    - https://search.maven.org/#artifactdetails|org.mockito|mockito-all|1.9.5|jar

## サンプルコードの作成
```FruitsMain.java
package com.example;

import java.util.List;

import com.example.dao.FruitsDao;
import com.example.service.FruitsService;

public class FruitsMain {
	public static void main(String[] args) {
	        FruitsService fruitsService = new FruitsService();
		fruitsService.setFruitsDao(new FruitsDao());
		boolean flag = fruitsService.isExist(1);
		System.out.println(flag ? "存在します" : "存在しません");
	}

}
```
```FruitsService.java
package com.example.service;

import com.example.dao.FruitsDao;
import com.example.dto.FruitsDto;

public class FruitsService {

	private FruitsDao fruitsDao;

	public void setFruitsDao (FruitsDao fruitsDao) {
		this.fruitsDao = fruitsDao;
	}

	public boolean isExist (int id) {
		FruitsDto fruitsDto = this.fruitsDao.selectById(id);
		System.out.println(fruitsDto);
		return fruitsDto != null;
	}

}
```
```FruitsDao.java
package com.example.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.example.dto.FruitsDto;

public class FruitsDao {

	private static final String DB_URL = "jdbc:mysql://localhost/maven_sample?useSSL=false";
	private static final String DB_USER = "root";
	private static final String DB_PASSWORD = "admin";

	public FruitsDto selectById (int id) {
		String sql = "select * from fruits where id = ?";
		try (
			Connection con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
			PreparedStatement ps = con.prepareStatement(sql)
		) {
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			FruitsDto fruitsDto = null;
			while (rs.next()) {
				fruitsDto = new FruitsDto();
				fruitsDto.setId(rs.getInt("id"));
				fruitsDto.setName(rs.getString("name"));
				fruitsDto.setPrice(rs.getInt("price"));
			}
			return fruitsDto;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

}
```
```FruitsDto.java
package com.example.dto;

public class FruitsDto {

	private int id;
	private String name;
	private int price;

	public FruitsDto() {

	}

	public FruitsDto (int id, String name, int price) {
		this.id = id;
		this.name = name;
		this.price = price;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
}
```

## Mockitoによるテストコードの作成
```FruitsServiceTest.java
package com.example.service;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

import org.junit.Test;

import com.example.dao.FruitsDao;
import com.example.dto.FruitsDto;

public class FruitsServiceTest {

	@Test
	public void test() {
		FruitsDao mockDao = mock(FruitsDao.class);
		when(mockDao.selectById(anyInt())).thenReturn(new FruitsDto(1, "apple", 100));
		FruitsService sut = new FruitsService();
		sut.setFruitsDao(mockDao);
		boolean actual = sut.isExist(1);
		assertThat(actual, is(true));
		verify(mockDao).selectById(1);
	}

}
```

## おわりに
なんとかMockitoを使えました。
モック伊藤さん。。。
参考になれば幸いです。
次はSeleniumをやってみようと思います。
→ [Seleniumをやってみる](https://qiita.com/hirotatsuuu/items/6a87df10c635a37ade16)