Showing posts with label agility. Show all posts
Showing posts with label agility. Show all posts

Saturday, April 27, 2013

Testing Android Apps with Junit (no more slow emulator)

Hi there, today i wanna share a very helpful way to test our Android Apps with fast Junit tests instead of using the "slow emulator". With Junit you'll speed up your development and ensure quality.

Requirements:
  • Your Project must use at a minimum platform 2.2 Google API level 8. (This is the most cases)
  • You must use Junit 4 Library and not Junit 3.
  • Download the newst jar file from here: robolectric-X.X.X-all.jar 
How to do it? 
  1. Create your Android project normally.
  2. select your project and add a new folder called test like the pic bellow.


Creating a Junit Test Project
Create a simple Java-Project (attention not another Android-Project) with the same name as your Android project but with the suffix -Test at the end and click next. (Attenion: do not press finish)  
Ex: MyProject and MyProjectTest

Select the src-folder, right mouse click and select "remove source folder" from build path. (the src folder will change from source into normal folder. see picture bellow after point 4)


Select the src-folder, right mouse click and take "Link additional source" and browse the created test folder from your Android Project and then click finish. The result will be something like point 1 in the pic above.

Create a folder called lib in your test project like point 4 in the picture above and copy/paste the downloaded roboelectric-x.x.x-all.jar into it.

Now select your test project, right mouse click, select Build Path > Configure Build Path... > select tab libraries > add library > Junit > next > select Junit 4 > click finish but stay in this dialog.

Click add jars... > select your test project (in this example here: MyProjectTest) > lib > select roboelectric-x.x.x-jar > click ok but stay in this dialog. (see picture bellow point 1)

Click add external jars... > [browse the location from your android-sdk] > platforms > android 8 > select android.jar > click ok but stay in this dialog. (see picture bellow point 1)

Click add external jars... > [browse the location from your android-sdk] > add-ons> addons-google_apis-android-8 > libs > select maps.jar > click ok. (see picture bellow point 1)


Select the Tab Projects > add your test project to it like the picture bellow. After that press the refresh key (F5) and run a project clean. (Project > clean...) (see picture bellow)


Setup your test run configuration
Attention: This step is very important. It will not work, if you don't do it.

Select menu run > run configurations... > double click Junit > and follow the steps in the picture bellow:




Write your first test to validate all those steps
Go back to your test project (in this case here MyProjectTest) and create a simple class in the test folder called MyActivityTest and copy the next code lines into it.

package com.example;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;

import com.example.myproject.MainActivity;
import com.example.myproject.R;
import com.xtremelabs.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {

    @Test
    public void shouldHaveHappySmiles() throws Exception {
        String hello = new MainActivity().getResources().getString(R.string.hello_world);
        assertThat(hello, equalTo("Hello world!"));
    }
}

Run the test
Menu > Run > Run Configurations... > select Junit > select your created MyProjectTestConfiguration and run it. If everything went well, we should get something like this:



So bye bye slow emulator... ;-) happy coding!



😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘‡

Be sure to read, it will change your life!
Show your work by Austin Kleonhttps://amzn.to/34NVmwx

This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practiceshttps://amzn.to/30WQSm2

Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv

This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp

Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy

😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘†




Monday, August 1, 2011

How to avoid the violation of the DRY-Principle

Don’t Repeat Yourself (DRY-Principle) ?

Table of contents
Why do you need source code conventions in your company?
An other common violation of the DRY principle is this one here :
Do you want to stay up to date?
Source code convention tools
Literature, good books and references
How can i subscribe/feed this blog ?
How can i rate this blog ?
Where do i find more clean code knowledge and gadgets?

How to avoid the violation of the DRY-principle?

In this blog i'm gonna show you how to avoid violation of the DRY principle. The most important thing: i will tell you WHY you should not. I'm gonna give you some arguments, so that you are able to explain and motivate anybody at anytime.

What i hate, is the violation of the DRY principle by copy-pate or auto-comments. It seams to be a world wide "disease", because we all know it, but still continually violate this principle almost all the time. Let's figure out what i mean by doing a very trivial example:

