What is the best practice for this?
Say, I want to initialize some counters or read a file to use it later in parsing. How would I do that? Override __init__? Or there are some other better ways?..
depends on your need , what ever it is project level , or simply spider level override init is the best approach.
Related
all
For some reason, we want to intercept java.io.File class to add some log and do some check.
I tried to create a java agent with Byte Buddy to use Advice to do such things for some other class.
But it never work for class which is already loaded before agent premain. I am on byte-byddy 1.10.5
I also did some try use redefinition, or transform, but it's not work as well, not sure if I did something wrong.
Is there a way to achieve this?
You need to enable a retransformation of existing classes. Also, you need to define a custom ignore matcher as boot classes are not instrumented by default.
agentBuilder
.with(RedefinitionStrategy.RETRANSFORMATION)
.disableClassFormatChanges()
.ignore(...)
I am trying to expand upon the functionality of the Dismissible widget, which is a StatefulWidget, with its state being a private class.
Since I need to change the functionality inside of _DismissibleState, is it possible to somehow extend from it?
If not, is there an alternative or recommended way how to extend upon Flutter standard classes, apart from copying the whole source?
You can only extend a class that you can refer to. If _DismissibleState is declared in a different library, then you cannot refer to it, and so you can't extend it.
There is no workaround. That's what it means to be private.
You also cannot extend Dismissible to return a different state because its interface contains _DismissibleState createState(). There is no way you can return a state which satisfies that interface restriction, and you also cannot override it with a different return type unless that type also implements _DismissibleState, which was the original unsolvable problem.
I had to copy and paste the entire original file into a new one to make a simple change just in one line in a private class.
I would like to write a JUnit5 Extension where I have to take some action when a test method annotated with #Disabled is found. Unfortunately, beforeTestExecution() is not called for such methods. Does anybody have an idea how to intercept such #Disabled test methods ?
Thanks !
As described in the User Guide, you can disable the built-in ExecutionCondition that handles #Disabled by default by setting junit.jupiter.conditions.deactivate to org.junit.*DisabledCondition (see Configuration Parameters on how to set it). This will cause your tests to be executed.
Next, you need to implement your own ExecutionCondition extension, check for #Disabled, take your action and return ConditionEvaluationResult.disabled("...").
In order to avoid having to register your extension on each test class, you can activate Automatic Extension Registration and register your extension globally.
Depending on what you want to achieve, it may be easier to register your own TestExecutionListener (see Plugging in Your Own Test Execution Listeners) and implement executionSkipped().
The extension point used here is https://github.com/junit-team/junit5/blob/master/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ExecutionCondition.java
It might be interesting to find out how several implementors compose, i.e. if the 2nd one will be called at all and under what circumstances. You’ll probably have to dive into Jupiter code or do some experiments.
How to specify the class and the method to make a MS tweak on it ?
There is a lot of classes and methods in every single app or in springboard :(
Any way to make it easy to find the one I'm gonna work on ?
You need to seek for the stuff you'll use within the dumped headers you have for the app. The names of the classes are usually (at least, in SpringBoard) explicit enough to let you guess their purpose.
Finding the right class/method to hook can be long, but you can use Flex (available on the Cydia Store) to browse headers and methods more easily
I found, that when I writing unit tests, especially for methods who do not return the value, I mostly write tests in white box testing manner. I could use reflection to read private data to check is it in the proper state after method execution, etc...
this approach has a lot of limitation, most important of which is
You need to change your tests if you rework method, even is API stay
the same
It's wrong from information hiding (encapsulation) point of view -
tests is a good documentation for our code, so person who will read
it could get some unnecessary info about implementation
But, if method do not return a value and operate with private data, so it's start's very hard (almost impossible) to test like with a black-box testing paradigm.
So, any ideas for a good solution in that problem?
White box testing means that you necessarily have to pull some of the wiring out on the table to hook up your instruments. Stuff I've found helpful:
1) One monolithic sequence of code, that I inherited and didn't want to rewrite, I was able to instrument by putting a state class variable into, and then setting the state as each step passed. Then I tested with different data and matched up the expected state with the actual state.
2) Create mocks for any method calls of your method under test. Check to see that the mock was called as expected.
3) Make needed properties into protected instead of private, and create a sub-class that I actually tested. The sub-class allowed me to inspect the state.
I could use reflection to read private data to check is it in the proper state after method execution
This can really be a great problem for maintenance of your test suite
in .Net instead you could use internal access modifier, so you could use the InternalsVisibleToAttribute in your class library to make your internal types visible to your unit test project.
The internal keyword is an access modifier for types and type members. Internal types or members are accessible only within files in the same assembly
This will not resolve every testing difficulty, but can help
Reference