Похоже, мы не можем запустить тест Junit с несколькими бегунами.В моем случае мне нужны и RandomizedRunner (чтобы иметь возможность использовать / расширять ESIntegTestCases) и SpringJUnit4ClassRunner для автоматической передачи тестируемых классов.
Поскольку мы можем указать одну аннотацию @RunWith.Я сохранил @RunWith (RandomizedRunner.class).Чтобы активировать поддержку теста Spring Junit, добавьте следующее в мой тестовый пример.
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
Однако, когда я пытаюсь запустить модульный тест с вышеуказанной настройкой, я сталкиваюсь с AccessControlException.
Caused by: java.security.AccessControlException: access denied ("java.lang.reflect.ReflectPermission" "suppressAccessChecks")
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
at java.security.AccessController.checkPermission(AccessController.java:884)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at java.lang.reflect.AccessibleObject.setAccessible(AccessibleObject.java:128)
at org.springframework.util.ReflectionUtils.makeAccessible(ReflectionUtils.java:207)
at org.springframework.util.ReflectionUtils.accessibleConstructor(ReflectionUtils.java:191)
at org.springframework.core.io.support.SpringFactoriesLoader.instantiateFactory(SpringFactoriesLoader.java:164)
... 17 more
Полный источник контрольного примера для справки:
import com.carrotsearch.randomizedtesting.RandomizedRunner;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;
@RunWith(RandomizedRunner.class)
@ThreadLeakScope(ThreadLeakScope.Scope.NONE)
@SpringBootTest(classes = ProductSearchDemoApplication.class)
public class ProductSearchServiceTest extends ESIntegTestCase {
@Autowired
private ProductService productService;
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Test
public void test() throws Exception {
System.out.println("SubTestWithRunner test()");
}
}
Было бы замечательно, если бы кто-нибудь смог подсказать, как обеспечить Spring-Test и ESIntegTestCase без проблем работать вместе.