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));
}
}
--
Watch video How to write Android App code including Unit Tests in minutes using Gemini AI? online without registration, duration hours minute second in high quality. This video was added by user Programmer World 13 May 2024, don't forget to share it with your friends and acquaintances, it has been viewed on our site 309 once and liked it 2 people.