How to autowire field in static @BeforeClass?

后端 未结 5 1307
天命终不由人
天命终不由人 2021-01-31 07:56
@RunWith(SpringJUnit4ClassRunner.class)
public void ITest {
    @Autowired
    private EntityRepository dao;

    @BeforeClass
    public static void init() {
        da         


        
5条回答
  •  野性不改
    2021-01-31 08:40

    With Junit 5 you can do this (@BeforeAll instead of @BeforeClass)

    public void ITest {
        @Autowired
        private EntityRepository dao;
    
        @BeforeAll
        public static void init(@Autowired EntityRepository dao) {
            dao.save(initialEntity); //possible now as autowired function parameter is used
        }
    }
    

    By leaving the field it means it can be used in other tests

提交回复
热议问题