Wednesday, August 29, 2018

GIT ERRORS

Error 1:git pull failed you have not concluded your merge MERGE_HEAD exists.exiting because of unfinished merge.You have not concluded your merge (MERGE_HEAD exists)

  • your previous pull failed and went into conflict and the conflict wasn't resolved properly before the next pull.soln is to undo the merge and pull again.
  1. git merge --abort //cmd for git version>1.7.4  or git reset --merge //cmd for git version<1.7.4
  2. git pull

Wednesday, April 25, 2018

POWERMOCKITO

For toast:
PowerMockito.mockStatic(Toast.class);
Toast toast=PowerMockito.mock(Toast.class);
PowerMockito.when(Toast.makeText(any(Context.class),any(String.class),any(Integer.class))).thenReturn(toast);

Mockito and Power Mockito – Cheatsheet

@RunWith(PowerMockRunner.class) – Tell Junit that run this test using PowerMockRunner
@PrepareForTest(A.class) – This is needed when we need to test static methods of A class
AService mock = PowerMockito.mock(A.class) – Creating a mock for A class
PowerMockito.when(mock.mockedMethod()).thenReturn(value) – When mockedMethod is called in the code, then return the value specified here.
PowerMockito.doNothing().when(mock).method() – do nothing when method() is called on mock object
Mockito.verify(mock).someMethod() – Verify that someMethod was called on mock once.
Mockito.verify(mock, times(n)).someMethod() – someMethod called n number of times
Mockito.verify(mock, never()).someMethod() – someMethod called n number of times
Mockito.verify(mock, atLeastOnce()).someMethod() – self explanatory
Mockito.verify(mock, atLeast(n)).someMethod() – self explanatory
Mockito.verify(mock, atMost(n)).someMethod() – self explanatory
Static
PowerMockito.mockStatic(A.class) – mock all static methods of class A
PowerMockito.doNothing().when(A.class)
A.staticMethod(value); – Nothing to be done when staticMethod(value) is called on class A
PowerMockito.doNothing().doThrow(new IllegalStateException()).when(A.class)
A.staticMethod(value); – Throw IllegalStateException when staticMethod(value) is called on class A
//We first have to inform PowerMock that we will now verify
//the invocation of a static method by calling verifyStatic.
PowerMockito.verifyStatic();
//Then we need to inform PowerMock about the method we want to verify.
//This is done by actually invoking the static
A.staticMethod();
@Before – annotation for a method that does the set up before starting the test.
InOrder verification
//First we have to let PowerMock know that the verification order is
//going to be important. This is done by calling Mockito.inOrder and passing
//it the mocked object.
InOrder inOrder = Mockito.inOrder(mock);
//Next, we can continue our verification using the inOrder instance
//using the same technique as seen earlier.
inOrder.verify(mock).isNew();
inOrder.verify(mock).update();
inOrder.verify(mock, Mockito.never()).create();
Constructor
PowerMockito.whenNew(A.class).withArguments(mock, “msg”).thenReturn(object)
PowerMockito.verifyNew(A.class).withArguments(mock, “msg”)
PowerMockito.verifyNew(A.class, times(n)).withArguments(mock, “msg”)
The class creating an object of A will be needed to be in @PrepareForTest
Matchers
PowerMockito.when(mock.method(Mockito.startsWith(“somestring”))).thenReturn(objValue);
Assert.assertSame(objValue, mock.method(“somestring123”));
Assert.assertSame(objValue, mock.method(“somestring456”));
PowerMockito.when(mock.method(Mockito.argThat(new ArgumentMatcher){public void matches(Object obj)….}).thenReturn(value); – Use the custom matcher to match the argument and return the value specified.
Mockito.eq(value)
Mockito.matches(regex)
Mockito.anyString, anyFloat, anyDouble, anyList, and so on
Mockito.isNull
Mockito.isNotNull
Mockito.isA
Mockito.endsWith
Answer Interface
When thenReturn() is not practical, use Answer interface
PowerMockito.when(mock.method()).then(new Answer<T>() {
public T answer(InvocationOnMock invocation) {
…invocation.getArguments()….
}
});
PowerMockito.mock(A.class, Answer obj) – This will act as default answer for all the invocation on this mock object.
Spy – Partial Mocking (some methods) of classes
//Following is the syntax to create a spy using the PowerMockito.spy method.
//Notice that we have to pass an actual instance of the EmployeeService class.
//This is necessary since a spy will only mock few methods of a class and
//invoke the real methods for all methods that are not mocked.
final EmployeeService spy = PowerMockito.spy(new EmployeeService());
//Notice that we have to use the PowerMockito.doNothing().when(spy).createEmployee()
//syntax to create the spy. This is required because if we use the
//PowerMockito.when(spy.createEmployee()) syntax will result in calling
//the actual method on the spy.
//Hence, remember when we are using spies,
//always use the doNothing(), doReturn() or the //doThrow() syntax only. PowerMockito.doNothing().when(spy)
.createEmployee(employeeMock);
Mocking private methods
PowerMockito.doNothing().when(spy,
“createEmployee”, employeeMock);
PowerMockito.verifyPrivate(spy)
.invoke(“createEmployee”, employeeMock);
==========================================================
https://github.com/powermock/powermock/wiki/MockPrivate



Thursday, March 15, 2018

SERVICES VS BROADCAST RECEIVERS

Service
1)Introduction
2)Lifecycle
3)Intent Service
4)Foreground Service
5)Bound Service
6)AIDL

