I am using SpringBoot version = 2.2.8 and Spring=5.2.7. We have a set of integration test cases , which are using mocks within them, to mock certain parts of the application. One of the test cases, hits the database using Hibernate and does not use MockBeans whereas another uses MockBean . The test case calling the service, which eventually uses Hibernate throws the following error

javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: service.domain.SaveProfile
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:748)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:710)
    at org.hibernate.engine.spi.CascadingActions$7.cascade(CascadingActions.java:298)
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:492)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:416)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:218)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:151)
    at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBefore

Though we have about 3500 test cases but in order to reproduce the problem, I have only a couple of them . I think the problem is that the application context is changed during the course of the test suite , causing the issue. I don't get the issue when running individual test cases

class ActionServiceTest extends  BaseTestCase {

    @MockBean
    @Qualifier('renewalAdverseActionsTableV9')
    RenewalAdverseActionsTable renewalAdverseActionsTable

    @MockBean
    @Qualifier("storeAdverseActionsTableV9")
    StoreAdverseActionsTable storeAdverseActionsTable

    @MockBean
    @Qualifier("dmAdverseActionsTableV9")
    DmAdverseActionsTable dmAdverseActionsTable
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/META-INF/applicationContext.xml")
public abstract class BaseTestCase {

    @Autowired
    protected ApplicationContext applicationContext

}
public class DatabaseIntegrationTest extends BaseTestCase {

    @Autowired
     DatabaseService databaseService

    @Test
    void testRun_Decline() {

        Response runRiskResponse = databaseService.run(request)
        /////
    }

}

I also realized if I copy the MockBean configurations from the 1st case onto the failing one, it works. Maybe because now the context is the same ??

Comment From: wilkinsona

@romashatomar Thanks for opening an issue. As requested on #7174, can you please provide a minimal sample that reproduces the problem. Unfortunately, the snippets above are not sufficient as there are several parts missing - build system configuration, various classes, etc. You can share the sample with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

Comment From: steklopod

Have similar issue when using gradle plugin and @AutoConfigureMockMvc

id("org.springframework.boot") version "2.3.5.RELEASE"

All @SIntegrationTest ok, but when first @WebTest test freeze and never ends.

But with 2.3.4.RELEASE - no such problem

// OK
@SpringBootTest
@Target(CLASS)
@Retention(RUNTIME)
@ActiveProfiles("test")
annotation class IntegrationTest

// Freeze (deadlock)
@Target(CLASS)
@Retention(RUNTIME)
@IntegrationTest
@AutoConfigureMockMvc
annotation class WebTest

Also in this project I'm using liquibase. No exception. Just deadlock

@WebTest
internal class UserControllerTest(@Autowired private val mockMvc: MockMvc) {
    private val baseUrl = "/user"

    @Test
    fun userTest() {
        mockMvc.perform(postWithTokenInHeader(baseUrl))
//            .andDo(MockMvcResultHandlers.print())
            .andExpect(status().isOk)
}

Comment From: wilkinsona

Thanks, @steklopod. That sounds like a different problem to me as you're seeing a freeze whereas @romashatomar reported seeing a failure. If you would like us to spend some time investigating, please open a new issue with a complete yet minimal sample that reproduces the problem. You can share that sample with us by zipping it up and attaching it to the issue that you create or by pushing it to a separate repository on GitHub.

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.