使用junit 4.10进行单元测试

时间:2012-06-09 20:37:23

标签: unit-testing junit4

在这里使用JUnit 4.10是我的测试。

package geometry;

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

public class SegmentTest extends junit.framework.TestCase {

    @Test
    public void SegmentParallelTest() {
        Segment a = new Segment(10, 5, 20, 11);
        Segment b = new Segment(20, 6, 30, 11);
        assertFalse(a.isParallel(b));
    }

    public static junit.framework.Test suite() {
        return new junit.framework.JUnit4TestAdapter(SegmentTest.class);
    }

    public static void main(String args[]) {
        org.junit.runner.JUnitCore.main("geometry.SegmentTest");
    }
}

哪会产生以下错误消息......

junit.framework.AssertionFailedError: No tests found in geometry.SegmentTest
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

这种方法有什么问题?

1 个答案:

答案 0 :(得分:6)

您正在扩展TestCase,希望测试方法的名称以test开头。删除extends junit.framework.TestCase,它将正常工作。

package geometry;

import org.junit.Test;
import static org.junit.Assert.assertFalse;

public class SegmentTest {

    @Test
    public void segmentParallelTest() {
        Segment a = new Segment(10, 5, 20, 11);
        Segment b = new Segment(20, 6, 30, 11);
        assertFalse(a.isParallel(b));
    }

}