Broadcast Receiver
1)Introduction
2)Pending Intents
3)Statically and Dynamically registered receivers
4)Calling a Service from it.
5)Normal vs Ordered vs Sticky Broadcast receivers.
6)Real usecase example with code.
7)Local Broadcastmanager and their use.

Tuesday, January 30, 2018

PARCELABLE VS SERIALIZABLE

  1. To pass primitive data types like string, integer, float, we use intents which is very direct.Add key name and the respective data type value in the Intents but for class objects,then we need to use Serialization or Parcelable.
  2. For marshalling and unmarshalling of java objects,Parcelable and Serialization are used.Java serialization or Anroid parcelable(pass object references between activities)
Serialization:
  1. Serialization is a marker interface, which implies the user cannot marshal the data according to their requirements. It is a marker interface as it converts an object into a stream using the Java reflection API.
  2. In Serialization, a marshaling operation is performed on a Java Virtual Machine (JVM) using the Java reflection API and this helps identify the Java objects member and behavior, but also ends up creating a lot of garbage objects during the stream conversation process.Due to this, the Serialization process is slow in comparison to Parcelable.
  3. Serialization, on the other hand, is a Java interface that allows users to implement the interface which gets marked as Serializable.
  4. Android developers prefer Parcelable over Serialization technique as Serialization is available in Java.


Parcelable:

  1. Parcelable is faster than Serialization makes it a preferred choice of approach while passing an object because Android Parcelable implementation allows objects to read and write from Parcels which can contain flattened data inside message containers.We can write custom code for marshaling and unmarshaling so it creates fewer garbage objects using Parcelable. The performance of Parcelable over Serialization dramatically improves (around two times faster), because of this custom implementation.
  2. If a developer wants to convert a Java object into Parcelable, then the best way to do so is by implementing the Parcelable interface and overriding the writeToParcel() methods in its own class.
  3.   The first step is to override the writeToParcel() method and write all object members into parcel objects.
  4.   The second is to create a static Parcelable.Creator object to de-serialize the Java object. Parcelable is well documented in the Android SDK;
  5. In Parcelable, One of the main reasons was the fact that Parcelable is fully customizable, allowing developers to convert required data into Parcelable. 
  6. Parcelable examples:
            1)@Override 
protected void onCreate(Bundle savedIntanceState){
super.onCreate(savedInstanceState);
}
                    Bundle should implement Parcelable

            2) MyClass myClassObj=new MyClass();

Intent myIntent=new Intent();
myIntent.putExtra("myClassObj",myClassObj);
                 MyClass should implement Parcelable

Saturday, January 20, 2018

BUTTERKNIFE

Where to use butterknife

1)Drawables, String, Boolean, Integer, Color and Dimension resources.

2) static class ViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.image_view)
    ImageView imageView;
    @BindView(R.id.TextView)
    TextView text_view;
    
    ViewHolder(View view) {
        super(view);
        ButterKnife.bind(this, view);
    }
}
Note the same with fragments public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view=inflater.inflate(R.layout.xxx,container,false);
ButterKnife.bind(this,view);
return view;
}

3) View lists
@BindViews({ R.id.button_flash, R.id.button_timer, R.id.button_hdr})
List<Button> camControls;
Now we have all the views in one list. Lets write VISIBILITY setter for simple show/hide behavior.
i)final ButterKnife.Setter<View, Integer> VISIBILITY = new ButterKnife.Setter<View,Integer>() {
    @Override
    public void set(@NonNull View view, Integer value, int index) {
        view.setVisibility(value);
    }
};

ii)Lambda Expressions
final ButterKnife.Setter<View, Integer> VISIBILITY 
          = (view, value, index) -> view.setVisibility(value);
ButterKnife.apply(camControls, VISIBILITY, View.GONE);

5)Listener Binding

  • @OnCheckedChange
  • @OnClick
  • @OnLongClick
  • @OnEditorAction
  • @OnFocusChange
  • @OnItemClick
  • @OnItemLongClick
  • @OnTouch
  • @OnTextChanged