注释在Maven项目中不起作用

发布于 2025-01-19 13:52:42 字数 6382 浏览 3 评论 0原文

我希望每个人都进展顺利,我在src/test/java/com.selenium.test下进行了2个课程:

apptest.java:

package com.selenium.test;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class AppTest {

WebDriver driver;

//Before function
@Before
public void Begin()
{
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    driver.manage().window().maximize();
}

@After
public void End()
{
    driver.quit();
}
}

和第二类Projectest.java:java:

package com.selenium.test;

import org.junit.Test;

public class Apptest2 extends AppTest{

@Test
public void testWebsite(){
    System.out.println("Starting test " + new Object({}.getClass().getEnclosingMethod().getName());
    driver.get("http://www.google.com");
 }

在CMD中,我访问了项目文件夹,我写了此命令“ MVN测试”,这是我的输出:

C:\Users\user\eclipse-workspace\SeleniumTest>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.selenium:SeleniumTest >----------------------
[INFO] Building SeleniumTest 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ SeleniumTest ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is 
platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ SeleniumTest ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ SeleniumTest ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is 
platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ SeleniumTest ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ SeleniumTest ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.106 s
[INFO] Finished at: 2022-04-06T21:43:07+02:00
[INFO] ------------------------------------------------------------------------

这也是我的“ 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 https://maven.apache.org/xsd/maven- 
4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.selenium</groupId>
<artifactId>SeleniumTest</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>



    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>4.4.3</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.11.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>
    
</dependencies>

 <build>
    <plugins>
    
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-site-plugin</artifactId>
            <version>3.7.1</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M4</version>
        </plugin>
    </plugins>
  </build>


  <reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>3.0.0-M4</version>
        </plugin>
    </plugins>
  </reporting>

问题是,当我将@test代码放在第一类中时:

package com.selenium.test;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class AppTest {
WebDriver driver;

//Before function
@Before
public void Begin()
{
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    driver.manage().window().maximize();
}

@Test
public void testWebsite(){
    System.out.println("Starting test " + new Object(){}.getClass().getEnclosingMethod().getName());
    driver.get("http://www.google.com");
}

@After
public void End()
{
    driver.quit();
}
}

当我编写命令MVN TEST时,我有这个输出:

 -------------------------------------------------------
 [INFO]  T E S T S
 [INFO] -------------------------------------------------------
 [INFO]
 [INFO] Results:
 [INFO]
 [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 

但是,当我在第二类中写下@test时,如上所示,我有一个结果:

 -------------------------------------------------------
 [INFO]  T E S T S
 [INFO] -------------------------------------------------------
 [INFO]
 [INFO] Results:
 [INFO]
 [INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

我不明白问题在哪里,任何人都可以帮助我。先感谢您

I hope everyone is going well, i have following 2 classes under src/test/java/com.selenium.test:

AppTest.java:

package com.selenium.test;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class AppTest {

WebDriver driver;

//Before function
@Before
public void Begin()
{
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    driver.manage().window().maximize();
}

@After
public void End()
{
    driver.quit();
}
}

and the second class Projectest.java:

package com.selenium.test;

import org.junit.Test;

public class Apptest2 extends AppTest{

@Test
public void testWebsite(){
    System.out.println("Starting test " + new Object({}.getClass().getEnclosingMethod().getName());
    driver.get("http://www.google.com");
 }

in cmd i access to the project folder and i write this command "mvn test", this is the output that i have:

C:\Users\user\eclipse-workspace\SeleniumTest>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.selenium:SeleniumTest >----------------------
[INFO] Building SeleniumTest 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ SeleniumTest ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is 
platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ SeleniumTest ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ SeleniumTest ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is 
platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ SeleniumTest ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ SeleniumTest ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.106 s
[INFO] Finished at: 2022-04-06T21:43:07+02:00
[INFO] ------------------------------------------------------------------------

this is also my "pom.xml" file:

<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 https://maven.apache.org/xsd/maven- 
4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.selenium</groupId>
<artifactId>SeleniumTest</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>



    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>4.4.3</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.11.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>
    
</dependencies>

 <build>
    <plugins>
    
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-site-plugin</artifactId>
            <version>3.7.1</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M4</version>
        </plugin>
    </plugins>
  </build>


  <reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>3.0.0-M4</version>
        </plugin>
    </plugins>
  </reporting>

The problem is that when i put the @test code in the first class like this:

package com.selenium.test;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class AppTest {
WebDriver driver;

//Before function
@Before
public void Begin()
{
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    driver.manage().window().maximize();
}

@Test
public void testWebsite(){
    System.out.println("Starting test " + new Object(){}.getClass().getEnclosingMethod().getName());
    driver.get("http://www.google.com");
}

@After
public void End()
{
    driver.quit();
}
}

when i write the command mvn test, i have this output:

 -------------------------------------------------------
 [INFO]  T E S T S
 [INFO] -------------------------------------------------------
 [INFO]
 [INFO] Results:
 [INFO]
 [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 

but when i wrote the @test in the second class as shown above, i have this resultat:

 -------------------------------------------------------
 [INFO]  T E S T S
 [INFO] -------------------------------------------------------
 [INFO]
 [INFO] Results:
 [INFO]
 [INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

I didn't understand where the problem is, can anyone please help me. Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无戏配角 2025-01-26 13:52:42

也许您需要首先运行“ MVN CLEAN COMPILE”(用使用“我运行” MVN CLEAN安装')。我猜问题是您的新课程没有重新编译。我看到

[info] - -Maven-Compiler-Plugin:3.1:testCompile(默认testCompile ) @ seleniumtest

[infor]没有编译 - 所有类都是最新的

perhaps you need to run "mvn clean compile" first (usally I run "mvn clean install' all the time). I guess problem is that your new class with new @Test annotation was not recompiled. I see that

[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ SeleniumTest

[INFO] Nothing to compile - all classes are up to date

野鹿林 2025-01-26 13:52:42

第一个 src/test/java/com.selenium.test 这不是一个正确的包 src/test/java/com/selenium/test 第二个名称 Apptest2 不遵循命名约定。它必须以 *Test.java 结尾,并注意大小写,因此将其更改为 AppTwoTest... 而...并且您应该重命名您的 AppTest 因为它本身不是测试,它只是其他测试的基类,例如 AppTestBase

First src/test/java/com.selenium.test this is not a correct package src/test/java/com/selenium/test second the name Apptest2 does not follow naming convention. It must end with *Test.java and pay attention to case so change it into AppTwoTest... instead ... And you should rename your AppTest because it's not a test itself it's only a base class for other tests so for example AppTestBase

滿滿的愛 2025-01-26 13:52:42

在我看来,您根本没有配置Maven-Surefire-Plugin来找到您的测试类。

另外,您可能需要使用“硒jupiter”,而不是在POM中使用的配置。只是我的意见。

以下是示例: https://github.com/bonigarcia/benigarcia/selenium-jupiter-jupiter-examples

Looks to me like you simply haven't configured maven-surefire-plugin to be able to find your Test classes.

Also, you might want to use "Selenium-Jupiter" instead of the configuration you have in your Pom. Just my opinion.

Here are the examples: https://github.com/bonigarcia/selenium-jupiter-examples

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文