roushan kumar Singh
roushan kumar Singh

Reputation: 384

Spring boot testing with testNG

I am using spring boot 2.0.1.RELEASE and TestNG for testing. But unable to load spring context and autowire repository. Kindly guide me.

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ApplictionTestsWithTestNG extends AbstractTestNGSpringContextTests{

    @Autowired
    MyRepository repo;

    @BeforeTest
    public void setup() {
        System.out.println("repository found "+repo);
    }

    @Test
    public void matchLastSavedData() {
        System.out.println("repository found " + repo);
        assertEquals(5,5);
    }
}

When running, it throws

java.lang.Exception: No runnable methods
    at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)

Upvotes: 1

Views: 2088

Answers (4)

Samt
Samt

Reputation: 204

For people who using IDEA and testNg or Junit

It is a bit different to create junit or testNg Run/Debug Configuration:

If you mixed up the two types of IDEA test runner, or mixed up annotation you are using, it will output error like: No runnable methods

Junit Test Annotations be like:

import org.junit.Test;
import org.junit.runner.RunWith;

@SpringBootTest(...)
@ActiveProfiles("test")
@RunWith(SpringRunner.class)
public class SomethingTest {

  @Test
  public void testSomemethod() {

  }
}

TestNg Test Annotations

import org.testng.Assert;
import org.testng.annotations.Test;

@SpringBootTest(...)
@ActiveProfiles("test")
public class SomethingTest extends AbstractTestNGSpringContextTests {

  @Test
  public void testSomemethod() {

  }
}

Docs Ref:

debug your first java application

run debug configuration-junit

run debug configuration-testNg

Upvotes: 0

Margarita Murazyan
Margarita Murazyan

Reputation: 29

try like this @RunWith(SpringRunner.class)

Upvotes: 0

user10475090
user10475090

Reputation: 11

@RunWith(SpringJUnit4ClassRunner.class) works with JUnit. Needs to define context configuration which should wire MyRepository bean.

Upvotes: 1

Jim Garrison
Jim Garrison

Reputation: 86774

You have no methods annotated @Test.

There are no tests to run, just as the error message told you.

Write a test method annotated with @Test.

Upvotes: 1

Related Questions