0

I have a class like

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={MainConfig.class})
public class AbstractCSVFileProcessOpTest {

   @Autowired
   FileSource fileSource;

   @Autowired
   ReadFileOp readFileOp;

can I execute something after autowired fields initialized? Someting like InitializingBean in tests?

Dims
  • 42,427
  • 94
  • 291
  • 543

1 Answers1

1

You can use @PostConstruct to execute a particular method immediately after the constructor has done its work.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={MainConfig.class})
public class AbstractCSVFileProcessOpTest {

    @Autowired
    FileSource fileSource;

    @Autowired
    ReadFileOp readFileOp;

    @PostConstruct
    public void init() {
        // do your task here
    }
}

The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.

Also, you can take advantage of @BeforeClass from junit to execute a method before running your tests from that particular class.

ansh
  • 656
  • 4
  • 10