user2092012
user2092012

Reputation: 15

Java Testing in Eclipse, writing methods

I'm having trouble writing test methods in Eclipse, I have created a JUnit Case class, selected the class and methods I'd like to test. Now I'm faced with a class with empty methods. I'm not actually sure what to in the methods. I've read you need to create a new instance of the class I want to test them call it's method. I've looked online but nothing is making sense. This is part of the class I want to test:

public class FilmSystem {


private String filmName;
private String lecture;
private String ageRating;
private String ticketCost;
private int noOfSeats;

public FilmSystem(String film, String lect, String age, String price, int seats){
    filmName = film;
    lecture = lect;
    ageRating = age;
    ticketCost = price;
    noOfSeats = seats;


}

public String getFilmName(){
    return filmName;
}

How would I test the getFilmName method? Thanks.

Upvotes: 0

Views: 98

Answers (4)

Tiago Almeida
Tiago Almeida

Reputation: 137

Unit Testing philosophy is AAA: Arrange, Act and Assert

You probably won't need tests for a class like that one, but here is the general idea.

public class TestFilmSystem {

    @Test
    public void test_get_film_name(){
        /*
         * Arrange
         */         
        String expectedFilmName = "Hobbit- An unexpected journey";
        FilmSystem fs = new FilmSystem(expectedFilmName, "xpto", "12", "12.99€", 100);      
        /*
         * Act
         */

        String resultFilmName = fs.getFilmName();           
        /*
         * Assert
         */         
        Assert.assertEquals(expectedFilmName,resultFilmName);           
    }
}

Upvotes: 1

Makoto
Makoto

Reputation: 106528

Your unit test should not differ from the canonical use of your object in that context. Since FilmSystem is immutable (that is, none of those fields have setters), you will have to assert that what you instantiate your object with is what your object returns.

@Test
public void testFilmName() {
    // arrange
    FilmSystem fs = new FilmSystem("Some movie", "", "", "", 10);
    // act
    // no actions
    // assert
    assertEquals("Some movie", fs.getFilmName());
}

Upvotes: 1

Rodrigo Sasaki
Rodrigo Sasaki

Reputation: 7236

Quick example:

public class FilmSystem{

    private String filmName;
    private String lecture;
    private String ageRating;
    private String ticketCost;
    private int noOfSeats;

    public FilmSystem(String film, String lect, String age, 
                      String price, int seats){
        filmName = film;
        lecture = lect;
        ageRating = age;
        ticketCost = price;
        noOfSeats = seats;

    }

    public String getFilmName(){
        return filmName;
        }

}

public class FilmSystemTest{

    @Test
    public void shouldReturnCorrectName(){
        FilmSystem filmSystem = new FilmSystem("Ocean's Eleven", "", "", "", 0);

        Assert.assertEquals("Ocean's Eleven", filmSystem.getFilmName());
    }
}

Although I have to say. Testing a getter doesn't seem quite so interesting

Upvotes: 2

Lee Meador
Lee Meador

Reputation: 12985

You will need to fill in the rest (or let Eclipse do it). I'm just being lazy.

private static final String FILM = "Casa Blanca";
.. similar for all ...

@Test
public void testFilmName() {

  FilmSystem f = new FilmSystem(FILM, LECTURE, AGE_RATING, PRICE, SEAT_COUNT);
  assertEquals("File name is wrong", FILM, f.getFilmName());

}

Upvotes: 0

Related Questions