Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What if the @ Test annotation cannot be used in the Test.class class of the Maven project

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how the @ Test annotation cannot be used in the Test.class class of the Maven project. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The @ Test annotation cannot use an exception background in the Test.class class of Maven

In a maven web project, I wanted to write a simple test class, so I created a class Test.class

Write the @ Test annotation method in this class and find that the package cannot be imported, and manual import org.junit.Test; import is also invalid.

Abnormal information

Abnormal analysis

My first intuition is that pom.xml does not have configuration coordinates. Look at it and find that it is configured.

After rebuilding the maven, I found that it still didn't work.

Then recreate another test class and find that it can be used normally.

In other words, the name of the test class created cannot use Test, which may be due to the problem that annotations and classes have the same name.

Solution

Just delete the Test.class test class and change its name.

@ Test could not be found in Maven project

Today, when building maven for unit testing under IDEA, under the non-test file, I wrote a test class and found that the @ Test annotation could not be found, but found that there was junit's jar package under the root path, which made me feel depressed. Then I went to look at my code and found that in the maven dependency, junit's scope is test. As shown in figure 1, the problem is solved by decisively placing the test class under the test file.

Of course, if you change scope to compile, the test class does not have to be placed under the test file, but also under the main file.

Figure 1

Further observation shows that the problem occurs within the scope of maven dependence. Looking up the data, we can find out

The scope range of maven is as follows

1.test scope means that the test scope is valid and this dependency is not used when compiling and packaging

2.compile scope means that the compilation scope is valid, and dependencies are stored in both compilation and packaging.

3.provided dependency: valid in the scope of compilation and testing, and will not be added when generating war, for example: servlet-api, as shown in figure 2, because servlet-api,tomcat and other things already exist in the web server, and conflicts will occur if you package.

Figure 2

4.runtime depends on it at run time and not at compile time

The default dependency range is compile

From this, the transitive dependency of test is further extended, as follows:

Transmission of dependency

Packages whose scope is test will not be passed to other projects that reference this project, but if not test will pass dependencies to other projects.

For example, there is a dependency package junit4.10 in Project A, whose scope is test

There is now a project B that references project A, and if project B wants to use junit4.10, it must redefine the dependency itself. [because dependencies are not passed, they will not be obtained from project A]

But: if the scope is something else, not test

Then project B can be used directly without having to define a dependency on its own. [because dependencies are automatically passed from project A]

Thank you for reading! This is the end of the article on "what if @ Test annotations cannot be used in the Test.class class of Maven project". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report