Ceci est une continuation de Try DbUnit. Déplaçons Mockito et d'autres.
Officiel Mockito: http://site.mockito.org/
pom.xml
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
</dependency>
--Référence - 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 ? "Existe" : "n'existe pas");
}
}
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;
}
}
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);
}
}
J'ai réussi à utiliser Mockito. Mock Ito-san. .. .. Je suis heureux que vous puissiez l'utiliser comme référence. Ensuite, j'essaierai Selenium. → Essayez Selenium
Recommended Posts