2

I have the following problem: I have a spring-boot (1.3.3) application that uses a mongodb as storage. All works fine with a real mongodb using the mongo repositories. But for unit tests we try to use fongo to have not install a mongodb on every server. Most parts of the tests work also fine with fongo, but when i load an object form the database(fongo) the field with the id is not set. Has anyone else experienced a similar? Thanks in an advance for all your help!

Document:

@Document
public class SystemEvent {

    @Id
    private String id;

    private String oid;

    private String description;

    private String type;

    private String severtity;

    public SystemEvent(){
    }

    // getter/setter

}

Repository:

@Repository
public interface SystemEventRepository extends MongoRepository<SystemEvent, String> {

}

Test:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MongoFongoApplication.class)
public class MongoFongoApplicationTests {

    @Test
    public void contextLoads() {
    }

    @Autowired
    SystemEventRepository systemEventRepository;

    @Test
    public void testRepo() {
        SystemEvent info1 = systemEventRepository.save(new SystemEvent("DESC 1", "TYPE 1", "INFO"));
        SystemEvent info2 = systemEventRepository.save(new SystemEvent("DESC 2", "TYPE 2", "INFO"));
        List<SystemEvent> all = systemEventRepository.findAll();

        assertThat(all.size(), is(2)); // WORKS FINE

        // -----

        SystemEvent systemEvent = systemEventRepository.findOne(info1.getId());

        assertThat(systemEvent, notNullValue());  // WORKS FINE
        assertThat(systemEvent.getId(), notNullValue()); // FAILS
    }

    @Configuration
    public static class TestConfig extends AbstractMongoConfiguration {
        @Override
        protected String getDatabaseName() {
            return "test";
        }

        @Override
        public Mongo mongo() throws Exception {
            return new Fongo(getDatabaseName()).getMongo();
        }
    }

}
R. Illger
  • 21
  • 2
  • Also, I see that you use `assertThat(systemEvent.getId(), notNullValue());` Can you try using your `.setId(int/long ) value` on that line.. I think that your test case is expecting a value – ryekayo May 03 '16 at 14:29
  • 1
    Yes I except the field id to be set. But i except that the id is set when the object is loaded form the database. With a real mongo this works (also this test case). But it fails with the fongo. – R. Illger May 04 '16 at 06:56

1 Answers1

0

Try adding these for your Id field in Document model class. This should fix you problem.

@Id
@Field(value = GdnBaseMongoEntity.ID)
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
private String id;

For GeneratedValue you will need to add dependency for javax persistence API. you can add this in ur pom.xml file

<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.0.Final</version>
</dependency>

For build.gradle

compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.0-api', version: '1.0.0.Final'
lrathod
  • 1,094
  • 1
  • 9
  • 17
  • Quite interesting your answer, but sorry I cannot find the annotation packages (MongoId, GeneratedValue...)... – Stefano Scarpanti Jul 31 '18 at 14:55
  • Sorry my bad. you don't need (Have removed it from answer). For *GeneratedValue* you will need to add dependency for *javax persistence API*. Have updated my answer. Can you try that ? – lrathod Aug 07 '18 at 14:09