What's the best way to test my mvp presenter? I've read a ton of tutorials and each one makes me even more confused. I'm just starting to learn about testing and I figured it'd be easier to start with the presenter.

My Presenter

public class HymnPresenter implements HymnPresenterContract, HymnRepository.HymnsCallback{

    private HymnRepository hymnRepository;
    private HymnsViewContract hymnViewContract;

    public HymnPresenter(HymnRepository repository, HymnsViewContract viewContract) {
        this.hymnViewContract = viewContract;
        this.hymnRepository = repository.attach(this);
    }

    @Override
    public void addHymns(ArrayList<Hymn> hymns){
        hymnRepository.addHymns(hymns);
    }

    @Override
    public void addHymn(Hymn hymn){
        hymnRepository.addHymn(hymn);
    }

    @Override
    public void handleSuccess() {
        hymnViewContract.success();
    }

    @Override
    public void handleError(String error) {
        hymnViewContract.error(error);
    }
} 

My Repository I'm using the firebase firestore db to store the hymns

public class HymnRepository implements HymnPresenterContract{

    private HymnsCallback hymnsCallback;
    private CollectionReference collectionReference;
    private FirebaseFirestore db;

    public HymnRepository(FirebaseFirestore database, @HymnCollection CollectionReference collection) {
        this.db = database;
        this.collectionReference = collection;
    }

    public HymnRepository attach(@NonNull HymnsCallback callback){
        this.hymnsCallback = callback;
        return this;
    }

    @Override
    public void addHymns(ArrayList<Hymn> hymns){
        WriteBatch batch = db.batch();
        for(int i = 0; i < hymns.size(); i++){
            batch.set(collectionReference.document(), hymns.get(i));
        }
        batch.commit()
                .addOnSuccessListener(documentReference -> hymnsCallback.handleSuccess())
                .addOnFailureListener(e -> hymnsCallback.handleError(e.toString()));
    }

    @Override
    public void addHymn(Hymn hymn) {
        collectionReference.add(hymn)
                .addOnSuccessListener(documentReference -> hymnsCallback.handleSuccess())
                .addOnFailureListener(e -> hymnsCallback.handleError(e.toString()));
    }

    public interface HymnsCallback {
        void handleSuccess();
        void handleError(String error);
    }
}

My presenter contract

public interface HymnPresenterContract {
    void addHymns(ArrayList<Hymn> hymns);
    void addHymn(Hymn hymn);
}

My view contract

public interface HymnsViewContract {
    void success();
    void error(String error);
}
New contributor
mayore is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

I recommend you look into Mockito as it is the go to mocking library for Android. You are on the right track with your code structure, using the Contract pattern to have interfaces define your requirements.

You can then setup JVM tests (tests that run on your machine vs the device) where you can test your presenter.

I recommend reading through this tutorial to get a better understanding of how to test your code.

  • Thanks. I'll give it a shot. Hope this time I'll understand what's happening – mayore 4 hours ago
  • Definitely feel free to message me if you need to discuss more! – JonMan08 1 hour ago

Your Answer

mayore is a new contributor. Be nice, and check out our Code of Conduct.
 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.