Consistent Testing

Posted by in Software, Tutorial

TL;DR: Delete the settings file before starting the test. Today I implemented a simple wrapper over the electron-settings functionality to gibe me access to a set of properties via get/set methods. My scenario is simple: I have a property named path.backup and I want to be able to get, set andtest it’s set`. The code is:

Now, my testing code would be something along the lines:

And I was surprised to find the test failed in the second part (can detect it has source paths). Consistently. Then, I…read more

Poor Man’s Mocks in TypeScript

Posted by in Software, Tutorial

Today I went against my better judgement of usign a mock framework and decided to create my own mocks to allow coverage testing for an IPC-calling code. My reference code I wanted to test is:

The code would use an angular service named PathService which, for the purpose of this test only has a method named getSourcePaths(), which returns a Promise. Now, I needed to emulate that method, and the code I wrote to do that is:

It basically defines an object containing a method getSourcePaths() which returns…read more

Use “toThrowError” and “toThrow” in TypeScript with Objects

Posted by in Software

TL;DR: wrap your method call in the expect() statement. Instead of expect(obj.method()).toThrow(), write expect( () => { obj.method() }).toThrow(). While in the process of improving the code coverage, I needed to write a test for a method throwing an exception. My initial code would have been something like:

Where SelectorComponent has a method named updateSelectors(value: any) which throws an error if the value is not in an array. If I write the test as above, it will fail (the test itself will throw the error):

However, If I…read more

Fix Angular 2 Tests with Karma

Posted by in Software

Below are some fixes I had to apply in order to make Angular 2 tests work with karma. Binding Error: Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’ Solution: Import FormsModulein your test:

Unknown component Error: ‘custom-component’ is not a known element Solution: Declare the component in your test:

Note: If the element is in another module, then import the other module (like in the previous case) instead of declaring elements one by one. Unknown <router-outlet> tag Error: ‘router-outlet’ is not a known element…read more

Skip the EPSO Online Test

Posted by in EPSO, Tutorial

TL;DR:: Replace the end of the application URL from e.g. 1947_en to 1947/apply_en. These days, EPSO is in the habit of launching calls with multiple fields with a twist: each field has its own application page. So, if you just want to browse the fields for more details (e.g. Talent Screener in specialist competitions), you’d have to go through the test each time. Quite time consuming… The URL for a field is:

where [field_id] is the identifier of the call and [lang] is the language. E.g. for Enterprise Resource…read more

Access Data Files for Local Tests in Android

Posted by in Software

TL;DR: Put your data files in test/resources and access it with:

I’ve got an android application with a module performing data processing (HTML scraping in fact). This part is pure java and all its tests are in the src/test path. My particular problem is that I have a set of (large-ish) files to be parsed by the tests and I need a reliable way to access these files. I know that best practice would be to keep the files inline, but dumping 300k-ish content in strings for a 10-line…read more

Manually Create a Retrofit/OkHttp Response for Testing

Posted by in Software

I’ve got a method in an use case which requires a Retofit Response object as parameter and I need to test it. Now, I can use the Retrofit itself and build one with the network, but it would require the full android shebang. I’ve opted instead for building a Retrofit Response manually. My method is testing headers, so I’ll need to build a Response with headers. The Response method to do this is:

A note: Where you see Response , it’s actually retrofit.Response. The OkHttp counterpart is prefixed: com.squareup.okhttp.Response….read more

MVP in Android – A Summary

Posted by in IT

These days I’m reading on architectures. I had a look at MVC, MVP, MVVM and how they are applied (adapted to?) to Android applications. Here are some short points about MVP. Model-View-Presenter has been adapted to Android and is advocated by more experienced people than I. It is interesting to note that in the Clean Architecture approach, MVP is only taking care of the presentation layer. Model In the Clean Architecture, the model is .However, given the magnitude of most applications, at least the Model part is more involved. Most…read more

Android: Provide Local Asset in Testing

Posted by in Software

In the past few days I had a little bit of a problem: InstrumentationTestCase gives access to local assets while AndroidTestCase gives access to getAssets() for assets specific to testing. I had a scenario to test where I get a file from my local src/androidTest/assets directory while the code would need to write some files in /data/data/ directory. Quite unfortunate I’d say. My solution was to create an InputStream method based on this answer, to cover both cases. The code is:

This way, if the method is called from…read more

NullPointerException – getInstrumentation().getContext()

Posted by in Software

Here’s a beginner mistake… The following code:

Throws an exception: Attempt to invoke virtual method ‘android.content.Context’ on a null object reference It took me some time to find out why: getInstrumentation() returns null in the constructor :) The correct way is to use setUp():

Then, you can call super in classes extending this one. HTH, A little experiment: If you find this post and ad below useful, please check the ad out :-)