java


Loading application context via annotation in benchmark


Let's assume I want to write a benchmark for the class which can be autowired thus I need to load application context.
My test has annotation #org.openjdk.jmh.annotations.State(Scope.Benchmark) and main method
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(MyBenchmark.class.getSimpleName())
.forks(1)
.build();
new Runner(opt).run();
}
And of course I have some benchmarks like this:
#Benchmark
public void countAllObjects() {
Assert.assertEquals(OBJECT_COUNT, myAutowiredService.count());
}
Now, the question is how do I inject myAutowiredService?
Possible solution
Load manually the context in #Setup method.
ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/application-context.xml");
context.getAutowireCapableBeanFactory().autowireBean(this);
But I don't like this solution. I would prefer that my test just have annotation
#ContextConfiguration(locations = { "classpath:META-INF/application-context.xml" })
and then I just inject my bean like
#Autowired
private MyAutowiredService myAutowiredService;
but this does not work. The reason, I assume, is that I have no annotation that my test should run with Spring:
#RunWith(SpringJUnit4ClassRunner.class)
However there is no point of doing this because I also don't have any #Test annotated methods, thus I will get No runnable methods exception.
Can I achieve loading the context via annotations in this case?
I would opt for the getAutowireCapableBeanFactory().autowire() solution you already sketched out.
There has to be some boilerplate code that loads the application context and triggers autowiring. If you prefer to specify your app config with annotations the setup method could look something like this:
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(MyBenchmarkWithConfig.class);
context.refresh();

Related Links

Java built in monitoring tool
Collapsing toolbar layout with recyclerview need to collapse based on recycler view height
How to log exception stack trace in Json object with out breaking with log4j2
Efficiency of sequential algorithm executed on machine with 60Gb RAM
Joda-Time Period Calculation
How to check if Current Hour falls between certain range
How to set alert cancel on Popup In selenium web driver
How to get all the keys of json in java using Gson or org.json library? [duplicate]
Can someone explain join method in Java Thread class?
Spring Boot PropertySources in test environment
How to get image source and description from html data using Jsoup
How to clear java.lang.IndexOutOfBoundsException in selenium webdriver
Java Reflection: Get type parameter of generic interface from implementation class
Start Spring Boot War in a tomcat instance
HDIV : I'm getting a NullPointerException when accessing a page
Android Material Design Toolbar extension

Categories

HOME
java
reflection
relative-path
vmware
xmpp
jira
setup-deployment
sharepoint-designer
rubygems
icloud
flyway4
awesome-wm
nano-server
user-input
midi
collectd
gz
systemc
angular-ui
footer
plunker
amazonsellercentral
intentfilter
css-animations
format-specifiers
dxf
firebase-crash-reporting
dynamic-featured-image
bootstrap-duallistbox
c++-amp
yadcf
ioio
commit
protovis
windows-dev-center
ghost4j
y86
appfabric
main
git-merge
serve
espeak
uft-api
grid.mvc
janrain
android-mediaprojection
spring-security-kerberos
midl
total-commander
reactive-cocoa-5
scorm
clean-architecture
git-diff
boost-preprocessor
memory-alignment
color-picker
google-feed-api
heidisql
slicknav
home-directory
adxstudio-portals
fody
etsy
contact-list
master-slave
captivenetwork
pagerank
pyke
srand
eclipse-clp
android-listview
sailfish-os
device-orientation
javax.mail
oxwall
qpainter
nsbutton
html5-notifications
spring-io
apc
listings
opcache
jquery-knob
bitsharp
rdoc
cisco-jtapi
ticoredatasync
bubble-chart
dotnethighcharts
nsobject
external-accessory
gcj
horizontal-accordion
cinema-4d
calling-convention
gamma
asp.net-profiles
vc90
rescale
multi-tier
simpletest
temporal-database
commodore

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App