Java 如何一起对多个函数进行junit测试

Java 如何一起对多个函数进行junit测试,java,eclipse,unit-testing,junit,Java,Eclipse,Unit Testing,Junit,我正在为一个java应用程序编写Junit测试用例。 如果我单独运行测试函数,它运行良好。 当我试着把它们放在一起运行时,它运行不正常 下面是JUnit代码 public class CultureMachineTestCases{ CultureMachineAssignment testObj=new CultureMachineAssignment (); HashSet<String> result =new HashSet<String>

我正在为一个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);
     }
 }
公共类CultureMachineTases{
CultureMachine Assignment testObj=新的CultureMachine Assignment();
HashSet result=新的HashSet();
串答,答1;
int flagVal;
@以前
public void init()引发IOException{
testObj.insertDataIntoSet();
testObj.addKeywords(“video1”);
}
@试验
public void testVideo()引发IOException{
结果=测试对象搜索(“abcd”);
答案=result.toString();
answer1=答案。替换全部(“[^a-z0-9]”,“”);
资产质量(“视频1”,回答1);
}
@以前
public void initMethod()引发IOException{
testObj.insertDataIntoSet();
testObj.addKeywords(“video2”);
}    
@试验
public void testLenth()引发IOException{
flagVal=testObj.flag;
资产质量(1,flagVal);
}
}
此处,CultureMachine分配文件中的标志设置为1。 在每个测试方法之前调用带注释的方法(init())之前,请任何人告诉我需要做什么,以便我可以一起运行测试文件中的所有函数。您应该只有一个方法用@Before注释,以免混淆

在init()中实现的代码应该移动到testVideo(),而initMethod()中的代码应该移动到testLength()

您的init方法应该如下所示,以确保所有测试的测试类状态都相同:

@Before
public void init(){
  answer = null;
  answer1 = null;
  flagVal = -1;
  result = new HashSet<String>();
}
@之前
公共void init(){
答案=空;
answer1=null;
flagVal=-1;
结果=新的HashSet();
}