当maven启动时,绿色测试失败

时间:2016-01-26 22:44:00

标签: java spring maven testing junit

我通过IDE直接启动时测试为绿色。

loadRelations

daoTest-context.xml中

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:dao/daoTest-context.xml"})
@ActiveProfiles(profiles = "test")
public class ClientDaoTest {

@Autowired
ClientDao clientDao;

@Test
public void shouldSaveClientInDBTest(){
    Client client = new Client();
    client.setName("ivan");

    client = clientDao.saveClient(client);

    assertNotNull(client.getId());
    assertEquals("ivan", client.getName());
}


}

然而,当maven命令“mvn test”运行此测试时,它会抛出:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<import resource="classpath:spring-context.xml" />

<beans profile="test">
    <bean id="ConnectionDB" name="ConnectionDB" class="org.mockito.Mockito" factory-method="mock">
        <constructor-arg value="biz.podoliako.carwash.services.impl.ConnectDB" />
    </bean>

    <bean id="myDataSourceTest" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
        <property name="driverClassName" value="org.h2.Driver"/>
        <property name="url" value="jdbc:h2:~/test"/>
        <property name="username" value=""/>
        <property name="password" value=""/>
    </bean>

    <bean id="entityManagerFactory"  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="myDataSourceTest"/>
        <property name="packagesToScan" >
            <list>
                <value>biz.podoliako.carwash</value>
            </list>
        </property>

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="true" />
                <property name="showSql" value="true" />
            </bean>
        </property>

        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.connection.pool_size">1</prop>
            </props>
        </property>
    </bean>
</beans>

主要解释是:

Tests in error: shouldSaveClientInDBTest(dao.ClientDaoTest): Failed to load ApplicationContext 

此外,此错误可能会显示,因为您验证了以下任一项:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ConnectionDB' defined in class path resource [dao/daoTest-context.xml]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.lang.Object]: Factory method 'mock' threw exception**; nested exception is org.mockito.exceptions.misusing.UnfinishedVerificationException: Missing method call for verify(mock) here: -> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) Example of correct verification: verify(mock).doSomething()

这些方法无法进行存根/验证。

请帮我弄清楚我做错了什么。

0 个答案:

没有答案