gpt4 book ai didi

java - 使用 compileOnly 和 testCompileOnly 时 gradle 测试失败

转载 作者:行者123 更新时间:2023-12-03 03:04:05 25 4
gpt4 key购买 nike

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

final class RandomUtils {

private static final SecureRandom random = new SecureRandom();

private RandomUtils() {
}

static String nextRandomId() {
return new BigInteger(130, random).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());
}

}


@RunWith(JUnit4.class)
public class URLUtilsTest {

@Test
public void testConstructorIsPrivate() throws Exception {
Constructor constructor = URLUtils.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
constructor.setAccessible(true);
constructor.newInstance();
}
@Test
public void getURLBase() throws MalformedURLException {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/entity");
assertEquals("http://localhost", URLUtils.getURLBase((HttpServletRequest) request));
request.setRequestURI(":8080/entity");
assertEquals("http://localhost:8080", URLUtils.getURLBase((HttpServletRequest) request));
}

}

我有以下 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 documentation它说
  • 只编译 : 仅编译时依赖,运行时不使用
  • testCompileOnly : 仅用于编译测试的附加依赖项,在运行时不使用

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

    最佳答案

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

    testCompileOnly: Additional dependencies only for compiling tests, not used at runtime


    testCompileOnly仅在编译测试时可用,而不是在执行它们时可用。如果你只在测试执行时需要一些东西,而不是像 slf4j 这样的编译绑定(bind)或类似的,属于 testRuntime .如果你需要一些东西来编译测试和运行时,那么它属于 testCompile .

    关于java - 使用 compileOnly 和 testCompileOnly 时 gradle 测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44505177/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com