不确定您使用的是什么版本的 Jongo,但这似乎与当前的 master (1.3-SNAPSHOT) 不兼容。这是您的 pojo 的有效 JUnit 测试用例:
import java.util.ArrayList;
import java.util.Arrays;
import org.bson.types.ObjectId;
import org.jongo.MongoCollection;
import org.jongo.marshall.jackson.oid.MongoId;
import org.jongo.util.JongoEmbeddedRule;
import org.jongo.util.MongoEmbeddedRule;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
public class ArrayListTest {
public static @ClassRule MongoEmbeddedRule mongoRule = new MongoEmbeddedRule();
public @Rule JongoEmbeddedRule jongoRule = new JongoEmbeddedRule(mongoRule);
MongoCollection collection;
@Before
public void before() {
collection = jongoRule.getJongo().getCollection("members");
}
@Test
public void shouldPopulateArrayList() {
Member joe = member("Joe", "123456", "Joe@example.com", "Adam", "Ben", "Josh");
int count = collection.save(joe).getN();
assertThat("Joe was saved", count, equalTo(1));
Member joeInMongo = collection
.findOne("{_id: #}", joe._id)
.as(Member.class);
assertThat(joeInMongo.username, equalTo("Joe"));
assertThat(joeInMongo.password, equalTo("123456"));
assertThat(joeInMongo.email, equalTo("Joe@example.com"));
assertThat(joeInMongo.friends, equalTo(Arrays.asList("Adam", "Ben", "Josh")));
}
public static class Member {
@MongoId
String _id;
String username;
String password;
String email;
ArrayList<String> friends;
public Member() {
friends = new ArrayList<String>();
}
public Member(String username, String password, String email) {
this.username = username;
this.password = password;
this.email = email;
friends = new ArrayList<String>();
}
}
public static Member member( String username, String password, String email, String... friends ) {
Member member = new Member(username, password, email);
member._id = new ObjectId().toString();
for( String friend : friends ) {
member.friends.add(friend);
}
return member;
}
}