@Test不使用junit 4。

时间:2023-01-18 08:45:12

I'am trying to test a class with Jnuit 4 in a maven project. The test runs successfully when I run it from my STS, but when I try to run it from maven command line, I get the following error: annotations are not supported in -source 1.3 (use -source 5 or higher to enable annotations) @Before

我试图用maven项目中的Jnuit 4测试一个类。当我从STS中运行测试时,测试运行成功,但是当我尝试从maven命令行运行它时,我得到了以下错误:在-source 1.3(使用-source 5或更高版本以启用注释)中不支持注释。

Code under test is:

测试代码是:

public class MyContollerTest{
 @Before
 public void setup(){
    System.out.println("This is MyControllerTest before..."); 
 }
    @Test
    public void testShouldTest(){
        System.out.println("This is MyControllerTest"); 
    }
    @After
    public void tearDown(){
        System.out.println("This is MyControllerTest after..."); 
    }
}

I'm using the Junit 4.8.1 as maven dependecy

我使用Junit 4.8.1作为maven的依赖项。

I have checked my complience level is 1.6 and I'm using jdk 1.6

我检查了我的遵从性级别是1.6,我使用的是jdk 1.6。

2 个解决方案

#1


3  

You'll need to add maven-compiler-plugin config in the build section of the pom.xml

您需要在pom.xml的构建部分添加maven-compile -plugin配置。

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <verbose>true</verbose>
        <compilerVersion>1.6</compilerVersion>
        <source>1.6</source>
        <target>1.6</target>
      </configuration>
    </plugin>
  </plugins>
</build>

#2


1  

Did you set appropriate source and target of your maven-compiler-plugin according to the documentation?

根据文档,您是否设置了maven-编译器插件的适当来源和目标?

#1


3  

You'll need to add maven-compiler-plugin config in the build section of the pom.xml

您需要在pom.xml的构建部分添加maven-compile -plugin配置。

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <verbose>true</verbose>
        <compilerVersion>1.6</compilerVersion>
        <source>1.6</source>
        <target>1.6</target>
      </configuration>
    </plugin>
  </plugins>
</build>

#2


1  

Did you set appropriate source and target of your maven-compiler-plugin according to the documentation?

根据文档,您是否设置了maven-编译器插件的适当来源和目标?