Already tried all possible options, but no result. I do not return the correct value from the database.
There is a class:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:unit-test-context/test-context.xml", "classpath:unit-test-context/servlet-context.xml" }) @WebAppConfiguration public class PlaceTypeServiceTest { PlaceType placeType = new PlaceType(); List<PlaceType> placeTypeList = new ArrayList<PlaceType>(); PlaceTypeForm placeTypeForm = new PlaceTypeForm(); @InjectMocks PlaceTypeServiceImpl placeTypeServiceImpl; @Mock private PlaceTypeDaoImpl mockPlaceTypeDaoImpl; @Mock private PlaceTypeServiceImpl mockPlaceTypeServiceImpl; @Before public void setUp() { placeTypeForm.setId(1L); placeType.setId(4L); placeType.setName("Test"); placeTypeList.add(placeType); MockitoAnnotations.initMocks(this); } @Test public void addPlaceTypeTest(){ int actual = placeTypeServiceImpl.getAllPlaceType().size(); System.out.println(actual + "***"); placeTypeServiceImpl.addPlaceType(placeType); int expected = placeTypeServiceImpl.getAllPlaceType().size(); System.out.println(expected+"***"); assertNotEquals(expected, actual); } The test should return different values at the end, since after writing to the base of a new entity, the length of the base will change. but it returns nothing in principle.
here is test-context.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter" ref="jpaVendorAdapter" /> <property name="jpaProperties"> <props> <prop key="hibernate.connection.characterEncoding">utf8</prop> <prop key="hibernate.hbm2ddl.auto">create</prop> <!-- DATABASE INSERT --> <prop key="hibernate.hbm2ddl.import_files">/insert.sql</prop> </props> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.postgresql.Driver" /> <property name="url" value="jdbc:postgresql://localhost:5433/test" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="POSTGRESQL" /> <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" /> <property name="showSql" value="true" /> <property name="generateDdl" value="true" /> </bean> <!-- Provides auto-injecting EntityManager into the DAOs field with annotation @PersistenceContext --> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <!-- Provides translation native under layer Exceptions into Spring Exceptions in DAOs classes with annotation @Repository --> <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> <!-- Provides declarative transaction management --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <context:component-scan base-package="com.khodan.caferacer"> <!-- Avoids to duplicate class scan in root-context and application-servlet.xml --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <bean id="placeTypeService" name="placeTypeService" class="org.mockito.Mockito" factory-method="mock"> <constructor-arg value="com.khodan.caferacer.service.PlaceTypeServiceImpl" /> </bean> In the file insert.sql, every time the tests are run, three entiti are inserted into the database, I have already rechecked it. But in the test method, it still returns the length of the table to zero. I also recorded the bin of the service itself.
I definitely make a mistake somewhere, maybe someone will be able to notice and suggest what the error is.