简体   繁体   English

如何对多个功能进行junit测试

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

I am writing Junit test cases for one java application. 我正在为一个Java应用程序编写Junit测试用例。 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 这是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);
     }
 }

Here flag is set to one in CultureMachineAssignment file. 这里Flag在CultureMachineAssignment文件中设置为1。 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. @Before在每个测试方法之前调用带注释的方法(init())。 You should only have one method annotated with @Before not to get confused. 你应该只有一个用@Before注释的方法,不要混淆。

The code you have implemented in init() should be moved to testVideo() and code from initMethod() should be moved to testLength(). 您在init()中实现的代码应该移动到testVideo(),initMethod()中的代码应该移动到testLength()。

And your init method should look like this to be sure that test class state is the same for all tests: 并且您的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