用JUnit比较两个对象显示出奇怪的行为

时间:2019-02-09 12:41:39

标签: java junit5

我正在为类AbsorptionScheme写一个JUnit测试方法,该方法采用所谓的AbsorptionBlocks并将它们分类为absorptionBlocks数组。我要测试的是排序是否正常。因此,我创建了两个吸收方案对象,并用相同的吸收块填充了它们,但是第一个未排序,第二个已预先排序。然后,我想使用JUnit的assertEquals(Object expected, Object actual)函数来确保两个AbsorptionScheme对象都相同。尽管toSting方法返回的字符串相同,但是JUnit仍然告诉我两个对象都不相同。

这里是JUnits评估:

java.lang.AssertionError: expected: info.rueth.fpucalculator.calc.AbsorptionScheme<Absorption Scheme: (1FPU -> 3h) (2FPU -> 4h) (3FPU -> 5h) (4FPU -> 6h) (6FPU -> 8h)> but was: info.rueth.fpucalculator.calc.AbsorptionScheme<Absorption Scheme: (1FPU -> 3h) (2FPU -> 4h) (3FPU -> 5h) (4FPU -> 6h) (6FPU -> 8h)>
Expected :info.rueth.fpucalculator.calc.AbsorptionScheme<Absorption Scheme: (1FPU -> 3h) (2FPU -> 4h) (3FPU -> 5h) (4FPU -> 6h) (6FPU -> 8h)> 
Actual   :info.rueth.fpucalculator.calc.AbsorptionScheme<Absorption Scheme: (1FPU -> 3h) (2FPU -> 4h) (3FPU -> 5h) (4FPU -> 6h) (6FPU -> 8h)>

我唯一看到的区别是,“期望”字符串的末尾有一个空格,而“实际”字符串的末尾没有空格。

这是我要测试的课程:

package info.rueth.fpucalculator.calc;

import java.util.Arrays;

/**
 * Holds an absorption scheme, connecting FPUs to recommended absorption times.
 */
public class AbsorptionScheme {
    private AbsorptionBlock[] absorptionBlocks;

    AbsorptionScheme() {
        // Create empty array
        absorptionBlocks = new AbsorptionBlock[0];
    }

    /**
     * Adds a new absorption block to the absorption scheme.
     * @param maxFPU The maximum FPU for the absorption time
     * @param absorptionTime The absorption time for that FPU.
     */
    public void addBlock(int maxFPU, int absorptionTime) {
        // Copy existing array into new array with one more empty element at the end ...
        AbsorptionBlock[] newArray = Arrays.copyOf(absorptionBlocks, absorptionBlocks.length + 1);

        // ... and put new AbsorptionBlock into that empty element
        newArray[absorptionBlocks.length] = new AbsorptionBlock(maxFPU, absorptionTime);

        // Sort by maxFPU and replace object variable
        Arrays.sort(newArray, new AbsorptionBlockSorter());
        absorptionBlocks = newArray;
    }

    /**
     * Picks the absorption time associated to the number of FPUs, e.g.:
     * <p>absorptionScheme: 0-1 FPU - 3 hours; 1-2 FPU - 4 hours; 2-3 FPUs - 5 hours; 3-4 FPUs - 6 hours; >4 FPUs - 8 hours</p>
     * <p>The fpu value is commercially rounded to 0 digits, i.e. 2.49 will be rounded to 2, 2.50 will be rounded to 3.</p>
     * <p>If the fpu value is beyond the last scheme block, the time of the last scheme block in the array is returned.</p>
     * 
     * @param fpus The calculated FPUs.
     * @return The associated absorption time.
     */
    public int getAbsorptionTime(double fpus) {
        // Round up the fpus - it's more secure to get a longer insulin interval
        long roundedFPUs = Math.round(fpus);

        // Find associated absorption time
        for (int i = 0; i < absorptionBlocks.length; i++) {
            if (roundedFPUs <= absorptionBlocks[i].getMaxFPU()) {
                return absorptionBlocks[i].getAbsorptionTime();
            }
        }

        // Seems to be beyond the last block, so return time of the last block
        return absorptionBlocks[absorptionBlocks.length - 1].getAbsorptionTime();
    }

    @Override
    public String toString() {
        String returnString = "Absorption Scheme:";
        for (int i = 0; i < absorptionBlocks.length; i++) {
            returnString += " (" + absorptionBlocks[i].getMaxFPU() + "FPU -> " + absorptionBlocks[i].getAbsorptionTime() + "h)";
        }
        return returnString;
    }
}

这是我的测试代码:

    @Test
    public void addBlock() {
        // Create absorption scheme
        AbsorptionScheme absorptionSchemeActual = new AbsorptionScheme();

        // Add absorption blocks - unsorted on purpose to test sorting!
        absorptionSchemeActual.addBlock(2, 4);
        absorptionSchemeActual.addBlock(3, 5);
        absorptionSchemeActual.addBlock(6, 8);
        absorptionSchemeActual.addBlock(1, 3);
        absorptionSchemeActual.addBlock(4, 6);

        // Create another absorption scheme ...
        AbsorptionScheme absorptionSchemeExpected = new AbsorptionScheme();

        // ... and add blocks, this time sorted!
        absorptionSchemeExpected.addBlock(1, 3);
        absorptionSchemeExpected.addBlock(2, 4);
        absorptionSchemeExpected.addBlock(3, 5);
        absorptionSchemeExpected.addBlock(4, 6);
        absorptionSchemeExpected.addBlock(6, 8);

        // Assert both schemes: They should be identical!
        assertEquals(absorptionSchemeExpected, absorptionSchemeActual);
    }

我看不出有什么区别,所以我在做什么错了?

1 个答案:

答案 0 :(得分:0)

//Reads the text file var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); using (var streamReader = new StreamReader(fileStream, Encoding.UTF8)) { //Adds the content of each line to a list string line; while ((line = streamReader.ReadLine()) != null) { destinationEmails.Add(line); } } 方法使用给定对象的assertEquals(Object, Object)检查是否相等。由于尚未在equals()类中覆盖equals(),因此AbsorptionScheme使用equals()的{​​{1}}实现。

java.lang.Object仅在两个对象相同(对象引用相同)时返回assertEquals()。参见下面的Javadoc Object.equals()文本。

  

Object类的equals方法实现了最有区别的   物体上可能的等价关系;也就是说,对于任何非null   参考值x和y,当且仅当x时,此方法返回true   和y指向同一对象(x == y的值为true)。

在您的测试用例中,trueObject.equals()是两个不同的对象。因此absorptionSchemeActual失败。