首页 > 解决方案 > vscode 中不存在包 org.junit

问题描述

我在导入时 收到此错误包 org.junit 在 vscode中不存在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

标签: javamaven

解决方案


尝试

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 进行单元测试。


推荐阅读