【问题标题】:java.lang.StackOverflowError while running junit test case运行junit测试用例时出现java.lang.StackOverflowError
【发布时间】:2015-05-27 10:25:26
【问题描述】:

我正在为 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;  
                    }
                }
            }   
        }

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

【问题讨论】:

    标签: java eclipse unit-testing junit


    【解决方案1】:

    您的测试用例不应扩展您要测试的对象:

    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 的原因。

    【讨论】:

    • 在上面的代码中,我有一个条件,如果关键字长度超过 500,它会说“视频的最大长度超过了”。所以我在那里设置了 flag =1 。在测试文件中,我添加了`public void testLenth() throws IOException{ flagVal=testObj.flag; assertEquals(1, flagVal); } ` 如果我单独运行它,它工作正常,但是当我用测试文件添加它时它没有运行我如何运行整个测试用例?
    • @user1121210 抱歉,我无法联系到您。您可以将其作为新问题提出或将代码添加为这些问题的更新吗?
    • 我已经修改了测试文件我想要做的是同时运行这两个测试功能单独它们运行良好
    • @user1121210 抱歉,没有设置标志的代码,我无法说出问题所在。并停止更改已回答问题的代码。也许将来这个困惑的读者。请在标题 UPDATE 下添加新代码。
    • 标志值的代码是正确的,因为 testLenth() 函数工作正常,现在唯一的问题是同时运行 testLenth() 和 testVideo() 函数。你可以在代码中看到我已经注释了一个部分,所以 testLenth() tuns 成功
    【解决方案2】:

    我最喜欢的错误,你有一个重复出现的代码,它永远不会结束,并且在一次又一次地输入相同的方法之后它会抛出 StackOverflow。

    这是因为你实例化了 CultureMachineTestCases,它有一个实例化 CultureMachineTestCases 的字段,等等……你有重复的实例化,当对象被构造时,它会在自身内部构造另一个对象,这会产生下一个,等等。 ..

    【讨论】:

    • 我投了反对票,因为这个答案既没有解释为什么 OP 获得了 SOE,也没有帮助避免它。如果您可以用更有用的陈述更新您的答案...
    • CultureMachineTestCases 的重复实例化
    猜你喜欢
    • 2023-03-06
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多