[JAVA] Try Mockito

Introduction

This is a continuation of Try DbUnit. Let's move Mockito and others.

Mockito Official: http://site.mockito.org/

Modify Maven config file

--Added dependent library to pom.xml

pom.xml


<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-all</artifactId>
  <version>1.9.5</version>
</dependency>

--Reference - https://search.maven.org/#artifactdetails|org.mockito|mockito-all|1.9.5|jar

Creating sample code

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 ? "Exists" : "do not exist");
	}

}

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;
	}
}

Creating test code with 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);
	}

}

in conclusion

I managed to use Mockito. Mock Ito-san. .. .. I'm glad if you can use it as a reference. Next, I will try Selenium. → Try Selenium

Recommended Posts

Try Mockito
Try HiveRunner
Try Selenium
Try DbUnit
try docker-compose
Try Lombok
Try App Clips
Try using libGDX
First try ~ catch
Changes in Mockito 2
Try using Maven
Try Java 8 Stream
Try using sql-migrate
Try using SwiftLint
Try using Log4j 2.0
Org.mockito.exceptions.misusing.InvalidUseOfMatchersException in Mockito
Try Ruby Minitest
Roughly try Java 9