需要有关Junit入门的建议 [英] need suggestions on getting started with Junit

查看:128
本文介绍了需要有关Junit入门的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前没有使用过Junit,也没有自动进行单元测试。

I have not used Junit before and have not done unit testing automatically.

场景:
我们正在将后端DAO从Sql Server更改为Oracle。所以在DB端,所有存储过程都转换为oracle。现在,当我们的代码调用这些Oracle存储过程时,我们希望确保返回的数据与sql server存储过程相比是相同的。

Scenario: We are changing our backend DAO's from Sql Server to Oracle. So on the DB side all the stored procedures were converted to oracle. Now when our code calls these thew Oracle Stored Procedures we want to make sure that the data returned is same as compared to sql server stored procedures.

例如,我在DAO中有以下方法:

So for example I have the following method in a DAO:

  //this is old method. gets data from sql server
  public IdentifierBean getHeadIdentifiers_old(String head){
      HashMap parmMap = new HashMap();
      parmMap.put("head", head);
      List result = getSqlMapClientTemplate().queryForList("Income.getIdentifiers", parmMap);
      return (IdentifierBean)result.get(0);
   }
  //this is new method. gets data from Oracle  
  public IdentifierBean getHeadIdentifiers(String head){
      HashMap parmMap = new HashMap();
      parmMap.put("head", head);
      getSqlMapClientTemplate().queryForObject("Income.getIdentifiers", parmMap);
      return (IdentifierBean)((List)parmMap.get("Result0")).get(0);
   }

现在我想编写一个首先调用<$ c的Junit测试方法$ c> getHeadIdentifiers_old 然后 getHeadIdentifiers 并将比较返回的对象(必须覆盖等于和中的哈希值) IdentifierBean )。只有当两个对象相同时,测试才会通过。

now I want to write a Junit test method that would first call getHeadIdentifiers_old and then getHeadIdentifiers and would compare the Object returned (will have to over-write equals and hash in IdentifierBean). Test would pass only when both objects are same.

在测试器方法中,我必须为这两种方法提供一个参数(在这种情况下是头部)。这将暂时手动完成。是的,从前端参数可能不同,SP可能不会返回这些参数的确切结果。但我认为有这些测试用例会让我们感到宽慰,他们会返回相同的数据...

In the tester method I will have to provide a parameter (head in this case) for the two methods..this will be done manually for now. Yeah, from the front end parameters could be different and SPs might not return exact results for those parameters. But I think having these test cases will give us some relief that they return same data...

我的问题是:


  • 这是一个好方法吗?

  • 我将有多个DAO。我是否在DAO
    本身或每个DAO中编写
    测试方法我应该有
    a单独的JUnit测试类?

  • (可能是n00b问题)所有
    测试用例会自动运行吗?我做
    不想去前端点击
    一堆东西以便调用
    DAO被触发。

  • 当测试运行时我会找出
    哪些方法失败了?并且对于
    的失败,它会告诉我测试
    方法失败了吗?

  • 最后,任何好的起点?任何
    教程,显示使用Junit工作
    的文章

  • Is this a good approach?
  • I will have multiple DAO's. Do I write the test methods inside the DAO itself or for each DAO I should have a seperate JUnit Test Class?
  • (might be n00b question) will all the test cases be ran automatically? I do not want to go to the front end click bunch of stuff so that call to the DAO gets triggered.
  • when tests are ran will I find out which methods failed? and for the ones failed will it tell me the test method that failed?
  • lastly, any good starting points? any tutorials, articles that show working with Junit

推荐答案

你将编写一个测试类。

public class OracleMatchesSqlServer extends TestCase {
    public void testHeadIdentifiersShouldBeEqual() throws Exception {
        String head = "whatever your head should be";
        IdentifierBean originalBean = YourClass.getHeadIdentifiers_old(head);
        IdentifierBean oracleBean = YourClass.getHeadIdentifiers(head);
        assertEquals(originalBean, oracleBean);
    }
}

您可能会发现需要在头上参数化这个;这很简单。

You might find you need to parameterize this on head; that's straightforward.

更新:看起来像这样:

public class OracleMatchesSqlServer extends TestCase {
    public void testHeadIdentifiersShouldBeEqual() throws Exception {
        compareIdentifiersWithHead("head1");
        compareIdentifiersWithHead("head2");
        compareIdentifiersWithHead("etc");
    }
    private static void compareIdentifiersWithHead(String head) {
        IdentifierBean originalBean = YourClass.getHeadIdentifiers_old(head);
        IdentifierBean oracleBean = YourClass.getHeadIdentifiers(head);
        assertEquals(originalBean, oracleBean);
    }
}




* Is this a good approach?


当然。


* I will have multiple DAOs. Do I write the test methods inside the DAO
  itself or for each DAO I should have a separate JUnit Test Class?


尝试为每个DAO使用单独的测试类;如果这太繁琐了,那就试试另一种方式,看看你最喜欢什么。拥有单独测试类的细粒度可能更有帮助,但您的里程可能会有所不同。

Try it with a separate test class for each DAO; if that gets too tedious, try it the other way and see what you like best. It's probably more helpful to have the fine-grainedness of separate test classes, but your mileage may vary.


* (might be n00b question) will all the test cases be run automatically?
  I do not want to go to the front end click bunch of stuff so that call
  to the DAO gets triggered.


根据您的环境,将有办法运行所有自动测试。

Depending on your environment, there will be ways to run all the tests automatically.


* when tests are ran will I find out which methods failed? 
  and for the ones failed will it tell me the test method that failed?


是和是。


* lastly, any good starting points? any tutorials, articles that
  show working with Junit


我非常喜欢Dave Astels的书籍

I really like Dave Astels' book.

这篇关于需要有关Junit入门的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