@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext-test.xml"})
@Transactional
public class MyServiceTest {
@Resource(name="myService")
public MyService myService;
@Test
public void testSeomthing() {
//do some asserts using myService.whatever()
}
}
However the tests are based on data I migrate in, so every time I run my suite of tests I want to execute my unrelated migration code. I don’t want to run a @Before in each test class. I want to run it once at beginning of complete test process, where can I put this ?
I would advice you to create a test bean somewhere with startup logic invoked in
@PostConstruct:Obviously this bean should only be created for tests, the easiest way to achieve this is to place it in
src/test/javain a package that iscomponent-scanned by Spring for@Service-annotated classes.Note: you must remember that
@PostConstructis not running in a transaction! See How to call method on spring proxy once initialised.