Announcement Announcement Module
Collapse
No announcement yet.
Spring Data MongoDB - indexes & junit tests Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data MongoDB - indexes & junit tests

    Hello,

    I have a small problem with indexes in my integration tests. Basically I defined a few indexes (1 simple and 1 compound), but it's not being created when I run my integration tests... They are there if I run my web app, but missing when I run integration tests.

    Does anyone know if it was designed this way? Basically I'd like to test a unique index constraint that it works, since my app has core and web components.

    To have a rough idea, this is what I have:

    Code:
    //AbstractIntegrationTest.groovy
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:test-core-context.xml")
    @ActiveProfiles("test")
    abstract class AbstractIntegrationTest {
    ...
    }
    Code:
    //MyServiceTest.groovy
    class MyServiceTest extends AbstractIntegrationTest {
        @Autowired
        MyService myService
        
        @Test
    	void test_unique_index() {
    	 /// some crud
    	 myService.save(new Something())
    	}
    }

    Code:
    //Something.java
    @Document
    @CompoundIndexes(value = { @CompoundIndex(name = "res_ag_idx", def = "{'resource': 1, 'agent': 1}", unique = true) })
    public class Something {
    	@Id
    	private ObjectId id;
    
    	@DBRef
    	private Resource resource;
    
    	@DBRef
    	private Agent agent;
    
    	@Indexed
    	private int status;
     ...
    }

    Code:
    //test-core-context.xml
    <beans ...>
    
    	<import resource="classpath:me/demo/my/core/config/core-context.xml" />
    	
    	<beans profile="test">
    		<bean id="settings" class="org.springframework.beans.factory.config.PropertiesFactoryBean" p:location="classpath:test.properties" />
    		<mongo:mongo host="${mongo.host.name}" port="${mongo.host.port}" write-concern="SAFE"/>
    		<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate" c:mongo-ref="mongo" c:databaseName="${mongo.db.name}" />
    	</beans>
    	
    </beans>
    Code:
    //core-context.xml
    <beans ...>
    	<context:annotation-config />
    
    	<context:component-scan base-package="me.demo.my">
    		<context:exclude-filter type="annotation" expression="org.springframework.context.annotation.Configuration" />
    	</context:component-scan>
    
    	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    	
    	<context:property-placeholder properties-ref="settings" />
    	
      	<mongo:repositories base-package="me.demo.my.repository" />	
    
    </beans>

    Thank you in advance for any input.

  • #2
    Ok, finally I figured it out...

    mongoTemplate.dropCollection() in my @Before annotated method will drop collection including all indexes, after which it won't re-initialize indexes... So, instead of dropping collection, I have to delete all data in order to preserve indexes.

    Basically

    mongoTemplate.remove(new Query(), Domain.class) instead of mongoTemplate.dropCollection(Domain.class)

    Thanks.

    Comment

    Working...
    X