官术网_书友最值得收藏!

Mocking frameworks

Our project looks cool, but it's too simple and it is far from being a real project. It still doesn't use external resources. A database is required by Java projects so we'll try to introduce it, as well.

What is the common way to test code that uses external resources or third-party libraries? Mocks are the answer. A mock object, or simply a mock, is a simulated object that can be used to replace real ones. They are very useful when objects that depend on external resources are deprived of them.

In fact, you don't need a database at all while you are developing the application. Instead, you can use mocks to speed up development and testing and use a real database connection only at runtime. Instead of spending time setting up a database and preparing test data, we can focus on writing classes and think about them later on during integration time.

For demonstration purposes, we'll introduce two new classes: the Person class and the FriendCollection class that are designed to represent persons and database object mapping. Persistence will be done with MongoDB (https://www.mongodb.org/).

Our sample will have two classes. Person will represent database object data; FriendCollection will be our data access layer. The code is, hopefully, self-explanatory.

Let's create and use the Person class:

public class Person { 
  @Id
  private String name; 
 
  private List<String> friends; 
 
  public Person() { } 
 
  public Person(String name) { 
    this.name = name; 
    friends = new ArrayList<>(); 
  } 
 
  public List<String> getFriends() { 
    return friends; 
  } 
 
  public void addFriend(String friend) { 
    if (!friends.contains(friend)) friends.add(friend); 
  }
}

Let's create and use the FriendsCollection class:

public class FriendsCollection { 
  private MongoCollection friends; 
 
  public FriendsCollection() { 
    try { 
      DB db = new MongoClient().getDB("friendships"); 
      friends = new Jongo(db).getCollection("friends"); 
    } catch (UnknownHostException e) { 
      throw new RuntimeException(e.getMessage()); 
    } 
  } 
 
  public Person findByName(String name) { 
    return friends.findOne("{_id: #}", name).as(Person.class); 
  } 
 
  public void save(Person p) { 
    friends.save(p); 
  } 
} 

In addition, some new dependencies have been introduced so the Gradle dependencies block needs to be modified, as well. The first one is the MongoDB driver, which is required to connect to the database. The second is Jongo, a small project that makes accessing Mongo collections pretty straightforward.

The Gradle dependencies for mongodb and jongo are as follows:

dependencies { 
    compile 'org.mongodb:mongo-java-driver:2.13.2' 
    compile 'org.jongo:jongo:1.1' 
} 

We are using a database so the Friendships class should also be modified. We should change a map to FriendsCollection and modify the rest of the code to use it. The end result is the following:

public class FriendshipsMongo { 
  private FriendsCollection friends; 
 
  public FriendshipsMongo() { 
    friends = new FriendsCollection(); 
  } 
 
  public List<String> getFriendsList(String person) { 
    Person p = friends.findByName(person); 
    if (p == null) return Collections.emptyList(); 
    return p.getFriends(); 
  } 
 
  public void makeFriends(String person1, String person2) { 
    addFriend(person1, person2); 
    addFriend(person2, person1); 
  } 
 
  public boolean areFriends(String person1, String person2) { 
    Person p = friends.findByName(person1); 
    return p != null && p.getFriends().contains(person2); 
  } 
 
  private void addFriend(String person, String friend) {
Person p = friends.findByName(person); if (p == null) p = new Person(person); p.addFriend(friend); friends.save(p); } }

The complete source code can be found in the FriendsCollection and FriendshipsMongo classes in the https://bitbucket.org/vfarcic/tdd-java-ch02-example-junit repository.

Now that we have our Friendships class working with MongoDB, let's take a look at one possible way to test it by using mocks.

主站蜘蛛池模板: 道孚县| 宁安市| 安徽省| 惠水县| 崇礼县| 紫云| 合水县| 望奎县| 新沂市| 大余县| 阿拉善盟| 正蓝旗| 句容市| 宁化县| 阳江市| 彰武县| 黎平县| 阜城县| 武川县| 麟游县| 东山县| 珲春市| 象州县| 宁晋县| 突泉县| 建瓯市| 桦川县| 博客| 邵阳县| 灵武市| 上虞市| 南川市| 科技| 鄂托克前旗| 青川县| 南开区| 甘孜县| 宁远县| 上蔡县| 绍兴市| 车致|