Bad example 1 - DRY violation by repeating comments / making javaDoc obsolete:
/**
* The id
* @return id
*/ 
public long getId(){
    return this.id;
This example is so trivial, the most of the programmers would say one of those sentences: 
  • it's ok, any programmer with a little experience is able to understand that.
  • if a programmer does not understand that, then he is probabily on the false job
  • it would be better without comments.
  • it was auto-generated from eclipse. 
  • My checkstyle tool would fail if a did not comment it.
it sounds legtmin right? Well, if i was forced to do that, i would probabily say to my boss:
hey boss, i know you need me to do the work XYZ, but i have to rewrite (even better repeat)  some pieces of my code to accomplish the rules we have in your company
What do you think, he would say? OK, i think you got it...let's jump this part and see how it should be and why!
Good example 1: 
/**
* With this id you are able to retrieve the object from the 
* database by calling {@link DAO#get(id)} 
* @return id from the object to be retrieved
*/ 
public long getId(){
    return this.id;
1 rule: we comment not what, but why we do something.
2. rule: we write an example how to use it, because in a year nobody knows that anymore
3. rule: writing a good comment, motivates and encourages junior programmers to do it correctly also.
4. rule: the javaDoc does not become obsolete this way and really helps
5. rule: a good comment can reduce maintenance efforts, when a bug is issued
6. rule: agility has nothing to do with zero documents! thats not the meaning. agility, means sustainability means, re-usability, understandable clean code and so on....

Do you want to improve your development skills? Follow: @algoritmo4j

An other common violation of the DRY principle is this one here :


Bad example 2 - DRY violation unconsciously:

/**comment ommited */ 
public void doThis(Object o){
    if(o !=null){
         // do something here... 
        }
/**comment ommited */ 
public void doThat(Object o){
    if(o !=null){
         // do something here... 
        }
this is an also very trivial example, but a really good one. such constructs are very often. instead of doing this way, do better this way here:

Good example 2: 
/**comment ommited */ 
public void doThis(Object o){
    doNotAllowNull ( o ) ;    
     // do something here... 
/**comment ommited */ 
public void doThat(Object o){
    doNotAllowNull ( o ) ;    
     // do something here...
/**comment ommited */ 
private <T>  void  doNotAllowNull(T anyObjectOfYourChoice){
    final String msg = "YourExceptionHelpMessage"; 
    if( anyObjectOfYourChoice  == null){
          throw new NullPointerException(msg);
        }
Or this way here:
/**comment ommited */ 
private <T>  boolean  isNotNull(T anyObjectOfYourChoice){
    return ( anyObjectOfYourChoice != null ) ?  true  :  false
Even better: 
Even better it would be to define an interface and a class that does that for you so that you don't have to write or implement it always from the scratch. you just have to delegate it to this class.

Do you want to stay up to date?

bookmark www.treslines.com or follow: @algoritmo4j

Please rat it by clicking the google+1 or leaving some constructive comments or some bad examples from your companies and how you solve them. i also wanna learn from you. The next lesson will be: The KISS Principle. Stay connected and follow me. Do not miss it.

😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘‡

Be sure to read, it will change your life!
Show your work by Austin Kleonhttps://amzn.to/34NVmwx

This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practiceshttps://amzn.to/30WQSm2

Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv

This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp

Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy

😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘†

Why do you need source code conventions ?

Why do you need source code conventions ?
(Clean Code Quality Seal : Orange)
Table of contents
Why do you need source code conventions in your company?
Why shall i establish code convention ? My IDE helps me search alredy if i need ?
OK, but what have code convention to do with data storage management and principles ?
Ok, but how can i make my repository visible and reusable to my developers ?
Ok convinced, but how can i establish good conventions  and what are good conventions?
Database and source code conventions
Some of the potential benefits that can be obtained by adopting a naming convention include the following:
Source code convention tools
Literature, good books and references
How can i subscribe/feed this blog ?
How can i rate this blog ?
Where do i find more clean code knowledge and gadgets?

Why do you need source code conventions in your company?

Hi there ! in this blog i wanna show you why i strong recommend to establish a good source code conventions in your company and why you’ll be developing faster and reusing classes with that. Code convention has been substimated and it is essencial in a company which want to stay competitive in the market. Good code conventions gives you a great vocabulary to communicate with other developers making it easy to understand and explain the code. It makes your code cleaner and reusable. Yes i sad reusable ! Are you curious now, how it is possible to make code reusable only by establishing code convention ? Read and share this blos to other developers. In the next section i’ll give you some ideias how to develop faster and increase your company revenue !

Why shall i establish code convention ? My IDE helps me search alredy if i need ?

I almost allways earn this answer, when i try to explain to developers why code covention is good for them. In fact that’s a good question, because it shows to me, that the developers are interessted on it and still don’t know or substimate the power of a good convention thinking that the IDE alredy solves this problem to him. The clue to find something quickly is the way your store it. The reason why we need code convention are justified in the theoretical bases of data storage. The secret of reusability i not only the fact that a class or interface was coded well, but also how to find it to reuse. To know that there is something good saving you a lot of time. This rule is gulty for evering thing you can imagine even in the market.
Example : Imagine you design a very good product. Let’s call it iPhone. To sell it successfully, two things has to come together. Customers must know, that your IPhone exists and most important: your customers should get from a store, if they want to buy it.
Imagine now that there is a unique store (your IDE) and there are a huge Stock. The stock is so big, (your IDE with thousends of classes and interfaces) that no customer is able to find the IPhone. Let’s make it a little more difficult: The store manager (you and me) does not have a catalog in which we have catalogazed the IPhones (classes in your IDE) we want to find, because we thouth that the store (our IDE) would solve this problem to us already. Your IPhone would flop! Without a system you’ll be just losing time searching a little bit and giving up by writing your own code again. How big is the probability that you find the class you may are looking for in a jungle of thousend trees ? Thats the way we work without code convention. Without class/interface catalog and no orientation the probability to find the right class or interface to reuse is very small. Thing about yourself ! How often do you search for a specific class/interface and really find it and also reuse it ? Ask yourself why ? This rate could be much more higher.

OK, but what have code convention to do with data storage management and principles ?

Well the clue to find something is the way you store something. Databases do that automatically and very efficient for you every time your store something in it. The reason why databases do that so good is that you can’t change the language it uses. (SQL) Database only says to you: use SQL, do something, but let me decide how i’ll do best for you. In Java or other object oriented languages we don’t have this barriere automatically and nothing is under control, because there are to many developers with diffent skill levels programming every day.
Example : (i have done this experiment also with my daughter): Imagine a very big old tree with thousends of leaves on it. You close your eyes and a mark one leave. (like we would store a new class in our IDE or repository) Now i say to you : Find this leave as fast as you can. Consider : There are thousend of green leaves(like in the search bar of our IDE) and they all look the same and very similar(like our classes in our IDE) My daughter sad to me: Papi you are crazy! How shall i do that ?
There is no chance and i bet with you that you also wouldn’t find it ! My daughter is ten year old. The same argument developers will give to you, if you aks them to reuse something they can’t find.
Remember that in your company the situation is more dramatically because of the factor time! And this is a good and acceptable reason. You would not be able to disagree with that, because it is the truth and reality. So now all the effort to code well is done! Nobody is reusing anything. Instead of that they are programing, programing and reprograming!!! That’s effective right? (no for sure not!!!)

Ok, but how can i make my repository visible and reusable to my developers ?

I sad to my daugther : Princeza, papi would find it in max. 5-7 tryes and in milliseconds! She sad: impossible! Well that’s very simple. What you need is not only the product (IDE or repository with classes) but also the catalog (code conventions) and the advertisement (cookbooks - very important) !
Databases have a schema how to store something in a way that you can refind it very fast again. One of those principles is the binary tree. The database saves something in a way that with each search iteration, you are able to cut the result into two reducing the fields binary. Thats a very powerfull way to search and find items, but you need that system to do so. You need a convention how to store it. What i’m telling you is nothing new. It is just something that to many companies and young developers substimates because they thing that in the 20ty centure things can be done faster and in an other way. That’t true, but only gulty to methods. Never for principles and physical rules! Notice that ! principles are static final variables. They are gulty over the universum. If you try to change that or work against that, you certainly early or later will get into troubles.

Ok convinced, but how can i establish good conventions  and what are good conventions?

Well, thats not so easy but it is possible and strongly recommended. Important : Always write down why you need the convention and what benefits it brings. Reason : If you leave the company or if one day a new developer asks you why you are doing those things in this way in the days of today, you will be able to share your expirience and explain to him, why there are things that never changes in life. (principles and constant rules) Also very important : Those conventions should not be in your PC, but futhermore as a small list glued on the front of your pc-screen visible to you. (developers are very lazy people) and every step we have to take, makes it harder to us to reuse.

Database and source code conventions

Minimal database conventions you should establish in your company :
·         Do not use (-)  to connect words. Reason : Some databases like MySQL don’t like this.
Bad : abc-def good : ABC_DEF
·         Allways write column names in big capital even if this doesn’t matter. Reason : be constant
Bad : abcDef good : ABC_DEF
·         Write column names always in the singular. Reason : Weltweiter standart
Bad : CAR_NAMES good : CAR_NAME
·         Avoid abbreviations or general names. Reason : avoid misunderstandings
Bad : NME or FIELD1 good : NAME or PREIS
·         Do not start field or table names with special characters. Reason : portability
Bad : 12_NAME, $NAME, *NAME good : NAME
·         Do not write white spaces. Reason : compatibility, portability
Bad : MY RING good : MY_RING
·         Primary keys has the same name as its table with the postfix ID. Reason : great developer still
Bad : Tablename: Preis, primary key Id good : PREIS, PREIS_ID
·         Foreign key is allways the name from the primary key. Reason : great developer still
Bad : Table: client has foreign key: foreign_Id_1 good : CLIENT, PREIS_ID
Minimal source code conventions best practices you should establish in your company :
·         Speak the same language. Why : See potential benefits.
Bad : to re-invent the wheel again good : see
Abode coding conventions and best practices

Some of the potential benefits that can be obtained by adopting a naming convention include the following:

·         to provide additional information (i.e., metadata) about the use to which an identifier is put;
·         to help formalize expectations and promote consistency within a development team;
·         to enable the use of automated refactoring or search and replace tools with minimal potential for error;
·         to enhance clarity in cases of potential ambiguity;
·         to enhance the aesthetic and professional appearance of work product (for example, by disallowing overly long names, comical or "cute" names, or abbreviations);
·         to help avoid "naming collisions" that might occur when the work product of different organizations is combined (see also: namespaces);
·         to provide meaningful data to be used in project handovers which require submission of program source code and all relevant documentation and
·         to provide better understanding in case of code reuse after a long interval of time.


😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘‡

Be sure to read, it will change your life!
Show your work by Austin Kleonhttps://amzn.to/34NVmwx

This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practiceshttps://amzn.to/30WQSm2

Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv

This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp

Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy

😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘†