在评估时进行调试

时间:2014-06-03 07:01:29

标签: java debugging jvm

在评估任意表达式时,在IDE评估对话框中,是否有可能在断点上停止执行?

假设我评估myClass.myMethod(),我设置了断点,并且断点被击中。

如果可能的话,我想这是一个JVM问题,而不是IDE。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

假设您正在使用Spring。然后你可能已经购买了整个Martin Fowler et al Layering模式。

我使用IntelliJ和JUnit,以便适应您的需求。

我的每个IntelliJ模块(图层)测试根的根目录都有一个基类:

/**
 * An abstract base class that other tests can inherit from to 
 * get spring functionality.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/spring-config-domain.xml"})
public abstract class SpringAwareTest {
}

然后,您的测试可以简单地将其扩展为"上下文感知"。

public class NavigationServiceTest extends SpringAwareTest {
    @Autowired private NavigationService service;

    @Test
    public void folders() throws IOException {
        List<Folder> folders = service.getFolders(TestConstants.USER_ID);
        Assert.assertNotNull("Folders is null", folders);
    }

使用更高级别的测试构建每个层(模块),例如在服务层:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/spring-config-service.xml"})
public abstract class SpringAwareTest {
}

并导入Spring配置文件中的较低级别图层(无论如何,您需要在应用程序中执行此操作):

<import resource="spring-config-dal.xml"/>
<import resource="spring-config-security.xml"/>
...

现在你可以设置断点并逐步完成你的心灵内容;所有这些都有完整的背景。

如果您只想测试一些不需要上下文的代码(或单步执行)。然后按照正常情况进行测试(不要延长SpringAwareTest)。这将更快,因为它不需要提出上下文的滞后。当你转到持续集成又名詹金斯等时,这开始变得很重要。