简体   繁体   English

如何在junit测试中跟踪值?

[英]How to trace the values in a junit test?

I am running a simple test case where I test if there is no id in the db and i get an expected exception back. 我正在运行一个简单的测试用例,其中测试数据库中是否没有id,并且返回预期的异常。

But it does not work. 但这行不通。 So my question is how to trace the values of a test in junit? 所以我的问题是如何在junit中跟踪测试的值? Any good solutions? 有什么好的解决方案吗?

I appreaciate your answer!!! 我感谢您的回答!!!

UPDATE: test case: 更新:测试用例:

@Test(expected = SQLException.class)
public void testDeleteID(){
    ArrayList<Produkt> queryResult=new ArrayList<Produkt>();
    shandler.deleteProdukt(666);
    queryResult=shandler.findAll();

}

IDE: eclipse IDE:日食

Are you using IDE such as Eclipse? 您是否正在使用诸如Eclipse之类的IDE? Add a break point, and run as Debug. 添加一个断点,并作为调试运行。

I think you try to catch a nested Exception. 我认为您尝试捕获嵌套的Exception。 Change your code to that and take a look in the stacktrace for the concrete exception: 将您的代码更改为该代码,并查看stacktrace中的具体异常:

@Test(expected = Exception.class) 
public void testDeleteID(){
 ArrayList<Produkt> queryResult=new ArrayList<Produkt>();
 shandler.deleteProdukt(666);
 queryResult=shandler.findAll();  
} 

Another way: remove the "expected" and surround the database operation with a try-catch block. 另一种方法:删除“ expected”,并用try-catch块包围数据库操作。

@Test
public void testDeleteID(){
 ArrayList<Produkt> queryResult=new ArrayList<Produkt>();
 try{
   shandler.deleteProdukt(666);
   queryResult=shandler.findAll();  
   assertTrue(false);
 }catch(Exception ex){
   assertTrue(true);
 }
} 

but the first one is the better solution. 但是第一个是更好的解决方案。 I think you catch the wrong exception. 我认为您发现了错误的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM