IllegalState无法使用jUnit和Spring加载ApplicationContext

时间:2015-01-05 22:45:46

标签: java spring maven junit

我正在尝试使用Spring和jUnit运行一些测试,但是当我运行mvn -Pprod package来创建war文件时,每个测试文件都会失败。

这是一个测试类示例:

@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<Nominee> expResult = new ArrayList();
        expResult.add(nominee);

        when(nomineeRepository.findNomineesByCategoryId(anyString())).thenReturn(expResult);
        when(voteRepository.getVotesByCategoryAndNominee(anyString(),anyString())).thenReturn(2);

        List<Nominee> 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);
    }
}

在编译结束时,这是终端上显示的内容:

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...

问题是我在CentOS 7机器上运行这个项目并且它给了我那些错误但是当我在Windows机器上做同样的事情时,它就像一个魅力。所以,我在想,也许我错过了一些Linux配置,但我根本找不到它。

我已经在SO中阅读了许多其他相关问题,但这些问题都不适用于我。

0 个答案:

没有答案
相关问题