How to reuse a method and test in JUnit?

I tried to avoid code duplication in the JUnit test, but I was kind of stuck.

This is my first test, for the second it has exactly the same methods, but different services (different input data). instead of TestCaseResourceTest1 , I have TestCaseResourceTest2 . Now, what could be the right way to test both? I want to have a separate file for test number 2, how do I avoid code duplication? (e.g. use beforeFileTest () method)

public class TestCaseResourceTest1 {

    @Mock
    private TestService testService;
    @Mock
    private AreaService areaService;

    private TestCaseService1 testCaseService1; // is changed in test2

    @Before
    public void before() throws Exception{
        testCaseService1 = mock(TestCaseService1.class); // is changed in test2
        MockitoAnnotations.initMocks(this);
        beforeFileTest();
    }

    private void beforeFileTest() throws Exception{
        doReturn(true).when(areaService).chechExists(any(String.class), eq(false));
    }

    @Test
    public void verifyFileExists() throws Exception{
        verifyOtherArea(testCaseService1); // is changed in test2
        doReturn(false).when(areaService).chechExists(any(String.class), eq(false));
    }
}

just the comment lines is changed in test2are the differences.

Tpx

+4
source share
5 answers

Given this excerpt from your question:

... TestCaseResourceTest1 TestCaseResourceTest2... 2

... :

  • Test Suite ( @BeforeClass @AfterClass). (1) ( ); (2) / (3) . :

    @RunWith(Suite.class)
    @Suite.SuiteClasses({
        TestCaseResourceTest1.class,
        TestCaseResourceTest2.class
    )}
    public class TestSuiteClass {
    
        @BeforeClass
        public void setup() {
            beforeFileTest();
        }
    
        private void beforeFileTest() throws Exception {
            // ...
        }
    }
    
  • , TestCaseResourceTest1 TestCaseResourceTest2, ( super()). , - (1) (2) /

  • JUnit, @RunWith(YourCustomRunner.class).

, ; , , .

+2

, ( , ), , , .

- :

public abstract class TestCaseResourceTest {

  protected abstract TestCaseService1 getServiceToTest();

  @Before
  public void before() throws Exception {
    testCaseService1 = getServiceToTest();
    MockitoAnnotations.initMocks(this);
    beforeFileTest();
  }

  @Test
  public void test() {
    // do your test here
  }
}

public class ConcreteTest extends TestCaseResourceTest {
  protected abstract TestCaseService1 getServiceToTest() {
    return new TestCaseService();
  }
}

public class ConcreteTest2 extends TestCaseResourceTest {
  protected abstract TestCaseService1 getServiceToTest() {
    return new DifferentService();
  }
}
+3

, . , , . , , , , .

?

  • .
  • . .
  • .
+3

, , . @Before beforeFileTest.

, , , , , , .

, , , -, , . , JunitParams: https://github.com/Pragmatists/junitparams/wiki/Quickstart

0

JUnit 5 http://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests?

. , , JUnit 5:

@ParameterizedTest
@ValueSource(strings = { "Hello", "World" })
void testWithStringParameter(String argument) {
    assertNotNull(argument);
}

, :

@ParameterizedTest
@MethodSource("stringProvider")
void testWithSimpleMethodSource(String argument) {
    assertNotNull(argument);
}

static Stream<String> stringProvider() {
    return Stream.of("foo", "bar");
}

, .

If you are using Maven, you can add these dependencies to start using JUnit 5:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <version>5.0.0-RC2</version>
    <scope>test</scope>
</dependency>

The only nasty thing in JUnit 5 is that it has not yet been released.

0
source

Source: https://habr.com/ru/post/1683176/


All Articles