简体   繁体   中英

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. 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:

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? using import static leetcode.JewelsAndStones.* or better yet import static leetcode.* does not work.

Set your cursor on the class name, press Alt + Enter and select "Create test" in the pop-up menu. Then check all methods you want to test and Idea will create a test class for you with all methods stubs.

UPD: I'd like to add that there is no reason to make your methods static at all. 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. Here is a small example of a parameterized test with hamcrest matchers:

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)));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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