@RunWith(SpringJUnit4ClassRunner.class)
public void ITest {
@Autowired
private EntityRepository dao;
@BeforeClass
public static void init() {
dao.save(initialEntity); //not possible as field is not static
}
}
How can I have my service injected already in the static init class?
It looks to me that you are trying to populate DB before tests.
I would give a try to two options:
@Sql
@DatabaseSetup
and@DatabaseTearDown
which will do thing on DB you needI know that this does not answer how to inject bean in static
@BeforeClass
but form code it looks it is solving your problem.Update: I recently run into same problem in my project and dug out this article which helped me and I think it is elegant way of dealing with this type of problem. You can extend
SpringJUnit4ClassRunner
with listener which can do instance level setup with all your defined beans.UPD for Spring 2.x versions.
Spring 2.x supports new feature a
SpringExtension
for Junit 5 Jupiter, where all you have to do is:Declare your test class with
@ExtendWith(SpringExtension.class)
Inject your
@BeforeAll
(replacement for@BeforeClass
in JUnit 5) with the beanFor example:
Assuming you correctly configured JUnit 5 Jupiter with Spring 2.x
More about it here: https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#testcontext-junit-jupiter-extension
To answer this question we should recap Spring 2.x versions.
If you want to "autowire" a bean in your
@BeforeTest
class you can use theApplicationContext
interface. Let's see an example:What's happening: using the
ClassPathXmlApplicationContext
we are instantiating all beans contained in theapplication-context.xml
file.With
context.getBean()
we read the bean specified (it must match the name of the bean!); and then you can use it for your initialization.You should give to the bean another name (that's the
dao2
!) otherwise Spring normal "autowired" cannot work on the predefined bean.As a side note, if your test extends
AbstractTransactionalJUnit4SpringContextTests
you can do some initialization usingexecuteSqlScript(sqlResourcePath, continueOnError)
; method, so you don't depend on a class/method that you also have to test separately.One workaround that I have been using to get this working is to use
@Before
with a flag to skip it being executed for each testcaseWith Junit 5 you can do this (@BeforeAll instead of @BeforeClass)
By leaving the field it means it can be used in other tests