title: Fail to load ApplicationContext SpringBoot with SpringSecurity and JUnit Jupiter
body: <p>I'm working on a REST API using Spring Boot. Currently, the V1 of the API is complete. So, I'm implementing Spring Security to manage authentication and authorization.<br />
  Since I've implemented Spring Security, my JUnit Jupiter tests does not work (no one works).</p>
  <p>I searched a lot a solution on internet, but all answers I found are for JUnit4 and not JUnit5 (so I don't have all required classes).<br />
  I got the classical &quot;Fail to load ApplicationContext&quot; error, but I don't know how to solve it.</p>
  <p>Can you help me?</p>
  <p>Here is my code for one class (UserController):</p>
  <p>gradle.build:</p>
  <pre><code>plugins {
      id 'jacoco'
      id 'org.springframework.boot' version '2.6.0'
      id 'io.spring.dependency-management' version '1.0.11.RELEASE'
      id 'java'
  }
  
  group = 'com.example'
  version = '0.0.1-SNAPSHOT'
  sourceCompatibility = '11'
  
  configurations {
      compileOnly {
          extendsFrom annotationProcessor
      }
  }
  
  repositories {
      mavenCentral()
  }
  
  dependencies {
      implementation 'org.springframework.boot:spring-boot-starter-web:2.5.6'
      implementation 'org.springframework.boot:spring-boot-starter-data-jpa:2.5.6'
      implementation 'org.projectlombok:lombok:1.18.22'
      annotationProcessor 'org.projectlombok:lombok:1.18.22'
      developmentOnly 'org.springframework.boot:spring-boot-devtools:2.5.6'
      testImplementation 'org.springframework.boot:spring-boot-starter-test:2.5.6'
      implementation 'com.h2database:h2'
      runtimeOnly 'com.h2database:h2'
  }
  
  test {
      systemProperty 'spring.profiles.active', 'test'
      useJUnitPlatform()
      finalizedBy jacocoTestReport
  }
  </code></pre>
  <p>Application:</p>
  <pre class="lang-java prettyprint-override"><code>@SpringBootApplication
  public class BackendApplication {
      public static void main(String[] args) {
          SpringApplication.run(BackendApplication.class, args);
      }
  }
  </code></pre>
  <p>UserController sample:</p>
  <pre class="lang-java prettyprint-override"><code>@RestController
  @RequestMapping(&quot;/api/v1/users&quot;)
  public class UserController extends AbstractCrudController&lt;User, Long&gt; {
  
      @Autowired
      public UserController(CrudService&lt;User, Long&gt; service) { super(service); }
  
      @GetMapping(&quot;&quot;)
      @Override
      @Secured({ &quot;ROLE_XXXXX&quot; })
      public ResponseEntity&lt;ResponseListDto&lt;User, Long&gt;&gt; findAll() {
          return super.findAll();
      }
  
      // ...
  }
  </code></pre>
  <p>MockedUserControllerTest sample:</p>
  <pre class="lang-java prettyprint-override"><code>@SpringBootTest
  public class MockedUserControllerTest {
  
      @Mock
      private UserService service;
  
      @InjectMocks
      private UserController controller;
  
      private static User user;
      private static List&lt;User&gt; users;
  
      @BeforeAll
      public static void beforeAll() {
          user = new User();
          user.setId(1L);
          user.setUsername(&quot;A user name&quot;);
          user.setFirstname(&quot;First-Name&quot;);
          user.setLastname(&quot;Last-Name&quot;);
          user.setPassword(&quot;A Gre4t P4ssw0rd!&quot;);
          user.setMail(&quot;first-name.last-name@mail.com&quot;);
          user.setBirthDate(Date.valueOf(&quot;1980-01-15&quot;));
          user.setKey(&quot;A-key&quot;);
          user.setNewsletter(Boolean.TRUE);
  
          users = List.of(user);
      }
  
      @Test
      public void testFindAll() {
          when(service.findAll()).thenReturn(users);
          assertEquals(new ResponseEntity&lt;&gt;(new ResponseListDto&lt;&gt;(users, null, null), HttpStatus.OK),
                  controller.findAll());
      }
  
      //...
  }
  </code></pre>
  <p>Thank you in advance for looking my problem.</p>