当前位置: 首页 > 知识库问答 >
问题:

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

欧阳安阳
2023-03-14

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

有人能帮忙解决这个错误吗?在这里,我不能单独运行两个测试函数,它们运行得很好,如何一起运行它们

共有1个答案

鲁炳
2023-03-14

您的testcase不应该扩展您想要测试的对象:

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

    }
}

如果运行testcase,将创建CultureMachineTestCases的新对象,该对象还将创建对象CultureMachineTestCases的实例,以此类推。这就是获得StackOverflowException的原因。

 类似资料:
  • 使用: cucumber芯-1.2.4 cucumber-java-1.2.4 cucumber-junit-1.2.4 Junit-4.12 Eclipse Mars.1 Java 8

  • 从很久以前的某个版本开始(大约是1.b.35版),Nutz 就偷偷的加入了 maven 的 pom.xml 文件, 极好的解决了测试时需要依赖其他 jar 包的问题,接下来就让我们使用 Maven 来进行 JUnit 测试吧。 Nutz 在 Github 的 地址为 https://github.com/nutzam/nutz(Git@OSC镜像 https://git.oschina.net/n

  • 现在您已经了解了TestNG及其各种测试,您现在必须担心如何重构现有的JUnit代码。 没有必要担心,因为TestNG提供了一种按照自己的节奏从JUnit转换到TestNG的方法。 您可以使用TestNG执行现有的JUnit测试用例。 TestNG可以自动识别并运行JUnit测试,因此您可以将TestNG用作所有现有测试的运行器,并使用TestNG编写新测试。 您所要做的就是将JUnit库放在Te

  • 问题内容: 我想从命令行运行JUnit测试用例。我怎样才能做到这一点? 问题答案: 对于JUnit 5.x,它是: 查找在简要https://stackoverflow.com/a/52373592/1431016的全部细节https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher 对于JUnit

  • 我创建了一个简单的测试来尝试Junit 5: 这就是我使用的依赖关系: 堆栈跟踪是下一步: 知道哪里出了问题吗?

  • 我已经编写了示例CRUD方法,我已经为服务组件编写了JUnit测试用例,但得到的结果是“地址id没有找到…”当我做测试的时候。 @test public void updateAddressTest()引发ResourceNotFoundException{