【问题标题】:Trying ot understand use of @RunWith(SpringJUnit4ClassRunner.class) and @ContextConfiguration(locations= {/dao-context.xml})试图了解 @RunWith(SpringJUnit4ClassRunner.class) 和 @ContextConfiguration(locations= {/dao-context.xml}) 的使用
【发布时间】:2018-04-28 15:36:31
【问题描述】:

我是 Spring 和 Junit 测试的新手。我试图了解 @RunWith(SpringJUnit4ClassRunner.class) 和 @ContextConfiguration(locations= {/dao-context.xml}) 在下面的第 1 行和第 2 行的使用在我的测试中。 dao-context 定义了 datasource 、 entityManagerFactory 和 TranasctionManager bean。

@RunWith(SpringJUnit4ClassRunner.class) //Line 1
@ContextConfiguration(locations= {/dao-context.xml}) //Line2
Public class ProductServiceTest{

@Autowired
private void ProductDao productDao

......
.......
}

【问题讨论】:

    标签: spring-mvc junit mockito spring-config spring-junit


    【解决方案1】:

    为了让单元测试运行批处理作业,框架必须加载 作业的 ApplicationContext。两个注解用于触发 这个:

    @RunWith(SpringJUnit4ClassRunner.class):表示该类 应该使用 Spring 的 JUnit 工具

    @ContextConfiguration(locations = {...}):指明哪些 XML 文件 包含 ApplicationContext。

    this official documentation 上查看更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      • 2013-06-03
      • 2017-07-05
      • 2018-01-23
      • 2013-01-27
      • 2021-05-05
      • 2011-01-26
      相关资源
      最近更新 更多