mercredi 29 avril 2015

Create a mock calling a constructor

Suppose I have the following class:

class Person {
  private String name;
  private Integer id;
  public Person(String name){
     this.name=name;
     this.id=random();
  }

  int random() {
     return new Random().nextInt();
  }
}

It's possible to create a partial mock for a Person class by calling the constructor with mocked random() method? I mean something like this:

Person a=EasyMock.createMockBuilder(Person.class)
                  .withConstructor(String.class)
                  .withArgs("Albina")
                  .addMockedMethod("random")
                  .createMock();

Aucun commentaire:

Enregistrer un commentaire