Spring Data MongoDB - indexes & junit tests

1,940 views
Skip to first unread message

Octavian Covalschi

unread,
Dec 26, 2012, 1:18:36 PM12/26/12
to mongod...@googlegroups.com
Hello,

Is anyone around here that's using spring data mongodb? 

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:

//AbstractIntegrationTest.groovy
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:test-core-context.xml")
@ActiveProfiles("test")
abstract class AbstractIntegrationTest {
...
}

//MyServiceTest.groovy
class MyServiceTest extends AbstractIntegrationTest {
    @Autowired
    MyService myService
    
    @Test
void test_unique_index() {
/// some crud
myService.save(new Something())
}
}


//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;
 ...
}



//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>

//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.

Tobias Trelle

unread,
Dec 28, 2012, 8:15:04 AM12/28/12
to mongod...@googlegroups.com
I suppose it's probably a problem with Spring Data Mongo and not with the Java driver.

Did you already ask in the Spring forum: http://forum.springsource.org/forumdisplay.php?80-NoSQL ?

HTH
Tobias

Octavian Covalschi

unread,
Dec 28, 2012, 10:55:36 AM12/28/12
to mongod...@googlegroups.com
Yes, I think so too. I was hoping to find someone here that's using spring data :)

And yes, I have created a new thread there too.. no replies so far... perhaps it's due holidays...

Thanks.



Tobias

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Tobias Trelle

unread,
Dec 28, 2012, 11:36:29 AM12/28/12
to mongod...@googlegroups.com
Hi oct,

I'm hosting a github repo with examples for Spring Data, including SD MongoDB: https://github.com/ttrelle/spring-data-examples

I just checked if my unit tests create an index - they do indeed. Have a look at

Domain class:
https://github.com/ttrelle/spring-data-examples/blob/master/springdata-mongodb/src/main/java/mongodb/User.java

Unit-Test:
https://github.com/ttrelle/spring-data-examples/blob/master/springdata-mongodb/src/test/java/mongodb/MongoDBRepoTest.java

HTH
Tobias

Octavian Covalschi

unread,
Dec 28, 2012, 1:19:42 PM12/28/12
to mongod...@googlegroups.com
Thank you, I'll check them out.

Octavian Covalschi

unread,
Dec 28, 2012, 2:08:54 PM12/28/12
to mongod...@googlegroups.com
So, I ran MongoDBRepoTest and it doesn't create any other indexes except _id.

After running "mvn -Dtest=MongoDBRepoTest test" all I get is one index, but I expect to see fullName too.

> db.user.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "test.user",
"name" : "_id_"
}
]

Do you see fullName index created too?

Thanks.

Octavian Covalschi

unread,
Dec 28, 2012, 3:11:50 PM12/28/12
to mongod...@googlegroups.com
In case if anyone is interested, but looks like indexes are getting created, it's just something removes them at the end... I tried to debug test case and during the test indexes are there... but once test is completed, only _id index is left...

Thanks.

Octavian Covalschi

unread,
Dec 28, 2012, 3:49:06 PM12/28/12
to mongod...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages