如何在junit测试中测试比较器 - java

我需要测试此方法-compare()。你能得到建议吗?我能做得更好(如果,否则,如果,否则,所有部分)。

public class AbsFigure {

class AreaCompare implements Comparator<FigureGeneral> {

    @Override
    public int compare(FigureGeneral oneFigure, FigureGeneral twoFigure) {
        double firstValue = oneFigure.area();
        double secondValue = twoFigure.area();
        int result = 0;

        if (firstValue > secondValue)
            result = 1;
        else if (firstValue < secondValue)
            result = -1;
        else
            result = 0;

        return result;
    }
}

在此推荐之后-我们有下一张照片(非常感谢你们!):

public AreaCompare areaCompare = new AreaCompare();

@Test
public void testEqual() {
    FigureGeneral oneFigure = new Rectangle(2.0, 2.0, "triangle");
    FigureGeneral twoFigure = new Rectangle(2.0, 2.0, "rectangle");
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be equal", result == 0);
}

@Test
public void testGreaterThan() {
    FigureGeneral oneFigure = new Triangle(2.0, 2.0, "triangle");
    FigureGeneral twoFigure = new Rectangle(1.0, 1.0, "rectangle");
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be greater than", result >= 1);
}

@Test
public void testLessThan() {
    FigureGeneral oneFigure = new Rectangle(1.0, 1.0, "rectangle");
    FigureGeneral twoFigure = new Triangle(2.0, 2.0, "triangle");
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be less than", result <= -1);

现在一切正常。

参考方案

只需实例化比较器类并传递对象:

public class Test extends TestCase {
    class AreaCompare implements Comparator<FigureGeneral> {

        @Override
        public int compare(FigureGeneral oneFigure, FigureGeneral twoFigure) {
            double firstValue = oneFigure.area();
            double secondValue = twoFigure.area();
            int result = 0;

            if (firstValue > secondValue) {
                result = 1;
            } else if (firstValue < secondValue) {
                result = -1;
            } else {
                result = 0;
            }

            return result;
        }
    }

    private final AreaCompare areaCompare = new AreaCompare();

    @Test
    public void testEqual() {
        FigureGeneral oneFigure = new FigureGeneral();
        FigureGeneral twoFigure = new FigureGeneral();
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be equal", result == 0);
    }

    @Test
    public void testGreaterThan() {
        FigureGeneral oneFigure = new FigureGeneral();
        FigureGeneral twoFigure = new FigureGeneral();
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be greater than", result >= 1);
    }

    @Test
    public void testLessThan() {
        FigureGeneral oneFigure = new FigureGeneral();
        FigureGeneral twoFigure = new FigureGeneral();
        int result = areaCompare.compare(oneFigure, twoFigure);
        assertTrue("expected to be less than", result <= -1);
    }
}

Java-搜索字符串数组中的字符串 - java

在Java中,我们是否有任何方法可以发现特定字符串是字符串数组的一部分。我可以避免出现一个循环。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一个if (some condition to tell wheth…

Java Scanner读取文件的奇怪行为 - java

因此,在使用Scanner类从文件读取内容时,我遇到了一个有趣的问题。基本上,我试图从目录中读取解析应用程序生成的多个输出文件,以计算一些准确性指标。基本上,我的代码只是遍历目录中的每个文件,并使用扫描仪将其打开以处理内容。无论出于何种原因,扫描程序都不会读取其中的一些文件(所有UTF-8编码)。即使文件不是空的,scanner.hasNextLine()在…

Java Globbing模式以匹配目录和文件 - java

我正在使用递归函数遍历根目录下的文件。我只想提取*.txt文件,但不想排除目录。现在,我的代码如下所示:val stream = Files.newDirectoryStream(head, "*.txt") 但是这样做将不会匹配任何目录,并且返回的iterator()是False。我使用的是Mac,所以我不想包含的噪音文件是.DS_ST…

Java RegEx中的单词边界\ b - java

我在使用\b作为Java Regex中的单词定界符时遇到困难。对于text = "/* sql statement */ INSERT INTO someTable"; Pattern.compile("(?i)\binsert\b");找不到匹配项Pattern insPtrn = Pattern.compile(&…

Java:线程池如何将线程映射到可运行对象 - java

试图绕过Java并发问题,并且很难理解线程池,线程以及它们正在执行的可运行“任务”之间的关系。如果我创建一个有10个线程的线程池,那么我是否必须将相同的任务传递给池中的每个线程,或者池化的线程实际上只是与任务无关的“工人无人机”可用于执行任何任务?无论哪种方式,Executor / ExecutorService如何将正确的任务分配给正确的线程? 参考方案 …