简体   繁体   English

只需为Java中的Integer.valueOf()编写Junit

[英]Only Need to write Junit for Integer.valueOf() in Java

So here is the thing, I found new Integer() in my code and which is deprecated now I planed to remove that pic of code (new Integer()) to Integer.valueOf() but as company rules I have to write the Junit for that change, I just need to verify that pic of code no need to write whole coverage of the method, Now I am getting confused, how I can do it. 这就是问题所在,我在代码中找到了新的Integer(),现在不推荐使用,我打算将代码(new Integer())的图片删除为Integer.valueOf(),但是根据公司规则,我必须编写Junit对于该更改,我只需要验证代码的图片,而无需编写该方法的整个覆盖范围,现在我很困惑如何实现此目的。

Before: 之前:

@SuppressWarnings({ "rawtypes", "unchecked" })
    public List getLogsByLcrErrorCodeInLast(String code, long period) throws Exception {
        Object params = new ObjectPair(new Integer(code), new MesDbUtil(ibatis).getPastMesDbDate(period));
        List mesdbLogs = client.queryForList("LcrLog.getLogsByLcrErrorCodeInLast", params);
        Iterator mesdbLogsIter = mesdbLogs.iterator();
        List results = new ArrayList();
        while (mesdbLogsIter.hasNext()) {
            MesDbLcrLog mesdbLog = (MesDbLcrLog) mesdbLogsIter.next();
            results.add(mesdbLog.getLog());
        }
        return results;
    }

After:: 后::

@SuppressWarnings({ "rawtypes", "unchecked" })
    public List getLogsByLcrErrorCodeInLast(String code, long period) throws Exception {
        Object params = new ObjectPair(Integer.valueOf(code), new MesDbUtil(ibatis).getPastMesDbDate(period));
        List mesdbLogs = client.queryForList("LcrLog.getLogsByLcrErrorCodeInLast", params);
        Iterator mesdbLogsIter = mesdbLogs.iterator();
        List results = new ArrayList();
        while (mesdbLogsIter.hasNext()) {
            MesDbLcrLog mesdbLog = (MesDbLcrLog) mesdbLogsIter.next();
            results.add(mesdbLog.getLog());
        }
        return results;
    }

I tried to do like following, I don't know is it correct or not, and need to go for some professional way for it too because of code review; 我试图做下面的事情,我不知道它是否正确,由于代码审查,也需要采取一些专业的方法。

public class integerValueOf(){
    private Integer code=23;
    @Test
        public void testGetLogsByLcrErrorCodeInlast() throws Exception{
            assertEquals(new Integer(code), Integer.valueOf(code));
        }
    }

Are you unit testing every other API in the JRE? 您是否要对JRE中的所有其他API进行单元测试? No? 没有? Why would you unit test Integer.valueOf(...) then? 那么为什么要对Integer.valueOf(...)进行单元测试?

Your existing unit tests for getLogsByLcrErrorCodeInLast should cover the change in the integer instantiation. 现有的getLogsByLcrErrorCodeInLast单元测试应涵盖整数实例化中的更改。 Eg you have tests for different values of code and check that you get the expected params in client.queryForList(...) . 例如,您对不同的code值进行了测试,并检查是否在client.queryForList(...)获得了预期的params

If you don't have the tests, then you need to write them before you change the code. 如果您没有测试,则需要先编写它们,然后再更改代码。

Edit: I didn't even notice it earlier, but your production code converts a String to an Integer and your sugested unit test converts an Integer to an Integer. 编辑:我什至没有注意到它,但是您的生产代码将String转换为Integer,而经过汇总的单元测试将Integer转换为Integer。 You're idea is fundamentally flawed because it doesn't test the code that you execute in production. 您的想法从根本上来说是有缺陷的,因为它不会测试您在生产中执行的代码。 It lets errors like this pass testing with green flags. 它可以使此类错误通过带有绿色标志的测试。

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

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