Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8514827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:58:35+00:00 2026-06-11T04:58:35+00:00

I have the following piece of code for my abstract test class (I know

  • 0

I have the following piece of code for my abstract test class (I know XmlBeanFactory with ClassPathResource is deprecated, but it’s unlikely to be the case of the problem).

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public abstract class AbstractIntegrationTest {

    /** Spring context. */
    protected static final BeanFactory context = new XmlBeanFactory(new ClassPathResource(
            "com/.../AbstractIntegrationTest-context.xml"));

    ...

}

It loads the default test configuration XML file AbstractIntegrationTest-context.xml (and then I use autowiring). I also need to use Spring in static methods annotated with @BeforeClass and @AfterClass, so I have a separate context variable pointing to the same location. But the thing is that this is a separate context, which will have different instances of beans. So how can I merge these contexts or how can I invoke Spring’s bean initialization defined by @ContextConfiguration from my static context?

I have in mind a possible solution by getting rid of those static members, but I’m curious, if I can do it with relatively small changes to the code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:58:37+00:00Added an answer on June 11, 2026 at 4:58 am

    You are right, your code will produce two application contexts: one will be started, cached and maintained for you by @ContextConfiguration annotation. The second context you create yourself. It doesn’t make much sense to have both.

    Unfortunately JUnit is not very well suited for integration tests – mainly because you cannot have before class and after class non-static methods. I see two choices for you:

    • switch to testng – I know it’s a big step

    • encode your setup/tear down logic in a Spring bean included in the context only during tests – but then it will run only once, before all tests.

    There are also less elegant approaches. You can use static variable and inject context to it:

    private static ApplicationContext context;
    
    @AfterClass
    public static afterClass() {
        //here context is accessible
    }
    
    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext) {
        context = applicationContext;
    }
    

    Or you can annotate your test class with @DirtiesContext and do the cleanup in some test bean:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration
    @DirtiesContext(classMode = AFTER_CLASS)
    public abstract class AbstractIntegrationTest {
    
        //...
    
    }
    
    public class OnlyForTestsBean {
    
        @PreDestroy
        public void willBeCalledAfterEachTestClassDuringShutdown() {
            //..
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following piece of code: class Test{ private: int id; public: Test(int v):id(v)
I have the following piece of code within my stored procedure , I know
I have following piece of code: class A { public C GetC() { return
I have the following piece of code: actor { loop { react { case
I have the following piece of code its working fine,no issues but i am
I have the following piece of code in Visual C++ 2005 : : class
I have the following piece of code. It all works but I'm having a
I have the following piece of code <a onclick=$('#result').load('codes/test.html');$('#result').show(); >XHTML code</a> it loads perfectly
I don't know that the following piece of code is really relevant, but for
I have following piece of code from my Backbone project: App.Controllers.Test = Backbone.Router.extend({ routes:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.