Giter Site home page Giter Site logo

Comments (5)

vessosa avatar vessosa commented on June 12, 2024 1

@krmahadevan Thank you for your response and for clarifying the situation. I truly appreciate your insights and understanding!

from testng.

juherr avatar juherr commented on June 12, 2024

That's a question for Spring.

from testng.

krmahadevan avatar krmahadevan commented on June 12, 2024

Since the object lifecycle management between spring and testng are completely different, in simple terms you would need to build a bean bag querying mechanism that looks like below

BeanBag
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class BeanBag implements ApplicationContextAware {
    private static BeanBag instance;
    private ApplicationContext context;

    public BeanBag() {
        instance = this;
    }

    public static <T> T getObject(Class<T> type) {
        return instance.context.getBean(type);
    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        this.context = context;
    }
}
This is how the sample config will look like
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class SampleConfig {

    @Bean
    public Employee employee() {
        return new Employee("Master Shifu", 40);
    }
    public record Employee(String name, int age) {

        @Override
        public String toString() {
            return String.format("Employee {%s, %d}", name, age);
        }
    }
}
This is how the test listener looks like
import com.rationaleemotions.demo.config.SampleConfig;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class ExampleListener implements ITestListener {

    @Override
    public void onTestStart(ITestResult result) {
        System.err.println(">>>>>>" + BeanBag.getObject(SampleConfig.Employee.class));
    }
}
Sample test case
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@SpringBootTest
@Listeners(ExampleListener.class)
public class DemoApplicationTests extends AbstractTestNGSpringContextTests {

    @Test
    public void contextLoads() {
        System.err.println("Hello world");
    }
}

execution output:

2023-10-24T12:17:38.267+05:30  INFO 19385 --- [           main] c.r.demo.DemoApplicationTests            : Starting DemoApplicationTests using Java 17.0.8 with PID 19385 (/Users/kmahadevan/githome/playground/spring_test_demo/target/test-classes started by kmahadevan in /Users/kmahadevan/githome/playground/spring_test_demo)
2023-10-24T12:17:38.269+05:30  INFO 19385 --- [           main] c.r.demo.DemoApplicationTests            : No active profile set, falling back to 1 default profile: "default"
2023-10-24T12:17:39.287+05:30  INFO 19385 --- [           main] c.r.demo.DemoApplicationTests            : Started DemoApplicationTests in 1.316 seconds (process running for 2.437)
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
>>>>>>Employee {Master Shifu, 40}
Hello world

===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

from testng.

vessosa avatar vessosa commented on June 12, 2024

@krmahadevan Thank you for your answer.

That works as you override onTestStart. I was trying to override onStart.

    @Override
    public void onStart(ITestContext context) {
        System.err.println(">>>>>>" + BeanBag.getObject(SampleConfig.Employee.class));
    }

Will result in:

java.lang.NullPointerException: Cannot read field "context" because "com.example.springboottestngtestsdemo.BeanBag.instance" is null

Test ignored.
	at com.example.springboottestngtestsdemo.BeanBag.getObject(BeanBag.java:18)
	at com.example.springboottestngtestsdemo.ExampleListener.onStart(ExampleListener.java:12)

@juherr Thanks for your feedback; my idea was to request to check if this is something that could be changed. The onStart method is invoked before the Spring App Context loads.

from testng.

krmahadevan avatar krmahadevan commented on June 12, 2024

@vessosa

  • org.springframework.test.context.testng.AbstractTestNGSpringContextTests is NOT owned by TestNG but its owned by the spring eco-system. So we have no control on when what gets called.
  • my idea was to request to check if this is something that could be changed. The onStart method is invoked before the Spring App Context loads.

Yes, you are correct, but onStart() is being called by TestNG before starting to run any @Test methods within a <test> tag, but AbstractTestNGSpringContextTests is initialising the spring application context only when it runs the @BeforeClass that is part of it. So onStart() is ALWAYS bound to happen before calling @BeforeClass. This cannot be altered in TestNG because the semantics would change. Perhaps you can either request for an alternate from the Spring community (or) you could create your own base class similar to AbstractTestNGSpringContextTests and work with it.

I am not sure what additionally can be done here. But the crux of the change is all isolated to AbstractTestNGSpringContextTests which is NOT within the scope of TestNG.

from testng.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.