Spring not autowiring in unit tests with JUnit

后端 未结 5 732
盖世英雄少女心
盖世英雄少女心 2020-12-03 02:33

I test the following DAO with JUnit:

@Repository
public class MyDao {

    @Autowired
    private SessionFactory sessionFactory;

    // Other stuff here

}
         


        
5条回答
  •  长情又很酷
    2020-12-03 03:00

    I had same problem with Spring Boot 2.1.1 and JUnit 4
    just added those annotations:

    @RunWith( SpringRunner.class )
    @SpringBootTest
    

    and all went well.

    For Junit 5:

    @ExtendWith(SpringExtension.class)
    

    source

提交回复
热议问题