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

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.

主站蜘蛛池模板: 田东县| 高平市| 金秀| 盘锦市| 柳河县| 嘉禾县| 平阴县| 普兰店市| 白山市| 远安县| 泊头市| 昌江| 进贤县| 赤壁市| 土默特左旗| 漾濞| 泾源县| 嘉义市| 丰宁| 内黄县| 沭阳县| 武功县| 平阳县| 阿拉尔市| 舒城县| 托克逊县| 青神县| 铁岭县| 历史| 新邵县| 乐昌市| 喀喇| 乌兰县| 手游| 拉孜县| 聊城市| 怀化市| 曲阳县| 泸州市| 乃东县| 无极县|