JUnit TestGroup - None of the test groups are enabled - java

I have grouped my test methods in 2 Test Groups like so -
#TestGroup("integration")
public abstract static class AbstractIntegrationTest {
#ClassRule
public static TestGroupRule rule = new TestGroupRule();
}
#TestGroup("unit")
public abstract static class AbstractUnitTest {
#ClassRule
public static TestGroupRule rule = new TestGroupRule();
}
And have my test methods defined this way -
public static class UnitTest extends AbstractUnitTest {
#Test
public void Test1() {...}
#Test
public void Test2() {...}
}
public static class IntegrationTest extends AbstractIntegrationTest {
#Test
public void Test3() {...}
#Test
public void Test4() {...}
}
When I try to run one of my test methods (say Test1) from the Intellij IDE, I get an error saying -
org.junit.AssumptionViolatedException: None of the test groups [unit] are enabled. Enabled test groups: []
How can I enable my test group and if I am running a specific test, do I need to still enable anything?Have I set this up completely wrong?
Thanks.

Related

JUnit test for ApplicationReadyEvent in spring boot app

I tried to write test for my spring boot application.
My application has business logic, which start after spring app has been initialized.
There is need to test triggeration of method with annotation #EventListener(ApplicationReadyEvent.class)
Below is a simple example that doesn't work. I expect that inscription "Testing..." appears in the console.
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {
MySpringBootTest.MyTestConfig.class
})
public class MySpringBootTest {
#Test
public void test() {
}
#Configuration
public static class MyTestConfig {
#EventListener(ApplicationReadyEvent.class)
public void init() {
System.out.println("Testing...");
}
}
}
how do I make this example work?
Because you are using #ContextConfiguration Spring application Context is partially loaded and you don't have access to every capability of Spring application Context, however There are many ways to achieve something you want to do. One of them is using TextExecutionListener. I will show you how to use that in both Junit 4 and Junit 5 (jupiter):
Junit 5 (jupiter):
#ExtendWith(SpringExtension.class)
#ContextConfiguration(classes= MySpringBootTest.MyTestConfig.class)
#TestExecutionListeners(listeners = {MySpringBootTest.MyTestConfig.class})
public class MySpringBootTest {
#Test
public void test1() {
....
}
#Test
public void test2() {
...
}
#Configuration
public static class MyTestConfig extends AbstractTestExecutionListener {
#Override
public void beforeTestClass(TestContext testContext) throws Exception {
System.out.println("Testing...");
testContext.getApplicationContext(); //Do anything you want here
}
}
}
Here is a quote from Java doc for beforeTestClass method of TestExecutionListener:
Pre-processes a test class before execution of all tests within
the class. This method should be called immediately before
framework-specific before class lifecycle callbacks.
From the testContext that will be passed to this method you will have access to the application context and test class itself, you can do every thing you want with the test class there.
Junit 4:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {MySpringBootTest.MyTestConfig.class})
#TestExecutionListeners(listeners = {MySpringBootTest.CustomTestExecutionListener.class,
SpringBootDependencyInjectionTestExecutionListener.class})
public class MySpringBootTest {
#Test
public void test1() {
....
}
#Test
public void test2() {
...
}
#Configuration
public static class MyTestConfig {
}
public static class CustomTestExecutionListener extends AbstractTestExecutionListener {
#Override
public void beforeTestClass(TestContext testContext) throws Exception {
System.out.println("Testing...");
testContext.getApplicationContext(); //Do anything you want here
}
}
}

TestNG one test for two classes

I have TestNG tests in some classes, which extends the same main class.
I need the same last test for class1 and class2, but if I add #Test(priority = 666) to main class it start after all classes.
How I should annotate #Test in main class what would it starts after all tests of each class?
Big thanks. Also sorry for bad english.
main class
public class MainTest {
#BeforeClass()
public void setup() {
//something
}
#AfterClass()
public void tearDown() {
//something
}
#AfterMethod()
public void log_writer(Method method) {
//something
}
#Test(priority = 666) {}
}
class1
public class Class1 extends MainTest {
#Test
public void test1(){}
#Test
public void test2(){}
}
and class2
public class Class2 extends MainTest {
#Test
public void test1(){}
#Test
public void test2(){}
}
what you are looking for is the #AfterClass annotation. handle the part where you want to check logs in the AfterClass annotated method
It's not possible. Basically each #Test runs only once. If you need to do something after each test class you have to use #AfterClass annotation on method in your MainTest. You can do some hacks with method order etc. in MethodInterceptor (http://testng.org/doc/documentation-main.html#methodinterceptors) but it's not good idea for this case.

SetUp/TearDown methods in sahi tests using junit framework

