title: Spring Boot MongoDB Repository null during unit test
body: <p>I have been at this for a while but can't figure it out. The repo injects fine when running the app normal, but when trying to do a spring boot unit test it never injects. Here is the code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>package com.g2p.g2prestservice.repositories;
  
          import com.g2p.g2prestservice.model.User;
          import org.springframework.data.mongodb.repository.MongoRepository;
  
  public interface UserRepository extends MongoRepository&lt;User, Integer&gt; {
  
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>package com.g2p.g2prestservice.repositories;
  
  import com.g2p.g2prestservice.model.User;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.test.context.junit4.SpringRunner;
  import org.testng.annotations.Test;
  
  import static org.testng.Assert.assertEquals;
  
  @RunWith(SpringRunner.class)
  @SpringBootTest
  public class UserRepositoryTest {
  
      @Autowired
      UserRepository userRepository;
  
      @Test
      public void testInsertUser() {
          User user = new User("fake@email.com", "fakePassword");
  
          userRepository.save(user);
  
          assertEquals(userRepository.count(), 1);
      }
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I am essentially trying to follow this guide as example: <a href="https://springframework.guru/configuring-spring-boot-for-mongo/" rel="nofollow noreferrer">https://springframework.guru/configuring-spring-boot-for-mongo/</a></p>
  
  <p>Thank you all for solving what I am sure is a very elementary mistake.</p>
  
  <p>EDIT: I THINK the problem is that the spring context isn't launching when I run the test class...</p>
  
  <p>EDIT: Here is launcher class:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>package com.g2p.g2prestservice;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
  
  @SpringBootApplication
  public class G2pRestServiceApplication {
  
   public static void main(String[] args) {
    SpringApplication.run(G2pRestServiceApplication.class, args);
   }
  }</code></pre>
  </div>
  </div>
  </p>