Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8155565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:42:22+00:00 2026-06-06T16:42:22+00:00

I have a following test @Test public void testPutDocuments() throws Exception { final DBObject

  • 0

I have a following test

    @Test
    public void testPutDocuments() throws Exception {
        final DBObject document01 = new DBObject();
        document01.put("uniqueId", "001");
        document01.put("rv", "values");
        document01.put("pv", "values");

        final DBObject document02 = new DBObject();
        document02.put("uniqueId", "002");
        document02.put("rv", "values");
        document02.put("pv", "values");

        final DBObject document03 = new DBObject();
        document03.put("uniqueId", "003");
        document03.put("rv", "values");
        document03.put("pv", "values");

        final List<DBObject> documents = new ArrayList<DBObject>();
        documents.add(document01);
        documents.add(document02);
        documents.add(document03);

        mongoRule.getMongoService().putDocuments(documents);
        assertEquals(3, mongoRule.getDatabase().getCollection("test").getCount());
    }

Where mongoRule is a rule which connects to database, provides MongoService(a wrapper written to mongo client)

MongoService related methods

public void putDocument(@Nonnull final DBObject document) {
    LOGGER.info("inserting document - " + document.get("uniqueId"));
    mongo.getDB(database).getCollectionFromFull(getCollectionName(document)).insert(document);
}

public void putDocuments(@Nonnull final List<DBObject> documents) {
    for (final DBObject document : documents) {
        putDocument(document);
    }
}

When I run this, I get

java.lang.AssertionError: 
Expected :3
Actual   :1  

Now, if I do this

  mongoRule.getMongoService().putDocuments(documents);
  Thread.sleep(1000); 
  assertEquals(3, mongoRule.getDatabase().getCollection("contract").getCount());

I see no error.

Question:
a.) Why sleeping a thread for a second helped in getting a right number. isn’t it a concurrency related issue? What if two threads are trying to put same documents where as document has to be unique.
b.) How can I fix this?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:42:23+00:00Added an answer on June 6, 2026 at 4:42 pm

    answered here – https://groups.google.com/forum/?fromgroups#!topic/mongodb-user/jyvMkDFVous

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following test - public void testStore() throws ItemNotStoredException { Boolean result
I have the following JUnit test: @Test public void testRunLocalhost() throws IOException, InterruptedException {
I have the following code: @Test public void springTest() throws SQLException{ //Connect to the
I have the following test: [Test] public void VerifyThat_WhenInitializingTheLoggingInterceptionFacility_TheLoggingInterceptorIsAdded() { var kernel = new
I have following code public class TEST { public static void main(String arg[]){ try
I have the following code: class Test { public static void main(String[] args) {
I have the following simple code abstract class A { public abstract void Test(Int32
I am new to junit testing and I have the following test - public
I have following test [Test] public void aaa() { CallContext.LogicalSetData(aa, 1); Action parallelMethod =
I have the following test: @Test(expected=ArithmeticException.class) public void divideByZero() { int n = 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.