Is it possible to place the setup/teardown methods using JUnit framework in a single class (which would be my baseclass) so on test runs they methods are always called first/last? it would be in a similar way to which nunit tests can be structured
currently the only way I can get my tests to kick off is if I have the setup/teardown methods within the same class as my tests are (which is something I wan't to avoid, to keep my test classes tidy)
example I would hope to set up;
public class baseclass
{
#Before
public void setUp
{}
#After
public void tearDown
{}
}
public class tests
{
#Test
public void test1
{
// test content here
}
}
Run this test and see the sequence of events
class Test1 {
#Before
public void setUp1() {
System.out.println("setUp1");
}
}
public class Test2 extends Test1 {
#Before
public void setUp2() {
System.out.println("setUp2");
}
#Test
public void test() {
System.out.println("test");
}
}
Yes, as long as your test class extend your baseclass.
For instance:
Suite
#RunWith(Suite.class)
#SuiteClasses(Tests.class)
public class AllTests {
}
BaseClass
public class BaseClass {
#BeforeClass
public static void beforeAll() {
}
#Before
public void setUp() {
}
#After
public void tearDown {
}
#AfterClass
public static void afterAll() {
}
}
Tests
public class Test extends BaseClass {
#Test
public void test1() {
}
#Test
public void test2() {
}
}

JUnit Enclosed runner and shared setup

I'm experimenting with the JUnit Enclosed runner in order to try and improve the organisation of some of my tests. At the moment I'm trying to work out how to share some setup between the inner classes.
Attempt the first:
#RunWith(Enclosed.class)
public class EnclosedTest {
#Before
public void printSomething() {
System.out.println("Helllooo Meggan");
}
public static class FirstTest {
#Test
public void assertThatSomethingIsTrue() {
assertThat(true, is(true));
}
}
public static class SecondTest {
#Test
public void assertThatSomethingIsFalse() {
assertThat(false, is(false));
}
}
}
Unfortunately, no-one says hello to Meggan. If I update an inner class to extend the outer one, then I get the following:
java.lang.Exception: class 'org.scratch.EnclosedTest$FirstTest' (possibly indirectly) contains itself as a SuiteClass
at org.junit.runners.model.InitializationError.<init>(InitializationError.java:32)
Is there a particular Enclosed idiom to use when trying to share setup between inner test classes? I was hoping it would be as simple as the C# example I found.
Enclosed runner internally works as a Suite, that is, it runs the classes as Test cases. And since Junit 4.12 abstract inner classes are ignored by Enclosed runner.
That said the way to share set up is to create an abstract class containing it (#Before, #After):
#RunWith(Enclosed.class)
public class EnclosedTest {
abstract public static class SharedSetUp {
#Before
public void printSomething() {
System.out.println("Helllooo Meggan");
}
}
public static class FirstTest extends SharedSetUp {
#Test
public void assertThatSomethingIsTrue() {
assertThat(true, is(true));
}
}
public static class SecondTest extends SharedSetUp {
#Test
public void assertThatSomethingIsFalse() {
assertThat(false, is(false));
}
}
}
Notice that you can even declare custom runners for each subclass.

JUnit #Parameterized function is run before #BeforeClass in a test suite?

I am using a JUnit test suite to run a few tests, one of which is run multiple times using #Parameterized. I am finding that when I run my tests, the #Parameterized function is run before #BeforeClass. Is this expected behavior or is something else happening? I would have expected that #BeforeClass would run before any of the tests are started.
Here is my test suite:
#RunWith(Suite.class)
#SuiteClasses({ Test1.class, Test2.class })
public class TestSuite {
#BeforeClass
public static void setup() throws Exception {
// setup, I want this to be run before anything else
}
}
Test1 uses #Parameterized:
public class Test1 {
private String value;
// #Parameterized function which appears to run before #BeforeClass setup()
#Parameterized.Parameters
public static Collection<Object[]> configurations() throws InterruptedException {
// Code which relies on setup() to be run first
}
public Test1(String value) {
this.value = value;
}
#Test
public void testA() {
// Test
}
}
How can I fix this to run the #BeforeClass setup() function before running anything else?
This is, unfortunately, working as intended. JUnit needs to enumerate all of the test cases before starting the test, and for parameterized tests, the method annotated with #Parameterized.Parameters is used to determine how many tests there are.
Although beeing a bit different solution, a static block does the trick. Also note, that it must be in the Test1.class. But beside of that it works ;-)
#RunWith(Parameterized.class)
public class Test1 {
static{
System.out.println("Executed before everything");
}
private String value;
// #Parameterized function which appears to run before #BeforeClass setup()
#Parameterized.Parameters
public static Collection<Object[]> configurations() throws InterruptedException {
// Code which relies on setup() to be run first
}
public Test1(String value) {
this.value = value;
}
#Test
public void testA() {
// Test
}
}
Recently ran into similar issue and solved problem using Function. Example below.
#RunWith(Parameterized.class)
public class MyClassTest {
#Parameterized.Parameters
public static Iterable functions() {
return Arrays.<Object, Object>asList(
param -> new Object()
);
}
Object param;
Function function;
public MyClassTest(Function f) {
this.function = f;
}
#Before
public void before() {
// construct dependency
param = "some value";
}
#Test
public void test() {
assertThat(myClass.doSomething(function.apply(param)), is(equalTo(expectedValue)));
}
}
In your scenario, do database setup in #Before or #BeforeClass then inject into function
I found a hack to force a code segment to run before all other methods that are annotated with #Parameterized.Parameters.
Just create a parameterized dummy test as follows:
#RunWith(Parameterized.class)
public class DummyInitTest
{
#Parameterized.Parameters
public static Collection<?> constructorFeeder()
{
// Your setup here. This will run before anything else.
// Return empty list so no tests will be executed for this test class.
return ImmutableList.of();
}
}
Then in your test suite, add this test first:
#RunWith(Suite.class)
#SuiteClasses({ DummyInitTest.class, Test1.class, Test2.class })
public class TestSuite {
// ...
}
Hoping this can help someone, I simply did it this way:
//#BeforeClass does not run before Parameters annotation
public static void beforeClassSetup() throws IOException {
InputStream is = Test.class.getResourceAsStream("/TestData.json");
// load data...
}
#Parameters(name = "testProductFunctionality")
public static Collection<Object[]> data() throws IOException {
beforeClassSetup();
// create parameters...
}
#Test
public void testProductFunctionality() {
//...

Categories