PowerMockito: #PrepareForTest programaticcaly? - annotations

I am looking for a way to replace the following:
#RunWith(PowerMockRunner.class)
#PrepareForTest(fullyQualifiedNames = ["jenkins.model.Jenkins", "hudson.model.Node", "hudson.model.Computer"])
class ClassToTest { ... }
By code that I would put in a setup method
#Before
void setup() {}
In the same way that it is possible to replace #RunWith(MockitoJUnitRunner.class) by MockitoAnnotations.initMocks(this)
I need this because this annotation prevent me from using #RunWith(Parametrized.class) from Junit4
Thanks !

Related

InversifyJS - Inject middleware into controller

I'm using inversify-express-utils using the shortcut decorators (#GET, #POST...) within a node application.
Is it possible to inject middleware into the controller to use with these decorators?
Example of what I'm trying to achieve (doesn't work):
export class TestController implements Controller {
constructor(#inject(TYPES.SomeMiddleware) private someMiddleware: ISomeMiddleware) {}
#Get('/', this.someMiddleware.someMiddlewhereMethod())
public test() {
...
}
}
Like #OweR ReLoaDeD said, currently you can't do that with middleware injected through the controller constructor, due to the way decorators work in TypeScript.
However, you can achieve the same effect by wrapping the controller definition in a function that accepts a kernel, like so:
controller.ts
export function controllerFactory (kernel: Kernel) {
#injectable()
#Controller('/')
class TestController {
constructor() {}
#Get('/', kernel.get<express.RequestHandler>('Middleware'))
testGet(req: any, res: any) {
res.send('hello');
}
}
return TestController;
}
main.ts
let kernel = new Kernel();
let middleware: express.RequestHandler = function(req: any, res: any, next: any) {
console.log('in middleware');
next();
};
kernel.bind<express.RequestHandler>('Middleware').toConstantValue(middleware);
let controller = controllerFactory(kernel);
kernel.bind<interfaces.Controller>(TYPE.Controller).to(controller).whenTargetNamed('TestController');
let server = new InversifyExpressServer(kernel);
// ...
UPDATE
I added an example to the inversify-express-examples repo that showcases this approach using both custom and third-party middleware.
You should be able to use middleware please refer to the following unit tests as an example.
Update
I don't think that is possible because decorators are executed when the class is declared. The constructor injection takes place when the class instance is created (which is after it has been declared). This means that, when the decorator is executed, this.someMiddleware is null.
I'm afraid you won't be able to inject the middleware into the same class that uses it but you can do the following:
import { someMiddlewareMethod} from "middleware";
class TestController implements Controller {
#Get('/', someMiddlewareMethod())
public test() {
// ...
}
}
This is not a limitation of InversifyJS this is a limitation caused by the way decorators work.

Using haxe.macro.TypeTools fails

I'm trying to debug a library which uses haxe.macro.TypeTools::findField. I've created a simple code for that:
package;
using haxe.macro.TypeTools;
class Main
{
public function new()
{
var test = findField(Child, "hello");
trace(test);
}
}
class Base
{
private function hello()
{
}
}
class Child extends Base
{
public function new() {}
}
However I'm getting error Unknown identifier : findField. Is this because it can only be used in build macro context?
This is what I'm trying to emulate.
First of all, function findField() is not from the haxe.macro.TypeTools.
It is a helper function from edge.core.macro.Macros.
To use it without a class path, import it's class with a wildcard import edge.core.macro.Macros.*
Secondly, findField() should be used in a build macro context only, since it expects Array<Field>, which is obtained by haxe.macro.Context.getBuildFields().

Normal function in Extbase controller

Is it possible to write a normal function in the controller?
I want to clean up my code a bit, so I want to write some methods for repeated code segments, but I don't want to create a special class.
How is it possible to do this?
If I do a normal
private function xyz () {}
I got a function not found error.
You should use protected, not private unless you have very good reasons to do so. Anyway, defining additional methods work fine for me.
You need to call this method with $this->xyz().
A good solution might be using an abstract class if you want to share methods accross controllers:
abstract class AbstractController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController{
protected function myFunction(){}
}
Your controllers inherit from the abstract class and will have all methods available:
class FirstController extends AbstractController {
public function firstAction(){
// has access to myFunction()
}
}
class SecondController extends AbstractController {
public function secondAction(){
// has access to myFunction()
}
}

How to pass our own runner instead of BlockJUnit4ClassRunner

Actually i wanted to override the runChild() method of BlockJUnit4ClassRunner hence I created a class (MyRunner.java) and exetended BlockJUnit4ClassRunner and overridden the runChild() method.
public class MyRunner extends BlockJUnit4ClassRunner
{
#Override
public void runChild(final FrameworkMethod method, RunNotifier notifier) {
System.out.println("<--------- Inside MyRunner.runChild() -------->");
// my code goes here
}
}
But the call doesn't go to my overridden method (it doesn't even come to MyRunner.java) and calls BlockJUnit4ClassRunner.runChild() method only.
I debugged and found that this is because the runner passed to JunitCore.run(Runner runner) method is BlockJUnit4ClassRunner.
I'm not sure on this but I think this might be coming from JUnit4Builder.runnerForClass() method .
// This is JUnit4Builder.runnerForClass(Class<?>) method
public Runner runnerForClass(Class<?> testClass) throws Throwable
{
return new BlockJUnit4ClassRunner(testClass);
}
I tried to override the JUnit4Builder.runnerForClass() also but that also did not helped.
Can anyone let me know if there's any way to pass the MyRunner or any means to override the BlockJUnit4ClassRunner.runChild() method.
You are probably not using the #RunWith annotation. In order to use your own Runner you need to annotate every TestClass with the #RunWith annotation. This will make sure, that your Test is executed with that runner.
Example:
#RunWith(MyRunner .class)
public class MyTest
{
....
}

how to check #Before is working in play framework

I'm running play application.
I have
import org.junit.Before;
public class Frontpage extends Controller {
#Before
private static void commonData() {
Map cacheMap = Cache.get("login_det",Map.class);
System.out.println("commonData");
if(cacheMap!=null)
{
renderArgs.put("login_det", cacheMap);
System.out.println("renderArgs"+renderArgs.toString());
}
}
}
But commonData is never printed in my console. How can i check #Before is working
You are using the org.junit.Before annotation, which is used by JUnit to run a method before a unit test execution.
In your case, you should use the play.mvc.Before annotation instead.
And instead of using System.out.println, try using Logger.info/debug/error/trace.
They are the standard output for Play applications.