No tests found matching Method main in IntelliJ

11,444

The exception is thrown as the main method is not having @Test annotation on it.

Share:
11,444
ROMANIA_engineer
Author by

ROMANIA_engineer

Updated on June 05, 2022

Comments

  • ROMANIA_engineer
    ROMANIA_engineer almost 2 years

    My class is a test class and it has the following structure:

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.powermock.modules.junit4.PowerMockRunner;
    
    @RunWith(PowerMockRunner.class)
    public class MyClassTest {
    
        public static void main(String[] args) {
            System.out.println("in main");
        }
    
        @Test
        public void maxTest() {
            System.out.println("test");
        }
    
    }
    

    If I press the right click on the main method > Run 'main', it throws the following exception:

    java.lang.Exception: No tests found matching Method main(MyClassTest) from org.junit.internal.requests.ClassRequest@26aa12dd
        at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:35)
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:41)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
    

    If I press right click on the class name > Run 'MyClassTest.main()' it works fine printing:

    in main
    

    If I comment out @RunWith(PowerMockRunner.class), everything works fine in both cases.

    The same problem appears if I use @RunWith(MockitoJUnitRunner.class) instead of @RunWith(PowerMockRunner.class).

    Why is that difference?

    Details:

    • Windows 7
    • JUnit 4.12
    • PowerMock 1.6.2
    • Java 8
    • Maven 3
    • IntelliJ IDEA 14.1.3
    • shruti1810
      shruti1810 almost 9 years
      Why do you want a main method in Test class?
    • ROMANIA_engineer
      ROMANIA_engineer almost 9 years
      It is a temporary method. It helps me to run some static methods from the tested class and to extract the output that will be used in tests. I can create another file containing the main method, but I'd like to understand why it doesn't work and how it can be fixed without avoiding the problem.
  • Jonas Czech
    Jonas Czech almost 9 years
    Nice to see that you are editing some questions, but please also fix all other problems with the questions while you are editing. Minor edits waste the time of those having to review them, and may get rejected.
  • shruti1810
    shruti1810 almost 9 years
    Thanks @JonasCz Taking a small step to make the site have clear questions that can be easily searched.