Chapter 2 Comments add/update test question

Hi,
I think I’m confused about what the test is trying to achieve. I got one test to pass by explicitly throwing an exception if the user passes a Comment object without an ID field, but that appears to be a flaw to me. More importantly this test:
@Test
public void testAddCommentUsingObjectId() {
String id = “5a9427648b0beebeb69579cc”;
Comment comment = dao.getComment(id);

    Assert.assertNotNull(comment);
  }

Can someone explain how we should have been able to get a result from this call? I checked and the ID 5a9427648b0beebeb69579cc doesn’t exist in my comments collection (or is this supposed to be a movie ID?) . Is this supposed to be an existing comment in the database that was pre-built (I’m pretty sure I performed no deletes on the database - there are 41,101 comments in my sample database). As I understand it, until I insert a comment, and get back a UID, that comment cannot exist, so the above test should fail.

Now that brings me to the very first test that runs, mainly the one that tests that the comment has an ID field being set, mainly this:
@Test(expected = IncorrectDaoOperation.class)
public void testUserAddCommentWithNoID() {
Comment actual = fakeCommentNoId();
dao.addComment(actual);
}
Am I missing something or why would a net new comment have a UID already assigned? Isn’t the point that we’re letting mongoDB assign us a UID so we don’t have to keep track of it?
Everything else I think I can eventually solve, but I need to understand the intent behind the use case a bit better. This may be an example of where the valid question is “who tests the tester”?
-Hugh

1 Like

Somebody is trying to hack your API. There is a bug somewhere else in the system. You just want to make your API as secure and robust as possible. Application specific exception classes are usually there to encapsulate and hide implementation details of the sub-components used. In this case something like https://mongodb.github.io/mongo-java-driver/3.6/javadoc/com/mongodb/client/MongoCollection.html#insertOne-TDocument-

As it is customary when writing unit tests, a comment or movie or both with well known (to the test writer) values are added to the database as we want them to be. And then, the test uses the API to verify that it behaves like it should be.

I’m still not quite understanding something - the documentation you quoted confirms that the _id field that gets set when you create a (in this case) new comment, is in fact either an autogenerated UID, or whatever you insert into the ID field.

So the implication is that in the mflix database, the UI should be generating the UIDs for all comments. That somehow doesn’t feel right. Unless something in a later chapter will make this all make sense.

To the second thing about the test on that nonexistent ID, I mentioned this in my other post, but the problem I have is that the test is to make sure that adding a comment works. The actual test class does a dao.getComment() with the ID as the argument. If this were a test on the getComment() function, then your explanation would make more sense.
-Hugh

I am not sure I am following you so I hope someone with a better understanding will pickup the discussion.

To whomever picks up this question next, here are the salient points:

  1. Despite the issues I encountered, I ultimately was able to get the test code I needed to complete the lab, so this is not me being stuck, but I found two separate issues related to the Maven test script we ran as part of our unit testing.
    a) I found it strange that there was a test that the comment being added for the first time required a UID to be entered. Wouldn’t a better design be to allow the database to provide the unique ID (i.e. allow mongo to set _id rather than the client app layer)? I could make this test pass, but philosophically this feels wrong. If you were looking to protect your API, you’re better off with a secure token (i.e. JWT, Oauth2, SAML, etc) than relying on the client to send a UID;
    b) This is probably a more important point: I believe the test to verify that you can add a comment is flawed. Instead of a test to verify that when you create a comment object and call the dao.addComment() method, you have this:
    public void testAddCommentUsingObjectId() {
    String id = "5a9427648b0beebeb69579cc";
    Comment comment = dao.getComment(id);

         Assert.assertNotNull(comment);
       }
    

The above does NOT test whether you can add a net new comment, but rather tests whether you can fetch a pre-existing comment. The comment _id field supplied, doesn’t exist in the default mflix database - I checked.

So, in summary, point a) is philosophical but shouldn’t cause problems passing the test, this is a case of the developer following the directives of the Architect. b) should probably be addressed as it will likely cause issues for others.

-Hugh

2 Likes

I’m having this same issue, although the id string is different for me in the test file. Still, there is no comment in my database filled with sample data that matches that specific id. Nonetheless, I was able to make sure that all other tests worked as expected and get the validation code even without this specific test working.

Lastly, this test is concretely incorrect in my opinion…

I also have the same issue, but my test case the id value is “619e483309f8c99feb5c87a2”, and the code is :

  @Test
  public void testAddCommentUsingObjectId() {
    String id = "619e483309f8c99feb5c87a2";
    Comment comment = dao.getComment(id);

    Assert.assertNotNull(comment);
  }

From the test, we can clearly see that it invoked a “get” method, which in the end invoked the following code:

commentCollection.find(new Document("_id", obj_id)).first();

But the test case name is “testAddCommentUsingObjectId” , but the method code has nothing to do with any insert and update or replace operations. which does not make any sense.