last day (14 days later) » 

1:14 PM
1
A: How to use @Before/@BeforeClass with @Autowired field

Grzegorz Górkiewicz1st solution Use TestNG instead. @Before* annotations behave this way in TestNG. No method annotated with @Before* has to be static. @org.testng.annotations.BeforeClass public void setUpOnce() { //I'm not static! } 2nd solution And if you don't want to do that, you can use an execution li...

 
This looks great, but my whole project has JUnit tests and I wasn't hoping to throw them away.
 
@carlspring, that is why I have added something else from Spring to my answer (no need to use TestNG, if you opt for the second solution) ;)
 
Can the test class itself be the listener?
 
So far used it as top-level class in another file. If it does not work, I would extend AbstractTestExecutionListener in an inner class... in the same Test.java file.
 
Grzegorz, it looks like your solution could do the trick, but I can't get it to work. Would you mind adding a really simple working example? I have this: gist.github.com/carlspring/662d8033fece212c3cd5718ed333142c and it's never hitting the beforeTestClass method. What am I doing wrong?
 
1:14 PM
Added a working example. No need to create yet another inner class (my bad). The SimpleTest class is enough ;)
 
I've almost gotten it to work, but the @Autowired class doesn't seem to be getting injected by Spring (which was my initial problem).
 
Will have another look at this in the evening, cause I'm on mobile now.
 
could you please ping me, when you're aroung?
around
thanks a lot for your help! :)
 
 
3 hours later…
4:27 PM
Yep. I'm here ;)
 
4:49 PM
hi\
i am working on an OSS project of my own
the problem is that i have a base class for the tests
i made it extend the AbstractTestExecutionListener as you advised
however, this is not taking care of initializing the @Autowired fields.
so in my beforeClass(...) method, the respective @Autowired field is null
oh, and on top of this, these tests are using RestAssured :D
 
@Autowired annotated fields only or methods as well?
 
only on fields
would you like to see the actual code?
 
Yeah, show me.
 
hang on, let me push my code
would you like to switch to skype, or google hangouts?
 
5:05 PM
I use Skype at work only, use Gmail.... so if Hangouts is preinstalled...
 
what's your e-mail?
 
gorkiewicz.grzegorz@gmail.com
 
i just mailed you
 
and messaged you on google hangouts
 

last day (14 days later) »