简体   繁体   English

如何在Intellij中使用junit自动导入测试方法

[英]How to auto import methods for testing with junit in Intellij

I'm trying to learn how to use junit4 for testing in Intellij to practice algorithm questions in CTCI and leetcode. 我正在尝试学习如何使用junit4在Intellij中进行测试,以练习CTCI和leetcode中的算法问题。 However, when I create the test I need to manually import every method to test, rather than importing the whole class and getting all the methods with it. 但是,在创建测试时,我需要手动导入每个方法进行测试,而不是导入整个类并获取所有方法。 Here is an example of a class in my src folder: 这是我的src文件夹中的类的示例:

package leetcode;

public class JewelsAndStones{

  /*Simple brute force method using nested for loops*/

  public static int findJewelsInStonesBruteForce(String J, String S){
    char[] jewelChars = J.toCharArray();
    char[] stoneChars = S.toCharArray();
    int count = 0;
    for (char jewel : jewelChars) {
      for (char stone: stoneChars) {
        if (jewel == stone) {
          count ++;
        }
      }
    }
    return count;
  }
}

and here is the test class located in the test folder: 这是位于测试文件夹中的测试类:

package leetcode;
import org.junit.*;
import static junit.framework.TestCase.assertEquals;
import static leetcode.JewelsAndStones.findJewelsInStonesBruteForce;


public class JewelsAndStonesTest {

  @Test
  public void testFindJewelsInStones() throws Exception {
    String J = "aA";
    String S = "aaAbbbb";
    assertEquals(3, findJewelsInStonesBruteForce(J,S));
  }
  @Test
  public void testFindJewelsInStonesBadInput() throws Exception {

  }
}

So for a class where I may have 3 different solution methods, do I need to do import static leetcode.JewelsAndStones.*additonalMethodToTest* for every one? 因此,对于可能有3种不同解决方法的类,是否需要为每个类import static leetcode.JewelsAndStones.*additonalMethodToTest* using import static leetcode.JewelsAndStones.* or better yet import static leetcode.* does not work. 使用import static leetcode.JewelsAndStones.*或更好的方法import static leetcode.*不起作用。

Set your cursor on the class name, press Alt + Enter and select "Create test" in the pop-up menu. 将光标放在班级名称上,按Alt + Enter ,然后在弹出菜单中选择“创建测试”。 Then check all methods you want to test and Idea will create a test class for you with all methods stubs. 然后检查您要测试的所有方法,Idea将使用所有方法存根为您创建一个测试类。

UPD: I'd like to add that there is no reason to make your methods static at all. UPD:我想补充一点,根本没有理由使您的方法完全静态。 Just public: 只是公开的:

public int findJewelsInStones(String J, String S) {
  ...
}

Then your test code should be something like this: 然后您的测试代码应该是这样的:

@Test
public void testFindJewelsInStones() throws Exception {
  // Given
  JewelsAndStones jewelAndStones = new JewelsAndStone();
  String J = "aA";
  String S = "aaAbbbb";
  int expected = 3;

  // When
  int result = jewelAndStones.findJewelsInStones(J, S);

  // Then 
  assertEquals(expected, result);
}

In this case, you do not need to do a static import of methods. 在这种情况下,您不需要执行静态方法导入。

In addition to this, you also may use a data-driven approach. 除此之外,您还可以使用数据驱动的方法。 Since the leetcode validator use test cases to verify your methods, you can rely on these test-cases as well. 由于leetcode验证器使用测试用例来验证您的方法,因此您也可以依赖这些测试用例。 Here is a small example of a parameterized test with hamcrest matchers: 这是使用hamcrest匹配器进行参数化测试的一个小示例:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@RunWith(Parameterized.class)
public class JewelsAndStonesTest {
    private String J;
    private String S;
    private int expected;

    public JewelsAndStonesTest(String J, String S, int expected) {
        this.J = J;
        this.S = S;
        this.expected = expected;
    }

    @Parameterized.Parameters(name = "{index}: J=\"{0}\", S=\"{1}\", expected result: {2}")
    public static Collection<Object[]> testCases() {
        return Arrays.asList(new Object[][] {
            {"aA", "aaAbbb", 3}, 
            {"z", "ZZ", 0}
            // add more test cases here: positive and negative 
        });
    }

    @Test
    public void jewelsAndStonesTestSuite() {
       // Given
       JewelsAndStones jewelAndStones = new JewelsAndStone();

       // When 
       int result = jewelAndStones.findJewelsInStones(J, S);

       // Then
       assertThat(result, is(equalTo(expected)));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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