如果我在测试 Spring 项目时使用了来自 JUnit 的注释“@Test”,是否需要编写 main() 函数?

Do I need to write a main() function when I've used annotation '@Test' from JUnit while testing Spring Project?

最近在学习Spring框架。当我尝试我书上的代码时,发生了一个错误

错误: 在类 com.test.app.SpringTest 中找不到 main 方法, 请将 main 方法定义为:
   public static void main(String[] args)
否则 JavaFX 应用程序类必须扩展javafx.application.Application

让我翻译如下。

Error: cannot find main function in class 'com.test.app.SpringTest', 
please define main function as: ... 
otherwise JavaFX application class must be extended from javafx.application.Application

我也是第一次使用JUnit,我已经搜索过这个但找不到答案,我将post我的项目结构以及下面的基本代码说清楚。 Here is the structure of my project

我确定我的maven已经下载了所有需要的jar包。

在applicationContext.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns = "http://www.springframework.org/schema/beans"
       xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation = "http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

在SprintTest.java

package com.test.app;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
// import org.springframework.test.context.junit4.SpringRunner;

// @RunWith(SpringRunner.class)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class SpringTest {

    @Test
    public void test(){
        System.out.println("test");
    }

}

书上告诉我要 运行 test(),但是当我按下 运行 按钮时,它只会给我这个错误。所以我想知道我是否应该添加一个主要功能,我知道这可能是一个非常简单的问题,这可能就是我找不到答案的原因。所以,如果你知道这一点,请告诉我。谢谢你的帮助。任何建议将不胜感激。

您已将测试代码放在 src/main/java 而不是 src/test/java 下,所以我怀疑 IDE 是否会将其视为测试。将测试代码代码移动到src/test/java,标记为测试srouce文件夹,应该就OK了。