title: IllegalState Failed to load ApplicationContext using jUnit and Spring
body: <p>I am trying to run some tests using Spring and jUnit, but when I run <code>mvn -Pprod package</code> to create the war file, every test file fails.</p>
  
  <p>Here is one test class example:</p>
  
  <pre class="lang-java prettyprint-override"><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = Application.class)
  @WebAppConfiguration
  public class VoteServiceTest {
  
      @InjectMocks
      @Autowired
      private VoteService voteService;
  
      @Mock
      private VoteRepository voteRepository;
  
      @Mock
      private NomineeRepository nomineeRepository;
  
      private Vote vote = new Vote("userId", "categoryId", "nominneId");
      private Nominee nominee = new Nominee("name", "description", null, null);
  
      @Before
      public void setup() {
          MockitoAnnotations.initMocks(this);
      }
  
      /**
       * Test of castVote method, of class VoteService.
       */
      @Test
      public void testUpdateVote() {
          ReflectionTestUtils.setField(vote, "id", "votID0001");
          when(voteRepository.findExistingVote(anyString(),anyString())).thenReturn(vote);
          voteService.castVote(vote);        
      }
  
  
      @Test
      public void testCastNewVote() {
          when(voteRepository.findExistingVote(anyString(),anyString())).thenReturn(null);
          voteService.castVote(vote);        
      }
  
      /**
       * Test of getResultsByCategory method, of class VoteService.
       */
      @Test
      public void testGetResultsByCategory() {
          ReflectionTestUtils.setField(nominee, "id", "catID0001");
          List&lt;Nominee&gt; expResult = new ArrayList();
          expResult.add(nominee);
  
          when(nomineeRepository.findNomineesByCategoryId(anyString())).thenReturn(expResult);
          when(voteRepository.getVotesByCategoryAndNominee(anyString(),anyString())).thenReturn(2);
  
          List&lt;Nominee&gt; result = voteService.getResultsByCategory("catID0001");
          assertEquals(expResult, result);
  
      }
  
      /**
       * Test of getVoteByCategoryAndUser method, of class VoteService.
       */
      @Test
      public void testGetVoteByCategoryAndUser() {
          when(voteRepository.findExistingVote(anyString(),anyString())).thenReturn(vote);
  
          Vote result = voteService.getVoteByCategoryAndUser("cat001", "usr001");
          assertEquals(vote, result);
      }
  }
  </code></pre>
  
  <p>At the end of compilation, this is what is shown on the terminal:</p>
  
  <pre><code>Results :
  
  Tests in error: 
    VoteRepositoryTest.testCountVotes » IllegalState Failed to load ApplicationCon...
    VoteRepositoryTest.deleteVotes » IllegalState Failed to load ApplicationContex...
    VoteRepositoryTest.testListByNominee » IllegalState Failed to load Application...
    UserServiceTest.testFindNotActivatedUsersByCreationDateBefore » IllegalState F...
    UserServiceTest.testRemoveOldPersistentTokens » IllegalState Failed to load Ap...
    VoteServiceTest.testGetResultsByCategory » IllegalState Failed to load Applica...
    VoteServiceTest.testGetVoteByCategoryAndUser » IllegalState Failed to load App...
    VoteServiceTest.testUpdateVote » IllegalState Failed to load ApplicationContex...
    VoteServiceTest.testCastNewVote » IllegalState Failed to load ApplicationConte...
    AccountResourceTest.testGetUnknownAccount » IllegalState Failed to load Applic...
    AccountResourceTest.testAuthenticatedUser » IllegalState Failed to load Applic...
    AccountResourceTest.testNonAuthenticatedUser » IllegalState Failed to load App...
    AccountResourceTest.testGetExistingAccount » IllegalState Failed to load Appli...
    AuditResourceTest.testFindAll » IllegalState Failed to load ApplicationContext
    AuditResourceTest.testFindByDates » IllegalState Failed to load ApplicationCon...
    CategoryResourceTest.testSaveCategories » IllegalState Failed to load Applicat...
    CategoryResourceTest.testGetCategories » IllegalState Failed to load Applicati...
    CategoryResourceTest.testDeleteCategoryNotFound » IllegalState Failed to load ...
    CategoryResourceTest.testUpdateCategories » IllegalState Failed to load Applic...
    CategoryResourceTest.testSaveEmptyCategory » IllegalState Failed to load Appli...
    CategoryResourceTest.testDeleteCategory » IllegalState Failed to load Applicat...
    CategoryResourceTest.testGetCategoryById » IllegalState Failed to load Applica...
    LogsResourceTest.testGetList » IllegalState Failed to load ApplicationContext
    LogsResourceTest.testChangeLevel » IllegalState Failed to load ApplicationCont...
    NomineeResourceTest.testSearchWithResults » IllegalState Failed to load Applic...
    NomineeResourceTest.testSaveCategories » IllegalState Failed to load Applicati...
    NomineeResourceTest.testDeleteNonExisting » IllegalState Failed to load Applic...
    NomineeResourceTest.testDeleteExisting » IllegalState Failed to load Applicati...
    NomineeResourceTest.testDeleteNominee » IllegalState Failed to load Applicatio...
    NomineeResourceTest.testDeleteNotExisting » IllegalState Failed to load Applic...
    NomineeResourceTest.testSearch » IllegalState Failed to load ApplicationContex...
    UserResourceTest.testGetUnknownUser » IllegalState Failed to load ApplicationC...
    UserResourceTest.testGetExistingUser » IllegalState Failed to load Application...
    VoteResourceTest.testGetResultsByCategory » IllegalState Failed to load Applic...
    VoteResourceTest.testGetResultsByCategoryAndUser » IllegalState Failed to load...
    VoteResourceTest.testCastVote » IllegalState Failed to load ApplicationContext
    VoteResourceTest.testCastEmptyVote » IllegalState Failed to load ApplicationCo...
  </code></pre>
  
  <p>The thing is that I'm running this project on a CentOS 7 machine and it gives me those errors but when I do the same thing on a Windows machine, it works like a charm. So, I'm thinking that maybe I'm missing some Linux configuration but I can't find it at all.</p>
  
  <p>I've read many other related questions here  in SO but none of those worked for me.</p>