user2811419
user2811419

Reputation: 1973

Writing a junit test to test the size of a set

I am writing a junit test to assert that the size of the set is not 0.

public interface Phone {

public Set<PhoneSample> getPhone();
public void setPhone<Set<PhoneSample> phone>
}

public class DefaultPhone implements Phone{
private Set<PhoneSample> phone;
@Override
   public Set<PhoneSample> getPhone() {
      return phone;
   }
}
   @Override
   public void setPhone(Set<PhoneSample> phone) {
      this.phone = phone;
   }
}
public class Test{
@Test
   public void testOrder10() throws Exception {
      Phone input = new DefaultPhone();

      int size = input.phone.getLength(); ///DOESNT WORK
      assertThat(size, is > 0);

   }
}

I eventually want to be able to get the size, and check that it is larger than zero.

My second question about this is how would I access the getPhone() method inside the set from the test class. And if I add in more methods to my set, how would I access each one of those methods from inside the set.

Upvotes: 0

Views: 2677

Answers (2)

Brian Agnew
Brian Agnew

Reputation: 272367

Rather than expose and test the members of your Phone class, you should ask the Phone interface directly e.g. implement a size() method such that you can test via:

int size = input.size();

Otherwise you're simply exposing the implementation of the Phone object. I would investigate and perhaps apply the Law Of Demeter here and get the Phone object to do the work for you as much as possible.

Upvotes: 0

Alfredo Osorio
Alfredo Osorio

Reputation: 11475

int size = input.phone.getLength(); ///DOESNT WORK

should be:

int size = input.getPhone().size();

Upvotes: 1

Related Questions