似乎我们无法让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()");
}
}
如果有人可以指导如何确保弹簧测试和ESIntegTestCase无缝地协同工作,那将很棒。
我知道有些晚了,但是我们只遇到了ESIntegTestCase
的许多问题,并希望分享我们的解决方案。我们最终将Elasticsearch container中的testcontainers.org作为嵌入式Elasticsearch用于我们的集成测试。
优点是您可以使用任何版本的Elasticsearch,并且可以轻松集成到任何项目中。