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 7668999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:29:50+00:00 2026-05-31T15:29:50+00:00

To run all my tests in isolation I would like to drop and recreate

  • 0

To run all my tests in isolation I would like to drop and recreate a MongoDb collection every time a test method is called, reading the POJO annotations. The problem is that it seems the indexes are created only when MongoTemplate class is instantiated.

This is perfect for the “normal” application, but during the integration testing I would like to have a test like this (maybe too slow for real applications…):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = {
    ApplicationConfig.class, 
    MongoConfiguration.class,
    TestMongoConfiguration.class})
@ActiveProfiles("test")
public class BookServiceIntegrationTests {
    private @Autowired TestHelper testHelper;

    @Before
    public void startup() {
        testHelper.init(Book.class);
    }

    @After
    public void cleanup() {
        testHelper.drop(Book.class);
    }

    //test methods...
}

And this is my pretty straightforward POJO class:

@Document(collection = "books")
public class Book {
    @Id
    private ObjectId id;

    @Indexed(unique = true)
    private String isbn;

    private String author;
    private String title;
    private String genre;

    private List<String> tags;
    private List<Comment> comments;
}

Checking the sources for Spring Data for MongoDB (1.0.1.RELEASE) I saw that the class MongoPersistentEntityIndexCreator is reading the POJO annotations and ensuring the indexes for the colletion. This class is called only inside the MongoTemplate constructor.

Do you think I can find a better way to simulate something like a rollbacked transaction in my tests ?

Thanks,
Carlo

  • 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-05-31T15:29:51+00:00Added an answer on May 31, 2026 at 3:29 pm

    Edit because I can see you want to read the annotations, so what follows might be something you’ve already tried. If you can live with scripting the collection outside of Java, you can try this approach:

    Use mongodump and mongorestore using a system runtime exec from Java.

    Firstly, take a snapshot of your test collection:

    /usr/bin/mongodump -d yourDB -c books
    

    Then in the setup of your JUnit test, restore the collection, dropping the old one first with the –drop option.

    /usr/bin/mongorestore --drop -d yourDB -c books dump/yourDB 
    

    This should restore the indexes at the same time.

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

Sidebar

Related Questions

I have solution where I have some tests. I would like to run all
When I put a VerificationModeFactory.times(2) in test before, when I run all tests of
I need run all tests from my multimodule project using IDE. Like I do
I can run all tests in a single file with: rake test TEST=path/to/test_file.rb However,
From Eclipse I can easily run all the JUnit tests in my application. I
When running mvn test maven won't run all Test Classes. When I explicitly provide
I am trying to use nosetests to run all test cases inside my project.
With selenium can one run a test which check all images on a current
I want run all script from the directory . Like , The directory contains
I have some Tests that I run with ReSharpers Run All Tests from Solution

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.