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

Viewed 30

Recently I've been learning Spring framework. And when I tried the code on my book, an error occurred which is

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

Let me translate this as follows.

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

It's also my first time to use JUnit, I've searched for this but cannot find an answer, I will post the structure of my project and also the essential codes below to make it clear. Here is the structure of my project

I'm sure that my maven has downloaded all the jar packages needed.

In 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>

In 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");
    }

}

and the book tells me to run test(), but when I press the Run button, It just gives me this error. So I wonder if I should add a main function, I know this might be a very simple question and that may be why I cannot find an answer. So please tell me if you know this. Thanks for your help. Any suggestion will be appreciated.

1 Answers

You've placed your test code under src/main/java instead of src/test/java, so I doubt the IDE will pick up on it being a test. Move the test code code to src/test/java and mark it as a test srouce folder, and you should be OK.

Related