软件包 org.junit 在 VSCODE 中不存在



我得到这个错误package org。在vscode中不存在junit当我导入时import junit.framework.Test;import static org.junit.Assert.assertEquals;

这是我的测试文件

package com.wezigo.myapp.service;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class MyServiceTest {
@Test
public void testCompute(){
MyService service =new MyService();
//MyService service =  new MyService();
double a = 15;
double b = 15;
double expected = 20;
double result = service.compute(a,b);
assertEquals(expected, result,0.001 );
}


}

这是pom.xml文件的内容

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wezigo</groupId>
<artifactId>myapp</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>myapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
</project>

我尝试这个地址的解决方案**为什么vscode不能识别import org.junit?**但我使用的vscode 1.60.0和这个命令是不可用的视图>命令面板->Java: Clean Java Language Server Workspace

编译时,我得到这些错误

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.083 s
[INFO] Finished at: 2021-09-24T12:10:31+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/home/rodolphe/javaTraining/work1). Please verify you invoked Maven from the correct directory. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException

Try

package com.wezigo.myapp.service;
public class MyServiceTest {
public void testCompute(){
MyService service =new MyService();
double a = 15;
double b = 15;
double expected = 20;
double result = service.compute(a,b);
assert Math.abs(expected - result) <= 0.001;
}
}

一个流行的谬论是你需要junit来进行单元测试。

相关内容

最新更新