I have this method that I want to test
private Connection con;
private Statement set;
private ResultSet rs;
public void actualizarJugador(String nombre) {
try {
set = con.createStatement();
set.executeUpdate("UPDATE Jugadores SET votos=votos+1 WHERE nombre " + " LIKE '%" + nombre + "%'");
rs.close();
set.close();
} catch (Exception e) {
// No modifica la tabla
System.out.println("No modifica la tabla");
System.out.println("El error es: " + e.getMessage());
}
}
How can I do it? How can I test a method that does not return anything?
Thanks
the method has a side effect, it changes the calue of votos
under a certain condition. To test that, you need to query your database and check that the value was actually updated. You can also check that values not fulfilling the where-condition are not updated. To do that you should query the database before and after you call your method, and compare the values with each other.
I have this example code
import java.sql.*;
public class ModeloDatos {
private Connection con;
private Statement set;
private ResultSet rs;
public void abrirConexion() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// Con variables de entorno
String dbHost = System.getenv().get("DATABASE_HOST");
String dbPort = System.getenv().get("DATABASE_PORT");
String dbName = System.getenv().get("DATABASE_NAME");
String dbUser = System.getenv().get("DATABASE_USER");
String dbPass = System.getenv().get("DATABASE_PASS");
String url = dbHost + ":" + dbPort + "/" + dbName;
con = DriverManager.getConnection(url, dbUser, dbPass);
} catch (Exception e) {
// No se ha conectado
System.out.println("No se ha podido conectar");
System.out.println("El error es: " + e.getMessage());
}
}
public boolean existeJugador(String nombre) {
boolean existe = false;
String cad;
try {
set = con.createStatement();
rs = set.executeQuery("SELECT * FROM Jugadores");
while (rs.next()) {
cad = rs.getString("Nombre");
cad = cad.trim();
if (cad.compareTo(nombre.trim()) == 0) {
existe = true;
}
}
rs.close();
set.close();
} catch (Exception e) {
// No lee de la tabla
System.out.println("No lee de la tabla");
System.out.println("El error es: " + e.getMessage());
}
return (existe);
}
public void actualizarJugador(String nombre) {
try {
set = con.createStatement();
set.executeUpdate("UPDATE Jugadores SET votos=votos+1 WHERE nombre " + " LIKE '%" + nombre + "%'");
rs.close();
set.close();
} catch (Exception e) {
// No modifica la tabla
System.out.println("No modifica la tabla");
System.out.println("El error es: " + e.getMessage());
}
}
public void insertarJugador(String nombre) {
try {
set = con.createStatement();
set.executeUpdate("INSERT INTO Jugadores " + " (nombre,votos) VALUES ('" + nombre + "',1)");
rs.close();
set.close();
} catch (Exception e) {
// No inserta en la tabla
System.out.println("No inserta en la tabla");
System.out.println("El error es: " + e.getMessage());
}
}
public void ponerVotosCero() {
try {
set = con.createStatement();
set.executeUpdate("UPDATE Jugadores SET votos=0 where votos>0");
rs.close();
set.close();
} catch (Exception e) {
// No modifica la tabla
System.out.println("Poner votos a cero. No modifica la tabla");
System.out.println("El error es: " + e.getMessage());
}
}
public void cerrarConexion() {
try {
con.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
With this test, it should be something similar
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class ModeloDatosTest {
@Test
public void testExisteJugador() {
System.out.println("Prueba de existeJugador");
String nombre = "";
ModeloDatos instance = new ModeloDatos();
boolean expResult = false;
boolean result = instance.existeJugador(nombre);
assertEquals(expResult, result);
//fail("Fallo forzado.");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.