Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid entity is generated and then searched with Controller #2533

Closed
alisevych opened this issue Aug 18, 2023 · 1 comment · Fixed by #2550
Closed

Invalid entity is generated and then searched with Controller #2533

alisevych opened this issue Aug 18, 2023 · 1 comment · Fixed by #2550
Assignees
Labels
comp-spring Issue is related to Spring projects support ctg-bug Issue is a bug spec-ux Issue is influencing user experience

Comments

@alisevych
Copy link
Member

Description

Invalid entity is generated and then searched with Controller

To Reproduce

  1. Install UnitTestBot plugin built from main in IntelliJ IDEA
  2. Open spring-petclinic
  3. Generate Integration tests for OwnerController$findOwner with PetClinicApplication configuration selected

Expected behavior

Valid entity is expected to be created and saved in to OwnerRepository.
Id should be saved and then used in findOwner method call.
Some invalid id should be used in another test.

Actual behavior

Owner object is tried to be saved with entityManager.persist()
But it is not valid and ConstraintViolationException is thrown.
then findOwner method is called with id=-65

Screenshots, logs

	///region FUZZER: ERROR SUITE for method findOwner(java.lang.Integer)

	/**
	 * @utbot.classUnderTest {@link OwnerController}
	 * @utbot.methodUnderTest {@link OwnerController#findOwner(Integer)}
	 */
	@Test
	@DisplayName("findOwner: ownerId = -65 (mutated from -1) -> throw AssertionFailure")
	public void testFindOwnerThrowsAF() {
		Owner owner1 = new Owner();
		try {
			entityManager1.persist(owner1);
		} catch (jakarta.validation.ConstraintViolationException constraintViolationException1) {
		}

        /* This test fails because method [org.springframework.samples.petclinic.owner.OwnerController.findOwner] produces [org.hibernate.AssertionFailure: null id in org.springframework.samples.petclinic.owner.Owner entry (don't flush the Session after an exception occurs)]
            org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:82)
            org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:171)
            org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:130)
            org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107)
            org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:214)
            org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:90)
            org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:48)
            org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107)
            org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1388)
            org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.lambda$new$0(ConcreteSqmSelectQueryPlan.java:111)
            org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.withCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:335)
            org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:276)
            org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:571)
            org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:363)
            org.hibernate.query.sqm.internal.QuerySqmImpl.list(QuerySqmImpl.java:1073)
            org.hibernate.query.spi.AbstractSelectionQuery.getSingleResult(AbstractSelectionQuery.java:457)
            org.hibernate.query.sqm.internal.QuerySqmImpl.getSingleResult(QuerySqmImpl.java:1103)
            org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:193)
            org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:90)
            org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:148)
            org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:136)
            org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:136)
            org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:120)
            org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:164)
            org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:143)
            org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
            org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
            org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:390)
            org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
            org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
            org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
            org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
            org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:134)
            org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
            org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
            org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
            org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:218)
            jdk.proxy3/jdk.proxy3.$Proxy182.findById(Unknown Source)
            org.springframework.samples.petclinic.owner.OwnerController.findOwner(OwnerController.java:62) */
		ownerController.findOwner(-65);
	}
	///endregion

Environment

IntelliJ IDEA version - Ultimate 2023.2
Project - Gradle
JDK - 17

@alisevych alisevych added ctg-bug Issue is a bug comp-spring Issue is related to Spring projects support labels Aug 18, 2023
@alisevych alisevych added this to the Spring Phase 4 milestone Aug 18, 2023
@alisevych
Copy link
Member Author

As discussed with @IlyaMuravjov

entityManager1.persist(owner1);

For this project that can be used. But if user repository class extends CRUD or JPA repository, it is appropriate to use it for data saving. Need to escape entity EntityManager usage and invoke user class methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-spring Issue is related to Spring projects support ctg-bug Issue is a bug spec-ux Issue is influencing user experience
Projects
Archived in project
3 participants