2

I am new to SpringBoot. I have built a simple application which should use fake data in the development environment, and connect to MongoDb in the test environment. Dev environment does not have mongodb setup.

I have tried using Spring Boot qualifiers/profiles to achieve it.

I have a main class which looks like the following:

@SpringBootApplication
public class Main {
   public static void main(String[] args) {
       SpringApplication.run(Main.class, args);
   }
}

I have a DAO interface StudentDao.java

public interface StudentDao {
    Student getStudentById(String id);
}

I then created a couple of implementations for the DAO, one for fake data, and one for data from Mongo

FakeStudentDaoImpl.java

@Repository
@Qualifier("fakeData")
public class FakeStudentDaoImpl implements StudentDao {

    private static Map<String, Student> students;

    static {

        students = new HashMap<String, Student>(){
            {
                put("1", new Student("Ram", "Computer Science"));
            }
        };
    }

    @Override
    public Student getStudentById(String id){
        return this.students.get(id);
    }
}

MongoStudentDaoImpl.java

@Repository
@Qualifier("mongoData")
public class MongoStudentDaoImpl implements StudentDao {

    @Autowired
    private MongoStudentRepo repo;

    @Override
    public Student getStudentById(String id) {
        return  repo.findById(id).get();
    }
}

The MongoStudentRepo is a simple interface extending MongoRepository:

public interface MongoStudentRepo extends MongoRepository<Student, String> {
}

And my POM file has the following dependencies called out:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

Of course, I have other controller classes. This works fine in the Test environment, where there is a MongoDb, and it is able to connect to it. However, when I am trying to start it in my local environment, it fails to start because it is not finding MongoDb on startup.

How do I disable the MongoDb part in my local environment (and just use fake data)? I want to make the same code work in both environments.

Thanks in advance.

TechiRik
  • 1,893
  • 6
  • 27
  • 37
  • for `local` and `dev` environment just use in memory `H2` database, you don't need to change anything other than configs – Ryuzaki L May 03 '19 at 00:20
  • 1
    Did you try an embedded mongodb database? – earandap May 03 '19 at 01:44
  • Alternatively you might consider using embedded mongodb for dev, or run a mongodb instance using docker (which is what I do). See: https://www.baeldung.com/spring-boot-embedded-mongodb . This will save you creating and maintaining fake repositories, etc. – peekay May 03 '19 at 01:52

3 Answers3

2

I had the same problem, and found the solution you ask for in this other question:

Spring Boot. How to disable Initialization of JPA Conditionaliy

For example, if you want to disable spring-data-mongodb in the development environment, then, assuming that you run under a "dev" profile:

application-dev.yml:

spring:
  autoconfigure:
    exclude: org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
abl
  • 5,970
  • 4
  • 25
  • 44
1

You can use an embedded MongoDB database. Here an example.

earandap
  • 1,446
  • 1
  • 13
  • 22
1

Several possible options:

1) You can use spring profiles. Map one bean with @Profile("test) and second one with @Profile("prod"). To specify which profile to use --spring.profiles.active=test

2) You can have different configurations.

application-prod.yml
--------------
mongo-url:produrl

application-test.yml
--------------
mongo-url:localhost

Use spring active profiles to select config. To use local profile you need to setup local mongo instance. And you can have several options again: just download instance, docker image, embeded mongo.

Ivan Lymar
  • 2,180
  • 11
  • 26