简体   繁体   中英

How should I define a collection method that returns an array?

I would like to know what I should do to make the array list be returned properly. Currently I am getting this error on each line of the testing method where get(o), get(1) etc: cannot find symbol symbol: method get(int) location: interface Collection

see here: error image

I have also tried other methods such as assertThat which did not work.

I have a testing method that looks like this:

 @Test
    public void testSaveCar() {
        dao.saveCar(car3);
    
        assertEquals(dao.getCars().get(0), car1.getCarId());
        assertEquals(dao.getCars().get(1).getCarId(), car2.getCarId());
        assertEquals(dao.getCars().get(2).getCarId(), car3.getCarId());
        dao.removeCar(car3);
    
    } 

and a jdbcdao class that looks like this:

/**
 *
 *
 */
public class CarJdbcDAO implements CarDAO {

    private String url = JdbcConnection.getDefaultConnectionUri();

    public CarJdbcDAO() {
    }

    public CarJdbcDAO(String uri) {
        this.url = uri;
    }

   

    @Override
    public Collection<Car> getCars() {
        

        String sql = "select * from Car";
        try (
            Connection dbCon = JdbcConnection.getConnection(url); //get connection to db
            PreparedStatement stmt = dbCon.prepareStatement(sql); //create stmt
            ) {
            ResultSet rs = stmt.executeQuery(); 

            List<Car> carsList = new ArrayList<>(); 

            //iterate through query results
            while (rs.next()) {
                            

                
                    String carId = rs.getString("Car_Id");
                    String carName = rs.getString("Car_Name");
                    
                    String carType = rs.getString("Car_Type");
                    String seatNumber = rs.getString("Seat_Number");
                    BigDecimal hourlyCharge = rs.getBigDecimal("Hourly_Charge");
                    String location = rs.getString("Location");
                                                
                                                Car car = new Car(carId, carName, carType, seatNumber, hourlyCharge, location);
                                                

                carsList.add(car); 

            }

            return carsList;

        } catch (SQLException ex) {
            throw new DAOException(ex.getMessage(), ex);
        }
    }

 

getCars returns a Collection which is not the same as a list. Specifically it does not allow you to get an item using an index.

The better way to assert that a collection contains certain items is to use:

assertThat(dao.getCars(), hasItems(car1, car2));

This is using the Hamcrest assertion library which is the one that is normally used with junit. You could use an alternative assertion library such as AssertJ that would allow:

assertThat(dao.getCars()).containsExactly(car1, car2);

AssertJ also lets you extract fields if you don't have equals defined:

assertThat(dao.getCars()).extracting("id").contains(id1, id2);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM