Announcement Announcement Module
Collapse
No announcement yet.
Getting application context while junit tests Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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 ... )

      Comment


      • #4
        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

        Comment

        Working...
        X