Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

Spring Boot: Testing custom MongoTemplate converters

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

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

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...