运行 junit 测试用例时出现 java.lang.StackOverflowError

标签 java eclipse unit-testing junit

我正在为 java 应用程序编写 junit 测试用例 这是junit测试代码

public class CultureMachineTestCases extends CultureMachineAssignment {

    CultureMachineTestCases testObj=new CultureMachineTestCases();

    @Before
    public void init() throws IOException{
        testObj.insertDataIntoSet();
        testObj.addKeywords("video1");

    }

    /*@Test
public void testVideo() throws IOException {
    result=testObj.search("abcd");
    answer=result.toString();
    answer1=answer.replaceAll("[^a-z0-9]","");

     assertEquals("video1", answer1);

}
@Before
public void initMethod() throws IOException{
    testObj.insertDataIntoSet();
    testObj.addKeywords("video2");
 }   */ @Test
  public void testLenth() throws IOException{
    flagVal=testObj.flag;

    assertEquals(1, flagVal);

    }
}

在 eclipse 中运行此代码后出现以下错误

 java.lang.StackOverflowError
    at cultureMachine.CultureMachineAssignment.<init>     (CultureMachineAssignment.java:13)
    at cultureMachine.CultureMachineTestCases.<init>(CultureMachineTestCases.java:11)
     at cultureMachine.CultureMachineTestCases.<init>(CultureMachineTestCases.java:14)
     at cultureMachine.CultureMachineTestCases.<init>(CultureMachineTestCases.java:14)
    at cultureMachine.CultureMachineTestCases.<init>(CultureMachineTestCases.java:14)
 java.lang.StackOverflowError
    at cultureMachine.CultureMachineAssignment.<init>     (CultureMachineAssignment.java:13)
    at cultureMachine.CultureMachineTestCases.<init>(CultureMachineTestCases.java:11)
    at cultureMachine.CultureMachineTestCases.<init>(CultureMachineTestCases.java:14)
    at cultureMachine.CultureMachineTestCases.<init>(CultureMachineTestCases.java:14)
    at cultureMachine.CultureMachineTestCases.<init>(CultureMachineTestCases.java:14)

这是我的主要java代码

package cultureMachine;
        public class CultureMachineAssignment {

            HashMap<String,HashSet<String>> kewordVideo = new HashMap<String,HashSet<String>>();
            HashMap<String,HashSet<String>> videoKeyword =  new         HashMap<String,HashSet<String>>();
            HashMap<String,Integer> keywordLength = new HashMap<String,Integer>();

            HashSet<String> videoNames = new HashSet<String>();
            HashSet<String> result = new HashSet<String>();

            public void insertDataIntoSet(){
                for(int i=0;i<500;i++){
                    videoNames.add("video"+i);
                }
            }
            public void addKeywords(String video)throws IOException{


                InputStreamReader ip = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(ip);

                Integer previousVal=0;

                if(!videoKeyword.containsKey(video) && videoNames.contains(video)){
                    videoKeyword.put(video,new HashSet<String>());
                }
                else if(!videoNames.contains(video)){
                    System.out.println("Video is not a part of lookup");
                }

                System.out.println("Enter keywords for video");
                String keyword =br.readLine();

                if(!keywordLength.containsKey(video))
                    keywordLength.put(video, 0);

                if((keywordLength.get(video)+keyword.length())<500){
                    videoKeyword.get(video).add(keyword);
                    previousVal=keywordLength.get(video);
                    keywordLength.put(video, previousVal+keyword.length());
                }
                else{
                    System.out.println("Maximum length exceeded for video "+ video);
                }
                if(!kewordVideo.containsKey(keyword)){
                    kewordVideo.put(keyword,new HashSet<String>());
                }
                kewordVideo.get(keyword).add(video);
            }

            public HashSet search(String searchKey){
                for (Entry<String, HashSet<String>> entry : videoKeyword.entrySet()) {
                    for (String s : entry.getValue()) {
                        if (s.contains(searchKey)) {
                            result.add(entry.getKey());
                        break;
                        }
                    }
                }
                return result;
            }

            public static void main(String args[]) throws IOException {

                CultureMachineAssignment obj1 = new CultureMachineAssignment();
                HashSet<String> searchResults = new HashSet<String>();
                int num=0;
                InputStreamReader ip = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(ip);
                obj1.insertDataIntoSet();
                Scanner in = new Scanner(System.in);
                while(num!=3){
                    System.out.println();
                    System.out.println("Please enter your choice");
                    System.out.println("1. To assign keyword to video");
                    System.out.println("2. To Search Video using keyword");
                    System.out.println("3. Exit");

                    num=in.nextInt();

                    switch(num)
                    {
                    case 1 :
                        System.out.println("Enter Video name[video followed by video number]");
                        String video =br.readLine();
                        if(obj1.videoNames.contains(video))
                            obj1.addKeywords(video);
                        else
                            System.out.println(video+" is not a part of lookup");
                        break;
                    case 2 :
                        System.out.println("Enter partial or complete keyword to search video");
                        String searchKey = br.readLine();
                        searchResults=obj1.search(searchKey);
                        System.out.println(searchResults);
                        break;  
                    case 3:
                        System.out.println("exiting from application");
                        break;  
                    default:
                        System.out.println("Please enter correct choice");
                        break;  
                    }
                }
            }   
        }

有人可以帮忙解决这个错误吗? 在这里我无法单独运行两个测试功能,它们运行良好 如何一起运行它们

最佳答案

你的测试用例不应该扩展你想测试的对象:

public class CultureMachineTestCases{

    CultureMachineAssignment testObj=new CultureMachineAssignment ();

    @Before
    public void init() throws IOException{
        testObj.insertDataIntoSet();
        testObj.addKeywords("video1");

    }

    @Test
    public void testVideo() throws IOException {
         assertEquals("video1", testObj.search("abcd"));

    }
}

如果您运行测试用例,将创建一个新的 CultureMachineTestCases 对象,该对象还将创建对象 CultureMachineTestCases 的实例,依此类推。这就是您得到 StackOverflowException 的原因。

关于运行 junit 测试用例时出现 java.lang.StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30479745/

相关文章:

java - JMockit 与 null 匹配

java - toString() 在不相关的更改后不起作用。我在 WEKA、Java 中找不到 toString()

android - 我怎样才能让背景的方向在屏幕旋转时保持纵向前夕

c# - 如何在没有 Moles 或 Isolator 的情况下对使用静态类的方法进行单元测试?

android - ant可以使用Eclipse创建的build.xml吗?

c++ - 如何在 Eclipse Indigo CDT C++ 中设置 libxml

javascript - 如何在 Jest 中测试基于计时器的 RxJS 可观察量

java - 在哪里可以找到 jpa orm.xml 使用示例

java - ListView细胞工厂: wrong font weight

java - Groovy 中字符串的增量运算符