Skip to content

Conversation

@IlyaMuravjov
Copy link
Collaborator

@IlyaMuravjov IlyaMuravjov commented Jul 10, 2023

Description

Fixes #2330

Also, this pull request

  • makes it so Spring application context is loaded outside of per class timeout, since for large projects loading it for the first time (including transforming classes) can take over a minute
  • prevents interactions with TestContextManager from getting timed out
  • improves Throwable deserialization, so it tries to use constructors

How to test

Manual tests

Add the following class to the spring-boot-testing-main project and generate integration tests.

@Service
public class UnrelatedService {
    public UnrelatedService() {
        throw new RuntimeException("UnrelatedService");
    }
}

The following tests should get generated.

@ExtendWith(SpringExtension.class)
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@ActiveProfiles(profiles = {"default"})
@ContextConfiguration(classes = {OrderServiceApplication.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@Transactional
@AutoConfigureTestDatabase
public final class OrderServiceTest {


    /**
     * This sanity check test fails if the application context cannot start.
     * <p>
     * Context loading throws an exception.
     * Please try to fix your context or environment configuration.
     * Spring configuration applied: com.rest.order.OrderServiceApplication.
     */
    @Test
    public void contextLoads() {
        /* Failure caused by: java.lang.IllegalStateException: Failed to load ApplicationContext
                at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
                at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
                at org.utbot.spring.SpringApiImpl.getOrLoadSpringApplicationContext(SpringApiImpl.kt:49)
                ... 30 more
            Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'unrelatedService' defined in file [C:\Users\murav\IdeaProjects\spring-boot-testing-main-10\spring-boot-testing-main\target\classes\com\rest\order\services\UnrelatedService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.rest.order.services.UnrelatedService]: Constructor threw exception; nested exception is java.lang.RuntimeException: UnrelatedService; nested exception is java.lang.RuntimeException: Failed to instantiate [com.rest.order.services.UnrelatedService]: Constructor threw exception; nested exception is java.lang.RuntimeException: UnrelatedService
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1334)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1232)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
                at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
                at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
                at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
                at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
                at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
                at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
                at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:127)
                at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
                at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:276)
                at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:244)
                at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
                at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
                ... 32 more
            Caused by: java.lang.RuntimeException: Failed to instantiate [com.rest.order.services.UnrelatedService]: Constructor threw exception; nested exception is java.lang.RuntimeException: UnrelatedService
                at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:224)
                at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.lambda$instantiateBean$3(AbstractAutowireCapableBeanFactory.java:1322)
                at java.base/java.security.AccessController.doPrivileged(Native Method)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1321)
                ... 48 more
            Caused by: java.lang.RuntimeException: UnrelatedService
                at com.rest.order.services.UnrelatedService.<init>(UnrelatedService.java:8)
                at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
                at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
                at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
                at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
                at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:211)
                ... 52 more
             */
    }


    ///region Test suites for executable com.rest.order.services.OrderService.createOrder

    ///region Errors report for createOrder

    public void testCreateOrder_errors() {
        // Couldn't generate some tests. List of errors:
        // 
        // 1 occurrences of:
        // Failed to load Spring application context

    }
    ///endregion

    ///endregion

    ///region Test suites for executable com.rest.order.services.OrderService.deleteOrderById

    ///region Errors report for deleteOrderById

    public void testDeleteOrderById_errors() {
        // Couldn't generate some tests. List of errors:
        // 
        // 1 occurrences of:
        // Failed to load Spring application context

    }
    ///endregion

    ///endregion

    ///region Test suites for executable com.rest.order.services.OrderService.getOrderById

    ///region Errors report for getOrderById

    public void testGetOrderById_errors() {
        // Couldn't generate some tests. List of errors:
        // 
        // 1 occurrences of:
        // Failed to load Spring application context

    }
    ///endregion

    ///endregion

    ///region Test suites for executable com.rest.order.services.OrderService.getOrders

    ///region Errors report for getOrders

    public void testGetOrders_errors() {
        // Couldn't generate some tests. List of errors:
        // 
        // 1 occurrences of:
        // Failed to load Spring application context

    }
    ///endregion

    ///endregion
}

Self-check list

  • I've set the proper labels for my PR (at least, for category and component).
  • PR title and description are clear and intelligible.
  • I've added enough comments to my code, particularly in hard-to-understand areas.
  • The functionality I've repaired, changed or added is covered with automated tests.
  • Manual tests have been provided optionally.
  • The documentation for the functionality I've been working on is up-to-date.

@IlyaMuravjov IlyaMuravjov added comp-codegen Issue is related to code generator comp-instrumented-process Issue is related to Instrumented process comp-spring Issue is related to Spring projects support ctg-enhancement New feature, improvement or change request labels Jul 11, 2023
@EgorkaKulikov EgorkaKulikov self-requested a review July 11, 2023 12:45
@IlyaMuravjov IlyaMuravjov merged commit 2b66d12 into main Jul 11, 2023
@IlyaMuravjov IlyaMuravjov deleted the ilya_m/contextloads_test branch July 11, 2023 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-codegen Issue is related to code generator comp-instrumented-process Issue is related to Instrumented process comp-spring Issue is related to Spring projects support ctg-enhancement New feature, improvement or change request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Spring integration test that context is loaded properly

3 participants