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

运行junit测试用例时出现java.lang.StackOverflowerError,java,eclipse,unit-testing,junit,Java,Eclipse,Unit Testing,Junit,我正在为java应用程序编写junit测试用例 下面是junit测试代码 public class CultureMachineTestCases extends CultureMachineAssignment { CultureMachineTestCases testObj=new CultureMachineTestCases(); @Before public void init() throws IOException{ testObj.i

我正在为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.lang.StackOverflower错误
在cultureMachine.CultureMachineAssignment。(CultureMachine Assignment.java:13)
位于cultureMachine.CultureMachineTestCases.(CultureMachineTestCases.java:11)
位于cultureMachine.CultureMachineTestCases。(CultureMachineTestCases.java:14)
位于cultureMachine.CultureMachineTestCases。(CultureMachineTestCases.java:14)
位于cultureMachine.CultureMachineTestCases。(CultureMachineTestCases.java:14)
栈溢出
在cultureMachine.CultureMachineAssignment。(CultureMachine Assignment.java:13)
位于cultureMachine.CultureMachineTestCases.(CultureMachineTestCases.java:11)
位于cultureMachine.CultureMachineTestCases。(CultureMachineTestCases.java:14)
位于cultureMachine.CultureMachineTestCases。(CultureMachineTestCases.java:14)
位于cultureMachine.CultureMachineTestCases。(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;  
                    }
                }
            }   
        }
包装文化机;
公共类文化机器分配{
HashMap kewordVideo=新HashMap();
HashMap videoKeyword=新HashMap();
HashMap关键字长度=新HashMap();
HashSet videoNames=新的HashSet();
HashSet result=新的HashSet();
public void insertDataIntoSet(){

对于(int i=0;i您的测试用例不应该扩展您想要测试的对象:

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"));

    }
}

如果运行测试用例,将创建一个新的
CultureMachineTases
对象,该对象还将创建一个object
CultureMachineTases
的实例,依此类推。这就是为什么您会得到
StackOverflowException

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

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"));

    }
}

如果运行测试用例,将创建一个新的
CultureMachineTases
对象,该对象还将创建object
CultureMachineTases
的实例,依此类推。这就是为什么会出现
StackOverflowException

我最喜欢的错误,输入相同的方法后,会出现一个永不结束的重复代码它一次又一次地抛出堆栈溢出


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

我最喜欢的错误,你有一个重复的代码,永远不会结束,等等输入相同的方法时,会再次抛出StackOverflow


这是因为你实例化了CultureMachineTases,它有一个实例化CultureMachineTases的字段,等等……你有一个重复的instatnation,当对象被构造时,它在自身内部构造了另一个,这就是next,等等……

我投了反对票,因为这个答案既不能解释OP为什么得到SOE,也不能解释为什么OP得到helps来避免它。如果你能用更多有用的语句更新你的答案…这是CultureMachineTaseSI的循环实例化,因为这个答案既不能解释OP获得SOE的原因,也不能帮助避免它。如果你能用更多有用的语句更新你的答案…这是CultureMachineTaseSI的循环实例化n在上面的代码中,我有一个条件,如果关键字长度超过500,它表示视频超过了最大长度。所以我在那里设置了flag=1。在测试文件中,我添加了“public void testLenth()throws IOException{flagVal=testObj.flag;assertEquals(1,flagVal);}'如果我单独运行此测试,它工作正常,但当我将其与测试文件一起添加时,它没有运行,我如何运行整个测试用例?@user1121210抱歉,我无法获取您的答案。您可以将其作为新问题提问,还是将代码作为更新添加到这些问题中?我已修改了测试文件。我要做的是将两个测试函数单独运行—它们正在运行宁fine@user1121210很抱歉,没有设置标志的代码,我说不出有什么问题。请停止更改回答问题的代码。这可能会使读者将来感到困惑。请在标题更新下添加新代码。标志值的代码正确,因为testLenth()函数工作正常。现在唯一的问题是运行testLenth()和testVideo()函数一起使用。您可以在代码中看到我对一个部分进行了注释,所以testLenth()tun成功。在上面的代码中,我有一个条件,如果关键字长度超过500,则表示视频超过了最大长度。所以我在那里设置了flag=1。在测试文件中,我添加了“public void testLenth()抛出IOException”{flagVal=testObj.flag;assertEquals(1,flagVal);}'如果我单独运行此测试,它工作正常,但当我将其与测试文件一起添加时,它没有运行,我如何运行整个测试用例?@user1121210抱歉,我无法获取您的答案。您可以将其作为新问题提问,还是将代码作为更新添加到这些问题中?我已修改了测试文件。我要做的是将两个测试函数单独运行—它们正在运行宁fine@user1121210很抱歉,没有设置标志的代码,我说不出有什么问题。请停止更改回答问题的代码。这可能会使读者将来感到困惑。请在标题更新下添加新代码。标志值的代码正确,因为testLenth()函数工作正常。现在唯一的问题是运行testLenth()和testVideo()函数一起使用。您可以在代码中看到,我已经对一个部分进行了注释,以便testLenth()成功地完成