How to write Android App code including Unit Tests in minutes using Gemini AI?

Published: 13 May 2024
on channel: Programmer World
309
2

In this video it shows the steps to use Gemini AI tool to generate Android App code and write the respective Java unit Test cases.

Gemini Prompts used:
1. Write a simple addition method for Android java code?
2. can you please provide unit test for the above?


I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]

Complete source code and other details/ steps of this video are posted in the below link:
https://programmerworld.co/android/ho...


However, the main Java code is copied below also for reference:


package com.programmerworld.gemini_appandunittestcodegeneration;

import android.os.Bundle;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) - {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});

TextView textView = findViewById(R.id.textView);
textView.setText(String.valueOf(AddCustomMethod(1, 2)));
}

public int AddCustomMethod(int a, int b) {
return a + b;
}
}


--


Unit Test Case code:


package com.programmerworld.gemini_appandunittestcodegeneration;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

import static org.junit.Assert.*;

@RunWith(RobolectricTestRunner.class)
public class ExampleUnitTest {

private MainActivity mainActivity;

@Before
public void setUp() {
mainActivity = Robolectric.buildActivity(MainActivity.class).create().get();
}

@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}

@Test
public void add() {
// Test positive numbers
assertEquals(8, mainActivity.AddCustomMethod(5, 3));
assertEquals(10, mainActivity.AddCustomMethod(7, 3));

// Test negative numbers
assertEquals(-2, mainActivity.AddCustomMethod(-5, 3));
assertEquals(-10, mainActivity.AddCustomMethod(-7, -3));

// Test zero
assertEquals(0,mainActivity.AddCustomMethod(0, 0));
assertEquals(5, mainActivity.AddCustomMethod(5, 0));
assertEquals(-5, mainActivity.AddCustomMethod(0, -5));
}
}


--


On this page of the site you can watch the video online How to write Android App code including Unit Tests in minutes using Gemini AI? with a duration of hours minute second in good quality, which was uploaded by the user Programmer World 13 May 2024, share the link with friends and acquaintances, this video has already been watched 309 times on youtube and it was liked by 2 viewers. Enjoy your viewing!