[英]Why can't Bazel find my test class when running bazel test on a java_test rule?
I have a test class defined in src/handlers/MyHttpHandlerTest.java
(sorry, I know it's best practice to put tests in a tests
directory).我在
src/handlers/MyHttpHandlerTest.java
中定义了一个测试 class(抱歉,我知道最好将测试放在tests
目录中)。
Here is my src/handlers/BUILD
file:这是我的
src/handlers/BUILD
文件:
java_test(
name = "MyHttpHandlerTest",
srcs = [
"MyHttpHandler.java",
"MyHttpHandlerTest.java",
],
deps = [
"//:java_test_deps",
"//src/messages",
],
)
My test class is defined in src/handlers/MyHttpHandlerTest.java
as follows (It currently doesn't assert anything -- I'm just trying to test out my installation of junit and Mockito):我的测试 class 在
src/handlers/MyHttpHandlerTest.java
中定义如下(它目前没有声明任何东西——我只是想测试我安装的 junit 和 Mockito):
package src.handlers;
import com.sun.net.httpserver.HttpExchange;
import src.messages.Message;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.io.IOException;
@ExtendWith(MockitoExtension.class)
public class MyHttpHandlerTest {
@Mock
Message message;
@Mock
HttpExchange exchange;
@Test
public void testGetMessage() throws IOException {
MyHttpHandler handler = new MyHttpHandler();
when(message.getMessage()).thenReturn("Hello world!");
handler.setMessage(message);
handler.handle(exchange);
}
}
When I run bazel test //src/handlers:MyHttpHandlerTest
, it successfully compiles the source files, but then returns the error:当我运行
bazel test //src/handlers:MyHttpHandlerTest
时,它成功编译了源文件,但随后返回错误:
Class not found: [handlers.MyHttpHandlerTest]
Why can't bazel find this class?为什么bazel找不到这个class?
Bazel guesses that the the root of the Java package hierarchy is the src
folder and therefore tries to load handlers.MyHttpHandlerTest
. Bazel 猜测 Java package 层次结构的根是
src
文件夹,因此尝试加载handlers.MyHttpHandlerTest
。 So, either change the package to actually be handlers
or override Bazel's heuristic with the test_class
attribute:因此,要么将 package 更改为实际
handlers
,要么使用test_class
属性覆盖 Bazel 的启发式方法:
java_test(
name = "MyHttpHandlerTest",
srcs = [
"MyHttpHandler.java",
"MyHttpHandlerTest.java",
],
deps = [
"//:java_test_deps",
"//src/messages",
],
test_class = "src.handlers.MyHttpHandlerTest",
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.