我的Junit测试不起作用

时间:2018-04-09 11:58:41

标签: java unit-testing testing junit

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class TextAnalysisTest { 

        @Test
        public void testNumberRange(){
        //if i put a number between 1-4 here - Junit will pass the test
        //if not - Junit will fail

        int [] input = {1, 2, 3, 4};
        int min = 1;
        int max = 4;
        assertEquals(input).isGreaterThanOrEqualTo(min).isLessThanOrEqualTo(max);

            System.out.println("test finished");
        }


}

我是Java的新手并且真的想学习。有人可以帮我解决这个问题吗?感谢。

3 个答案:

答案 0 :(得分:0)

您必须迭代数组并证明每个值都在最小值和最大值之间。

答案 1 :(得分:0)

输入单个int,而不是array []。
你必须修复assertEquals逻辑。

class TextAnalysisTest {

    @Test
    public void testNumberRange(){    
    int input = 2;
    int min = 1;
    int max = 4;
    assertTrue(input>=min);
    assertTrue(input<=max);
    System.out.println("test finished");
    }

}

答案 2 :(得分:0)

class TextAnalysisTest {

    @Test
    public void testNumberRange(){
    //if i put a number between 1-4 here - Junit will pass the test
    //if not - Junit will fail

    int [] input = {1, 2, 3, 4};
    int min = 1;
    int max = 4;
    for(int k:input){
assertEquals(k).isGreaterThanOrEqualTo(min).isLessThanOrEqualTo(max);


}
 System.out.println("test finished");
    }
}