Comments (5)
@krmahadevan Thank you for your response and for clarifying the situation. I truly appreciate your insights and understanding!
from testng.
That's a question for Spring.
from testng.
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.
@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.
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)
- How to enable generateTestResultAttributes programatically using Java ? HOT 1
- Should not we use the lock everywhere suiteResult is used? HOT 1
- Associate a unique id with every test class object instantiated by TestNG
- Discrepancy with combination of (Shared Thread pool and Method Interceptor) HOT 1
- IInvokedMethodListener Iinvoked method does not return correct instance during @BeforeMethod, @AfterMethod and @AfterClass
- Please document project's PGP artifact signing keys HOT 2
- Why is my test case not retried but ignored after execution failure? HOT 5
- Possible to merge PR 3067 in 7.9.0 HOT 2
- Super class annotated with ITestNGListenerFactory makes derived test class throw TestNGException on execution HOT 1
- Test cases are being skipped when running in parallel a factory class containing dependent methods HOT 11
- Update from testng 7.9.0 to 7.10.0 break maven build with junit5 HOT 11
- Replacement API for `IClass.getInstanceHashCodes()` and `IClass.getInstances(boolean)` HOT 19
- How come TestNG listeners are global? HOT 3
- Failing while building testng version 7.7.1 using ./gradlew build HOT 3
- ListenerComparator doesn't work HOT 4
- ITestNGListenerFactory is broken and never invoked HOT 3
- can not set custom test method name when using data provider HOT 12
- Update JCommander to 1.83
- [Feature Request] allow multiple groups with AND condition HOT 2
- How to rerun skipped test cases using TestNG? HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testng.