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
  • 47,675
  • 117
  • 331
  • 600

1 Answers1

2

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
  • 696
  • 5
  • 10