我正在尝试使用junit对DAO Layer方法进行单元测试,但是因为没有找到xml文件而遇到一些错误。我的代码文件如下:
我的测试用例文件:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( locations = { "/classpath:web.xml", "/classpath:spring-mvc-crud-demo-servlet.xml"})
public class ClientYearDAOImplementationTest {
@InjectMocks
private ClientYearDAOImplementation clientYearDAOImplementation;
@Mock
private SessionFactory mockSessionFactory;
@Mock
private Session mockSession;
@Mock
private org.hibernate.Transaction transaction;
@Mock
private Query<ClientYear> mockQuery;
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
when(mockSessionFactory.openSession()).thenReturn(mockSession);
when(mockSession.beginTransaction()).thenReturn((org.hibernate.Transaction) transaction);
when(mockSession.getTransaction()).thenReturn(transaction);
doNothing().when(transaction).commit();
clientYearDAOImplementation = new ClientYearDAOImplementation();
when(mockSessionFactory.getCurrentSession()).thenReturn(mockSession);
when(mockSessionFactory.getCurrentSession()).thenReturn(mockSession);
when(mockSession.createQuery("from ClientYear", ClientYear.class)).thenReturn(mockQuery);
ClientYear.class)).thenReturn(mockQuery);
}
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void checkgetClientYear() {
ArrayList<ClientYear> clientYearExpectedList = new ArrayList<>();
List<ClientYear> clientYearReturnList = new ArrayList<>();
ClientYear clientyear1 = new ClientYear(1, 1);
ClientYear clientyear2 = new ClientYear(2, 1);
clientYearExpectedList.add(clientyear1);
clientYearExpectedList.add(clientyear2);
when(mockSessionFactory.getCurrentSession()).thenReturn(mockSession);
when(clientYearDAOImplementation.getClientYear()).thenReturn(clientYearExpectedList);
clientYearReturnList = clientYearDAOImplementation.getClientYear();
assertTrue(clientYearExpectedList.equals(clientYearReturnList));
}
}
此处ClientYear是POJO类,ClientYearDAOImplementationClass是DAO Layer类。
我试图在DAO层测试的功能是:
@Override
@Transactional
public List<ClientYear> getClientYear() throws HibernateException {
// get the current hibernate session
Session currentSession = sessionFactory.getCurrentSession();
// create a query
Query<ClientYear> theQuery = currentSession.createQuery("from ClientYear", ClientYear.class);
// execute query and get result list
List<ClientYear> clientYearList = theQuery.getResultList();
// return the results
return clientYearList;
}
错误堆栈跟踪:
SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@15975490] to prepare test instance [com.cernerbusinessmodel.test.ClientYearDAOImplementationTest@6b143ee9]
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [classpath:spring-mvc-crud-demo-servlet.xml]; nested exception is java.io.FileNotFoundException: class path resource [classpath:spring-mvc-crud-demo-servlet.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:223)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:194)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:258)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:93)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:44)
at org.springframework.test.context.TestContext.buildApplicationContext(TestContext.java:198)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:233)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:126)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:85)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:61)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:54)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.io.FileNotFoundException: class path resource [classpath:spring-mvc-crud-demo-servlet.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
... 25 more
java.lang.NoSuchMethodError: org.junit.runner.notification.RunNotifier.testAborted(Lorg/junit/runner/Description;Ljava/lang/Throwable;)V
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:146)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:61)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:54)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:52)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
我没有得到我出错的地方或者我有什么遗失的东西?