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

Access Report from p:menuitem
Eclipse commands in java code
Add regex matches together
$Class.getResource failed from fat jar packed by maven shade plugin
Java ThreadPool that allocates work by thread name
jackson jersey json : serializing date from java to json
findViewById problems for webview in navigation drawer
For loop how to print to console without leaving on first iteration java
Dispatcher servlet throws exception: NumberFormatException
Overriding equals method to accept doubles inside of Object class. Java
Line break between GWT widgets
How to set shift in String.format dynamically? [duplicate]
Java audio doesn't play in jar file
How can I obtain the -D parameters passed in to Java launch
Set multiple listeners to one method
why one class of same package can't access another?

Categories

HOME
ms-access
twitter
fme
google-project-tango
mouse
uber-api
ios-charts
medical
metatrader4
user-input
worldwind
ghc
pc
normalizr
interop
web-sql
blazemeter
su
web-api-testing
gsmcomm
r-raster
nameservers
junit5
twilio-api
uisplitview
winrt-xaml-toolkit
phpfreechat
google-api-nodejs-client
scaffold
lto
twitch
azure-ml
ws-security
unoconv
ibpy
firebase-admin
total-commander
modelmapper
reactive-cocoa-5
websphere-mq-fte
btrace
apple-news
chain-builder
typescript1.8
aurelia-validation
composite-key
sybase-asa
boost-preprocessor
knpmenubundle
pillow
nbconvert
orthogonal
pintos
deadbolt-2
android-fonts
disque
markojs
skype4py
dlna
captivenetwork
maven-tomcat-plugin
cartesian-product
comobject
riak-cs
microbenchmark
graph-api-explorer
inmobi
onactivityresult
poller
bundles
fmod
tws
buster.js
quickdialog
online-compilation
jquery-ui-layout
hgsubversion
adk
locationlistener
remember-me
radcombobox
jquery-ui-droppable
forums
fixed-width
temporal-database
nerddinner
msdev
ugc

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