I have test which is green when it is launched directly through IDE.
@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());
}
}
daoTest-context.xml
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/GArMu6 http://ift.tt/1jdM0fG">
<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>
However when maven command "mvn test" run this test it throws "Tests in error: shouldSaveClientInDBTest(dao.ClientDaoTest): Failed to load ApplicationContext" and the main explanation is
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()
Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods. Those methods cannot be stubbed/verified.
Please help me to find out what I did wrong.
Aucun commentaire:
Enregistrer un commentaire