Java 使用compileOnly和testCompileOnly时gradle测试失败

Java 使用compileOnly和testCompileOnly时gradle测试失败,java,servlets,intellij-idea,gradle,build.gradle,Java,Servlets,Intellij Idea,Gradle,Build.gradle,我有一个小的gradle库项目,只有两个文件和两个测试: RandomUtils.java final class RandomUtils{ 私有静态最终SecureRandom random=新SecureRandom(); 私有随机数(){ } 静态字符串nextRandomId(){ 返回新的BigInteger(130,随机).toString(32); } } URLUtils.java: public class URLUtils { private URLUtils()

我有一个小的gradle库项目,只有两个文件和两个测试:

RandomUtils.java

final class RandomUtils{
私有静态最终SecureRandom random=新SecureRandom();
私有随机数(){
}
静态字符串nextRandomId(){
返回新的BigInteger(130,随机).toString(32);
}
}
URLUtils.java

public class URLUtils {

    private URLUtils() {
    }

    /**
     * Return the base url (eg: http://localhost:8080)
     * @param request
     * @return String
     * @throws MalformedURLException
     */
    public static String getURLBase(HttpServletRequest request) throws MalformedURLException {
        URL requestURL = new URL(request.getRequestURL().toString());
        String port = requestURL.getPort() == -1 ? "" : ":" + requestURL.getPort();
        return requestURL.getProtocol() + "://" + requestURL.getHost() + port;
    }

}
这些是再透视测试:

@RunWith(JUnit4.class)
public class RandomUtilsTest {

    @Test
    public void testConstructorIsPrivate() throws Exception {
        Constructor constructor = RandomUtils.class.getDeclaredConstructor();
        assertTrue(Modifier.isPrivate(constructor.getModifiers()));
        constructor.setAccessible(true);
        constructor.newInstance();
    }

    @Test
    public void nextRandomId() throws MalformedURLException {
        assertNotNull(RandomUtils.nextRandomId());
    }

}

我有以下
build.gradle

dependencies {
    compileOnly("javax.servlet:javax.servlet-api:${javaxServletApiVersion}")
    testCompileOnly("javax.servlet:javax.servlet-api:${javaxServletApiVersion}")
    testCompileOnly("org.springframework:spring-test:${springVersion}")
    testCompileOnly("junit:junit:${junitVersion}")
}
intellij中的依赖项没有任何问题,但是当我尝试运行
/gradlew clean build
时,我在运行任务
:common:webutils:test
时出现此错误:

java.lang.NoClassDefFoundError: org/springframework/mock/web/MockHttpServletRequest
    at com.domain.api.common.webutils.URLUtilsTest.getURLBase(URLUtilsTest.java:27)
我看过gradle,上面写着

  • compileOnly:仅编译时依赖项,不在运行时使用
  • testCompileOnly:仅用于编译测试的附加依赖项,不在运行时使用

我的配置正确吗?如果test和src都存在
javax.servlet:javax.servlet api:${javaxServletApiVersion}
,为什么在测试过程中失败?

您已经回答了自己的问题:

testCompileOnly:仅用于编译测试的附加依赖项,不在运行时使用


testCompileOnly
仅在编译测试时可用,而不是在执行测试时可用。如果您只在测试执行时需要一些东西,而不需要编译,比如
slf4j
bindings或类似的东西,那么它属于
testRuntime
。如果您需要编译测试和运行时所需的东西,那么对于这个有用的答案,它属于
testCompile

+1。否则,我不使用Gradle;-)我试图依靠文档,但没有成功。我觉得真的不清楚。“默认情况下,还包括编译依赖项、运行时依赖项和测试编译依赖项。”似乎意味着它在测试执行期间也可用。不,您可以从另一个角度阅读它。没有写过testRuntime在compile可用的地方可用,有写过testRuntime包含compile,因此是一个超集。我发现文档对此非常清楚。:-)
java.lang.NoClassDefFoundError: org/springframework/mock/web/MockHttpServletRequest
    at com.domain.api.common.webutils.URLUtilsTest.getURLBase(URLUtilsTest.java:27)