简体   繁体   中英

How to do junit testing for multiple functions together

I am writing Junit test cases for one java application. If I run the test functions individually its running fine. When I try to run them together its not running properly

Here is JUnit code

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

Here flag is set to one in CultureMachineAssignment file. Can any one please tell me what I need to do so I can run all the functions inside test file together

@Before annotated method (init()) is called before every test method. You should only have one method annotated with @Before not to get confused.

The code you have implemented in init() should be moved to testVideo() and code from initMethod() should be moved to testLength().

And your init method should look like this to be sure that test class state is the same for all tests:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM