Skip to content

Add utbot-spring-commons-shadow.jar to engine process utContext #2543

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

Merged
merged 1 commit into from
Aug 23, 2023

Conversation

IlyaMuravjov
Copy link
Collaborator

Description

Fixes generation of failing contextLoads() test after #2479 which caused it to fail with ClassNotFoundexception: org.utbot.spring.api.UTSpringContextLoadingException

Also, lessens the size of utbot-spring-commons-shadow.jar by not including common UtBot dependencies that are present in every UtBot process anyway.

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 ctg-bug-fix PR is fixing a bug comp-spring Issue is related to Spring projects support labels Aug 22, 2023
@IlyaMuravjov IlyaMuravjov merged commit abb6268 into main Aug 23, 2023
@IlyaMuravjov IlyaMuravjov deleted the ilya_m/spring_commons_in_utcontext branch August 23, 2023 09:14
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-fix PR is fixing a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants