该类的java junit测试用例

标签 java unit-testing testing junit testcase

我的程序如下:

   import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

public class MangoDemo
{

    public static void main(String[ ] args) throws FileNotFoundException
     {
        output();

      }

        public static void output() throws FileNotFoundException{
             char[] lineChar = null;

                Scanner scanner = new Scanner(new File("src/input.txt"));
                while(scanner.hasNextLine()){
                    String line = scanner.nextLine();
                    //System.out.println(line);
                lineChar = line.toCharArray();
                    sortStringBubble (lineChar);
                     int n = lineChar.length-1;
                     System.out.print(lineChar[n--]);
                     for (;  n >=0;  n-- ) {
                       if(lineChar[n] != ',') {
                         System.out.print(",");
                         System.out.print(lineChar[n]);
                       }
                     }
                     System.out.println();

             }//end oof while loop
        }

      public static void sortStringBubble( char  x [ ] )
      {
            int j;
            boolean flag = true;  // will determine when the sort is finished
            char temp;

            while ( flag )
            {
                  flag = false;
                  for ( j = 0;  j < x.length - 1;  j++ )
                  {

                          if ( Character.toString(x[j]).compareToIgnoreCase( Character.toString(x[j+1]) ) > 0 )
                          {                                             // sorting in ascending order
                                      temp = x[j];
                                      x [j] = x [j+1];     // swapping here 
                                      x [j+1] = temp; 
                                      flag = true;
                           } 


                   } 
            } 
      } 
}

我尝试制作测试用例,这里是:

import static org.junit.Assert.*;

import org.junit.Test;


public class MangoDemoTest {

    @Test
    public void testoutput() {
        MangoDemo m = new MangoDemo();
        char[] a = {'a','b','c'};
        assertEquals("Result: ","c,b,a",m.output());
    }

    @Test
    public void testSortStringBubble() {
        fail("Not yet implemented");
    }

}

我对这个绝对是新手。我也收到错误消息。有没有办法在不给输出函数参数的情况下测试输出?有人可以帮忙吗?

谢谢

最佳答案

您的 void output() 方法被声明为返回 void。这意味着它什么都不返回。但在下面的代码中,您希望它返回 "c,b,a"。您要么必须更改 output() 以像 String output() 那样返回 String,要么检查副作用,即输出的任何更改正在方法中执行。

assertEquals("Result: ","c,b,a", m.output());

编写可测试的代码也很重要。阅读本文 Writing Testable Code

关于该类的java junit测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8996798/

相关文章:

java - 不偶尔发送证书

java - 非常小的程序来提高编程技能?

symfony - Symfony WebTestCase 只是内部的吗?

android - python ,猴跑者。如何在所有列表包中找到包名?

php - Laravel 路由和 Controller 测试

java - 执行一对一映射时,Hibernate 不会填充第二个表

java - 你将如何在 Java 中实现 LRU 缓存?

c# - 模拟 rabbitMQ IModel 验证错误

c++ - 如何使用 Google Test 隐藏行号

javascript - 在 Node Tools Visual Studio (NTVS) 中调试单元测试