簡體   English   中英

如何對多個功能進行junit測試

[英]How to do junit testing for multiple functions together

我正在為一個Java應用程序編寫Junit測試用例。 如果我單獨運行測試功能,它運行正常。 當我嘗試將它們一起運行時,它運行不正常

這是JUnit代碼

 public class CultureMachineTestCases{

     CultureMachineAssignment testObj=new CultureMachineAssignment ();
     HashSet<String> result =new HashSet<String>();
     String answer,answer1;
     int flagVal;
     @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);
     }
 }

這里Flag在CultureMachineAssignment文件中設置為1。 任何人都可以告訴我我需要做什么,這樣我就可以一起運行測試文件中的所有功能

@Before在每個測試方法之前調用帶注釋的方法(init())。 你應該只有一個用@Before注釋的方法,不要混淆。

您在init()中實現的代碼應該移動到testVideo(),initMethod()中的代碼應該移動到testLength()。

並且您的init方法應如下所示,以確保所有測試的測試類狀態相同:

@Before
public void init(){
  answer = null;
  answer1 = null;
  flagVal = -1;
  result = new HashSet<String>();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM