【问题标题】:Spring/Junit accessing a sibling classSpring/Junit 访问同级类
【发布时间】:2012-03-09 14:58:02
【问题描述】:

运行我的测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:eq-mo-dcc-context-block.xml"})

public class SpringTest {

@Autowired
private DateUtils dateManager;

@Autowired
private RegionFilter filter;

@Autowired
private ApplicationContext appContext;

@Test
public void test_This()
{
    Object obj = appContext.getBean("BlockTransformer");
    BlockTransformer Trans = (BlockTransformer) obj;


}

所有非常早期的阶段,但是配置文件出现的问题是从其他 Maven 模块/项目访问 bean。但是我在 src/test/resources 下拥有所有 xml,因此它可以获取 xml。然而,当我试图拿起我得到的类文件时:

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [dcc.mdp.DefaultMessageListener] for bean with name 'BlockMessageListener' defined in class path resource [eq-mo-dcc-context-block.xml]; nested exception is java.lang.ClassNotFoundException: dcc.mdp.DefaultMessageListener

此类驻留在同级 maven 模块中,但无法从测试类中获取。如何让类知道来自其他模块而不是它自己的类?

【问题讨论】:

    标签: java spring testing maven junit


    【解决方案1】:

    Maven 模块不会自动“了解”同一父项目下的其他模块的任何信息,因此包含您想要的类的兄弟模块需要在包含您的测试类的模块中列为依赖项。

    【讨论】:

    • 您需要显式定义依赖项才能运行此测试套件。
    猜你喜欢
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多