Home > Mobile >  No runnable method junit/java
No runnable method junit/java

Time:11-28

I'm trying to run a test to compare if the user input has the same amount of words as the specified expected length (4).

I right-click the source file in NetBeans and select "Test file", but for some reason, I keep getting the error: "No runnable method" when there clearly is one? Can someone explain to me what is going on?

import java.util.Scanner;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class UserMainTest {
    
    @Test
    public void testMain() {

        Scanner input = new Scanner(System.in);
        String line = "";
        line = input.nextLine();
        String arr [] = line.split(" ");
        int expected = 4;
        int actual = arr.length;
        assertEquals(expected, actual);
    }
}

CodePudding user response:

  1. If you change your code a bit and change it, you can do it like this
    @DisplayName("testMain")
    @ParameterizedTest
    @ValueSource(strings = {"1 2 3 4"})
    public void testMain(String line){

        String arr[]=line.split(" ");
        int expected=4;
        int actual=arr.length;

        assertThat(expected).isEqualTo(actual);
    }

  1. If you are using junit4 you may get an error if you use junit5 import org.junit.jupiter.api.Test.
JUnit 4 org.junit.Test 

Junit 5 import org.junit.jupiter.api.Test
  • Related