【问题标题】:How to do junit testing for multiple functions together如何一起对多个功能进行junit测试
【发布时间】:2015-05-27 12:00:30
【问题描述】:

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

这里的标志在 CultureMachineAssignment 文件中设置为 1。 谁能告诉我我需要做什么,以便我可以一起运行测试文件中的所有功能

【问题讨论】:

    标签: java eclipse unit-testing junit


    【解决方案1】:

    @Before 注释方法 (init()) 在每个测试方法之前调用。您应该只使用@Before 注释的一种方法,以免混淆。

    您在 init() 中实现的代码应移至 testVideo(),而 initMethod() 中的代码应移至 testLength()。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-01
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多