【问题标题】:Convert an int to Hex and check the assertion. Java AT将 int 转换为 Hex 并检查断言。 Java AT
【发布时间】:2021-10-08 19:24:49
【问题描述】:

我的任务是编写一个使用 Integer toHexString 并断言 11 变为 b 的 @Test 注释方法

这就是我尝试获取非法表达式开头的方法:

import org.junit.Test;
import org.junit.Assert;

public class HexTest {
    @Test
    public static void main (String[] args){
        static Integer.toHexString(11){
            Assert.assertEquals("int to Hex", 'b', 11);
        }
    }
}

如果有人能告诉我哪里出了问题以及如何解决,我将不胜感激。

【问题讨论】:

  • static Integer.toHexString(11){ 应该是什么?此外,Assert.assertEquals("int to Hex", 'b', 11); 将检查 11 是否等于 'b',但事实并非如此,因此您的测试永远不会通过。您应该调用要测试的方法,将要转换的值作为参数传递给它,将转换结果存储在变量中,并检查该变量是否等于您的预期结果。
  • 1)static Integer.toHexString(11) 应该是Integer-toHexString方法的调用。应该如何正确调用? 2)这是测试人员书中的一项任务 - 检查 11 是否等于 'b' - 了解静态方法和整数。
  • 您不需要static 关键字来调用静态方法。我已经回答了这个问题。
  • 谢谢!我解决了语法问题,代码是正确的。虽然我不明白我是如何得到 98 的,但我必须做什么才能让 11 变为十六进制 java.lang.AssertionError: int to Hex Expected :98 Actual :11

标签: java testing automation integer hex


【解决方案1】:

固定:

import org.junit.Assert;
import org.junit.Test;

public class HexTest {
    @Test
    public void integerToHexStringTest() { // test method definition
        // GIVEN
        String expected = "b";
        
        // WHEN 
        String actual = Integer.toHexString(11); // call method `toHexString(11)`

        // THEN
        Assert.assertEquals("int to Hex", expected, actual);
    }
}

【讨论】:

    【解决方案2】:

    问题是您不需要static 关键字来调用静态方法。您应该做的是调用该方法,将结果存储在一个变量中并检查该变量是否包含您期望的值:

    import org.junit.Test;
    import org.junit.Assert;
    
    public class HexTest {
        @Test
        public void testToHexString(){
            String result = Integer.toHexString(11);
            Assert.assertEquals("int to Hex", "b", result);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-09
      • 2016-03-23
      • 2016-04-15
      • 2019-06-04
      • 2014-04-17
      • 1970-01-01
      • 2011-06-02
      • 2016-01-22
      • 1970-01-01
      相关资源
      最近更新 更多