Dim*_*iwa 2 java servlets intellij-idea gradle build.gradle
我有一个小的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);
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这些是相关的测试:
@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());
}
}
Run Code Online (Sandbox Code Playgroud)
和
@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));
}
}
Run Code Online (Sandbox Code Playgroud)
我有以下内容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}")
}
Run Code Online (Sandbox Code Playgroud)
我对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)
Run Code Online (Sandbox Code Playgroud)
我查看了gradle 文档,并说它
我的配置是否正确?如果javax.servlet:javax.servlet-api:${javaxServletApiVersion} 测试和src都存在,为什么它在测试期间失败?
你已经回答了自己的问题:
testCompileOnly:仅用于编译测试的附加依赖项,不在运行时使用
testCompileOnly仅在编译测试时可用,而不是在执行时.如果你只需要在测试执行时需要某些东西,而不是像编译类似slf4j绑定或类似的东西那样,那就属于testRuntime.如果你需要一些东西来编译测试,也需要在运行时,那么它属于testCompile.
| 归档时间: |
|
| 查看次数: |
2209 次 |
| 最近记录: |