Mockito match any class argument

Two more ways to do it (see my comment on the previous answer by @Tomasz Nurkiewicz): The first relies on the fact that the compiler simply won’t let you pass in something of the wrong type: when(a.method(any(Class.class))).thenReturn(b); You lose the exact typing (the Class<? extends A>) but it probably works as you need it to. … Read more

Python decorators in classes

Would something like this do what you need? class Test(object): def _decorator(foo): def magic( self ) : print “start magic” foo( self ) print “end magic” return magic @_decorator def bar( self ) : print “normal call” test = Test() test.bar() This avoids the call to self to access the decorator and leaves it hidden … Read more

Why can outer Java classes access inner class private members?

The inner class is just a way to cleanly separate some functionality that really belongs to the original outer class. They are intended to be used when you have 2 requirements: Some piece of functionality in your outer class would be most clear if it was implemented in a separate class. Even though it’s in … Read more

TypeScript static classes

Abstract classes have been a first-class citizen of TypeScript since TypeScript 1.6. You cannot instantiate an abstract class. Here is an example: export abstract class MyClass { public static myProp = “Hello”; public static doSomething(): string { return “World”; } } const okay = MyClass.doSomething(); //const errors = new MyClass(); // Error

How do I correctly setup and teardown for my pytest class with tests?

According to Fixture finalization / executing teardown code, the current best practice for setup and teardown is to use yield instead of return: import pytest @pytest.fixture() def resource(): print(“setup”) yield “resource” print(“teardown”) class TestResource: def test_that_depends_on_resource(self, resource): print(“testing {}”.format(resource)) Running it results in $ py.test –capture=no pytest_yield.py === test session starts === platform darwin — … Read more

Calling a class function inside of __init__

Call the function in this way: self.parse_file() You also need to define your parse_file() function like this: def parse_file(self): The parse_file method has to be bound to an object upon calling it (because it’s not a static method). This is done by calling the function on an instance of the object, in your case the … Read more

Call static methods from regular ES6 class methods

Both ways are viable, but they do different things when it comes to inheritance with an overridden static method. Choose the one whose behavior you expect: class Super { static whoami() { return “Super”; } lognameA() { console.log(Super.whoami()); } lognameB() { console.log(this.constructor.whoami()); } } class Sub extends Super { static whoami() { return “Sub”; } … Read more

Final arguments in interface methods – what’s the point?

It doesn’t seem like there’s any point to it. According to the Java Language Specification 4.12.4: Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors. However, a final modifier on a method parameter is not mentioned in the rules for matching signatures of … Read more

How to initialize static variables

PHP can’t parse non-trivial expressions in initializers. I prefer to work around this by adding code right after definition of the class: class Foo { static $bar; } Foo::$bar = array(…); or class Foo { private static $bar; static function init() { self::$bar = array(…); } } Foo::init(); PHP 5.6 can handle some expressions now. … Read more