【问题标题】:How to write a test class to test my code?如何编写测试类来测试我的代码?
【发布时间】:2013-02-24 11:18:30
【问题描述】:

我想要一个测试类来测试这个类,但我不知道如何编写它,我试图在网上看到但我仍然无法弄清楚。我在 BlueJ 上编写了代码,我正在尝试创建设置游戏.

import java.util.*;

public class Deck
{
    ArrayList<Card> deck;
    public Deck ()
    {
         deck = new ArrayList<Card>();
    }

     public Deck (int capacity)
    {
        deck = new ArrayList<Card>(capacity);
    }

    public int getNumCards ()
    {
        return deck.size();
    }

    public boolean isEmpty () 
    {
        return deck.isEmpty();
    }

    public void add (Card card) 
    {
        deck.add(0,card);
    }

    public Card takeTop() 
    {
        return deck.remove(0);
    }

    public void shuffle ()
    {
        Collections.shuffle(deck);
    }

    public void sort ()
    {
        Collections.sort(deck);
    }

    public String toString ()
    { 
         return (deck.toString()+ "\n");
    }
}

【问题讨论】:

    标签: java arraylist bluej


    【解决方案1】:

    首先你需要决定你需要为你的班级写什么测试用例, 准备好测试用例列表后,您可以使用 Junit 之类的库来创建测试用例。

    这里是几个Junit方法的例子

    import static org.junit.Assert.assertEquals;
    
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    public class MyClassTest {
    
      MyClass tester;
    
      @BeforeClass
      public static void testSetup() {
        tester = new MyClass();
      }
    
      @AfterClass
      public static void testCleanup() {
        // Do your cleanup here like close URL connection , releasing resources etc
      }
    
      @Test(expected = IllegalArgumentException.class)
      public void testExceptionIsThrown() {        
        tester.divide(1000, 0);
      }
    
      @Test
      public void testMultiply() {
        assertEquals("Result", 50, tester.multiply(10, 5));
      }
    } 
    

    【讨论】:

    • 通过将其拉入 @Before 或类似的东西来删除冗余初始化。将正在测试的对象命名为“剪切”是一个很好的约定,imo。
    【解决方案2】:

    使用Junit之类的测试框架,请看下面的示例,

        public class ThingTester extends TestCase
    {
        public ThingTester (String name) 
        {
            super (name);
        }
    
        public static void main(String[] args) 
        {
            TestRunner.runAndWait(new TestSuite(ThingTester.class));
        }
    
        public void testGetName() throws Exception 
        {
            String fileSpec = new String("c:xxxyyyzzz.txt");
            assertEquals("zzz.txt", getName(fileSpec));
        }
    }
    

    【讨论】:

      【解决方案3】:

      您需要创建测试类功能的主要方法。

      public static void main(String args[])
      {
          //To do
      }
      

      在您的主要方法中,您需要例如构造一个 Card 对象(假设您有 Card 类)。

      Card card = new Card();
      

      然后您还需要构造一个 Deck 对象,您将使用它来调用 Deck 类的方法,以便将卡片添加到 Deck 中

      Deck deck = new Deck();
      

      使用deck对象调用add方法将卡片添加到Deck

      deck.add(card);
      

      所以现在你的 main 方法应该是这样的:

      public static void main(String args[])
      {
         Card card = new Card();
         Deck deck = new Deck();
         deck.add(card);
      }
      

      在你的 Deck 课程中,我建议使用List&lt;Card&gt; deck = new ArrayList&lt;Card&gt;(); 而不是ArrayList&lt;Card&gt; deck = new ArrayList&lt;Card&gt;();

      希望这能给你一个起点。

      【讨论】:

      • 测试框架的存在是有原因的。你的类是一个驱动程序,测试意味着断言一个结果。
      • 如果他仍然不知道如何为他的程序创建一个测试类,我觉得将 OP 暴露给他对他没有好处。从 OP 描述中,我认为他需要一个驱动程序类来测试他的程序。
      【解决方案4】:

      我想我不明白你想要什么,但我会在这里给出我的建议。

      卡片类在哪里?

      在你的 Deck 类中添加这个方法,编译你的代码并执行。

      public static void main(String[] args) {
          Deck deck = new Deck();
          // Call your methods here and do what do you want...
      }
      

      【讨论】:

      • 这也是一个驱动程序。你必须断言一个结果。
      【解决方案5】:

      如果您在 Blue Jay 中,您只需右键单击课程,在弹出窗口的底部,将有一个“创建测试课程”选项。使用它可以简化流程。下面我提供了一个 Blue Jay 创建的示例。

      import static org.junit.Assert.*;
      import org.junit.After;
      import org.junit.Before;
      import org.junit.Test;
      
      /**
       * The test class TestOfClass2Test.
       *
       * @author  (your name)
       * @version (a version number or a date)
       */
      public class TestOfClass2Test
      {
          /**
           * Default constructor for test class TestOfClass2Test
           */
          public TestOfClass2Test()
          {
          }
      
          /**
           * Sets up the test fixture.
           *
           * Called before every test case method.
           */
          @Before
          public void setUp()
          {
          }
      
          /**
           * Tears down the test fixture.
           *
           * Called after every test case method.
           */
          @After
          public void tearDown()
          {
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多