Results 1 to 4 of 4

Thread: Getting application context while junit tests

  1. #1
    Join Date
    Jun 2008
    Location
    Germany
    Posts
    67

    Default Getting application context while junit tests

    Hi,

    is a better way of getting the application context while junit testing than the following:

    Code:
     protected static ApplicationContext ctx;
    
        public static ApplicationContext getApplicationContext() {
            if (Application.isLoaded()) {
                // normal apps life
                ctx = Application.instance().getApplicationContext();
            } else {
                // unit testing
                ctx = new ClassPathXmlApplicationContext(
                        new String[]{"/de/timefinder/core/ctx/application-context.xml"});
            }
            return ctx;
        }

  2. #2
    Join Date
    Aug 2004
    Location
    San Francisco
    Posts
    423

    Default

    Not really RCP specific but yes, Spring has support for both JUnit and TestNG. Take a look at the testing section in the user manual (http://static.springframework.org/sp...text-framework).

    Jonny

  3. #3
    Join Date
    Jun 2008
    Location
    Germany
    Posts
    67

    Default

    My problem was that the normal way of getting the application context
    Application.instance().getApplicationContext()
    will not work in unit tests ("the spring rich client application was not started")
    so I had to introduce this ugly hack. How do you get the context in your application? (and then in unit tests ... )

  4. #4
    Join Date
    Aug 2004
    Location
    San Francisco
    Posts
    423

    Default

    Ahh, sorry I misunderstood the first message.

    I've actually never had that problem with my unit tests. I tend to test my classes is isolation, outside of the spring configuration environment. I other applications I do have what I call integration tests that do bring in the spring environment but I've never done that with RCP applications.

    Sorry I can't be more help, but I think your solution reflects one piece of the SRCP framework that people have been talking about changing - the overuse of singletons and static methods.

    Jonny

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •