如何重新构建返回客户列表的方法?

时间:2018-08-12 10:41:54

标签: java arraylist mockito

我需要重新构建一些方法以返回客户端列​​表。我需要这样做,因为那一刻我无法编写任何单元测试。 这是我负责将客户端添加到数据库的方法。如您所见,从已创建的客户端对象中获取值:name,surname...。 CarRentalSQLDatabase

@Override
public void addClient(Client client) throws SQLException {
    preparedStatement = connection.prepareStatement("insert into client" + "(namee, surname, street,houseNumber,city,peselNumber,rentDate, clientNumber)" + "values(?,?,?,?,?,?,?,?)");

    preparedStatement.setString(1, client.getName());
    preparedStatement.setString(2, client.getSurname());
    preparedStatement.setString(3, client.getStreet());
    preparedStatement.setInt(4, client.getHouseNumber());
    preparedStatement.setString(5, client.getCity());
    preparedStatement.setLong(6, client.getPeselNumber());
    preparedStatement.setString(7, client.getRentDate());
    preparedStatement.setInt(8, client.getClientNumber());

    preparedStatement.executeUpdate();
}

这是我的方法,负责在屏幕上显示所有客户端: CarRentalSQLDatabase

@Override
public void populateTableViewClients() throws SQLException {
    String sql = "SELECT * FROM `client`";
    result = statement.executeQuery(sql);

    while (result.next()) {

        String namee = result.getString("namee");
        String surname = result.getString("surname");
        String street = result.getString("street");
        int houseNumber = result.getInt("houseNumber");
        long peselNumber = result.getLong("peselNumber");
        String rentDate = result.getString("rentDate");
        System.out.println("----------------------------");
        System.out.printf("Name:" + namee + "\nSurname:" + surname + "\nStreet:" + street + "\nNumber of house:" + houseNumber + "\nPesel number:" + peselNumber + "\nDate of rental:" + rentDate + "\n");
        System.out.println("----------------------------");
    }
}

我想构建这样的东西:

public List<Client> getAllCustomers() throws SQLException {
    List<Client> listOfClients = new ArrayList<Client>();

    String sql = "SELECT * FROM `client`";
    result = statement.executeQuery(sql);
    //....
    return listOfClients;
}

那么如何从我的数据库中检索数据并将其放入ArrayList? 进行测试所需的所有步骤如下:

@RunWith(MockitoJUnitRunner.class)
class CarRentalOptionsTest{
CarRentalOptions objUnderTests;

@Test
public void addedCustomerShouldBeSaved(){

    CarRentalStorage storageMock = mock(CarRentalStorage.class);

    objUnderTests = new CarRentalOptions(storageMock);

    Client client = new Client();
    objUnderTests.addClient(client);

    verify(storageMock).addCustomer(client);


    List<Customer> customers = new ArrayList<>();
    customers.add(client);
    when(storageMock.getAllCustomers()).thenReturn(customers);

    Assert.assertTrue(objUnderTests.isCustomerRegistered(customer));
}

1 个答案:

答案 0 :(得分:0)

int main(){
    std::string* pointer = (std::string*) malloc(4 * sizeof(std::string));

    for(int i = 0; i < 4; i++){
        pointer[i] = "test";
    }

    std::cout << "Success" << std::endl;

    return 0;
}