Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm using this custom converters into my Spring Boot service:

@Configuration
public class MongoConfig {

        @Bean
        public MongoCustomConversions customConversions(){
            List<Converter<?,?>> converters = new ArrayList<>();
            converters.add(ReferenceWriterConverter.INSTANCE);
            return new MongoCustomConversions(converters);
        }

        @WritingConverter
        enum ReferenceWriterConverter implements Converter<Reference, DBObject> {

            INSTANCE;

            @Override
            public String convert(Reference reference) {
                //do stuff
            }
        }
    }

Into my controllers, I'm using MontoTemplate in order to talk with MongoDB. So, all converters are already loaded into template.

However, I'd like to test MongoDbTemplate using Spring injection features. I mean, I want to test MongoDbTemplate using custom converters which should already be loaded.

Any ideas on how it can be achieved?

EDIT

public class ModelTest {

    private List<Reference> references;

    public ModelTest() {
        this.references = new ArrayList<Reference>();
    }

    @Before
    public void setUp() {
        Reference reference = new Reference();
        reference.setId("Ref1");
        reference.setTimestamp(new Date());

        Metadata met = new Metadata();
        met.setId("Mdt1");
        met.setUser("user");
        met.setCreationTimestamp(new Date());

        met.setMetadata("[{'departament': 'JUST'}]");

        reference.setMetadata(met);

        this.references.add(reference);

        ServerAddress serverAddress = new ServerAddress("127.0.0.1", 27017);
        MongoClient mongoClient = new MongoClient(serverAddress);
        MongoTemplate mongoTemplate = new MongoTemplate(mongoClient, "db");

        mongoTemplate.insert(reference);
    }

    /**
     * Assert Office mime type documents.
     */
    @Test
    public void office() {
        fail("Not yet implemented");
    }
}

EDIT 2

I also would like to use custom testing properties. I mean, currently, we are setting properties into src/test/resources/application.properties.

spring.data.mongodb.host: localhost
spring.data.mongodb.port: 27017

How could I load these file properties?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
200 views
Welcome To Ask or Share your Answers For Others

1 Answer

Solution 1

If you want to test it with the Spring context, you can annotate your Test class as SpringBootTest and autowire the MongoTemplate. This should then contain your custom conversions for you to test them:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ModelTest {

    private List<Reference> references;

    @Autowired
    private final MongoTemplate mongoTemplate;

    public ModelTest() {
        this.references = new ArrayList<Reference>();
    }

    @Before
    public void setUp() {
        Reference reference = new Reference();
        reference.setId("Ref1");
        reference.setTimestamp(new Date());

        Metadata met = new Metadata();
        met.setId("Mdt1");
        met.setUser("user");
        met.setCreationTimestamp(new Date());

        met.setMetadata("[{'departament': 'JUST'}]");

        reference.setMetadata(met);

        this.references.add(reference);

        mongoTemplate.insert(reference);
    }

    /**
     * Assert Office mime type documents.
     */
    @Test
    public void office() {
        fail("Not yet implemented");
    }
}

Solution 2

If you just want to test the converter alone, you could make a ReferenceWriterConverterTest like so:

public class ReferenceWriterConverterTest {

    private ReferenceWriterConverter converter;

    @Before
    public void setUp() {
        converter = ReferenceWriterConverter.INSTANCE;
    }

    //test stuff
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...