jonua
jonua

Reputation: 2005

Get access to spring context from JUnit test

I try to run test for spring framework. I use only the xml context. In test (JUnit) class I specify

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"springContext.xml"})
public class MyTest extends TestCase {
    @Test
    public void test() {

    }
}

but I don't know how to get access to the spring context in the @Test method? I don't can use of @Autowired annotation, because I don't use annotation driven context

Upvotes: 0

Views: 2193

Answers (1)

Martin Strejc
Martin Strejc

Reputation: 4347

The test class is like a regular bean. So far you can use both of those in your test class:

  • autowire ApplicationContext
  • implement ContextAware interface

See How to inject ApplicationContext itself

Upvotes: 2

Related Questions