Skip to content

Commit c8295c5

Browse files
committed
Add tests for Java examples
1 parent 6779d28 commit c8295c5

35 files changed

+1127
-1184
lines changed

java/pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
<artifactId>google-genai</artifactId>
2121
<version>0.2.0</version>
2222
</dependency>
23+
24+
<dependency>
25+
<groupId>org.junit.jupiter</groupId>
26+
<artifactId>junit-jupiter-engine</artifactId>
27+
<version>5.9.1</version>
28+
<scope>test</scope>
29+
</dependency>
2330
</dependencies>
2431

2532
</project>

java/src/main/java/com/example/gemini/BuildConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
package com.example.gemini;
1818

1919
public class BuildConfig {
20-
public static String media_path = "third_party/";
20+
public static String media_path = "../third_party/";
2121
}

java/src/main/java/com/example/gemini/CodeExecutionRequestOverride.java renamed to java/src/main/java/com/example/gemini/CodeExecution.java

+77-8
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,84 @@
1717
package com.example.gemini;
1818

1919
import com.google.genai.Client;
20-
import com.google.genai.types.*;
21-
import org.apache.http.HttpException;
20+
import com.google.genai.types.GenerateContentConfig;
21+
import com.google.genai.types.GenerateContentResponse;
22+
import com.google.genai.types.Part;
23+
import com.google.genai.types.Tool;
24+
import com.google.genai.types.ToolCodeExecution;
2225

23-
import java.io.IOException;
2426
import java.util.Collections;
2527

26-
public class CodeExecutionRequestOverride {
27-
public static void main(String[] args) throws IOException, HttpException {
28+
@SuppressWarnings("resource")
29+
public class CodeExecution {
30+
public static GenerateContentResponse CodeExecutionBasic() throws Exception {
31+
// [START code_execution_basic]
32+
Client client = new Client();
33+
34+
String prompt = """
35+
Write and execute code that calculates the sum of the first 50 prime numbers.
36+
Ensure that only the executable code and its resulting output are generated.
37+
""";
38+
39+
GenerateContentResponse response =
40+
client.models.generateContent(
41+
"gemini-2.0-pro-exp-02-05",
42+
prompt,
43+
null);
44+
45+
for (Part part : response.candidates().get().getFirst().content().get().parts().get()) {
46+
System.out.println(part + "\n");
47+
}
48+
49+
System.out.println("-".repeat(80));
50+
System.out.println(response.text());
51+
// [END code_execution_basic]
52+
53+
/*
54+
[START code_execution_basic_return]
55+
Expected output:
56+
--------------------------------------------------------------------------------
57+
```python
58+
import math
59+
60+
def is_prime(n):
61+
"""Checks if a number is prime."""
62+
if n < 2:
63+
return False
64+
if n == 2:
65+
return True
66+
if n % 2 == 0:
67+
return False
68+
# Check only odd divisors up to the square root
69+
for i in range(3, int(math.sqrt(n)) + 1, 2):
70+
if n % i == 0:
71+
return False
72+
return True
73+
74+
count = 0
75+
num = 2
76+
prime_sum = 0
77+
target_count = 50
78+
79+
while count < target_count:
80+
if is_prime(num):
81+
prime_sum += num
82+
count += 1
83+
num += 1
84+
85+
print(prime_sum)
86+
```
87+
88+
Output:
89+
```
90+
5117
91+
```
92+
[END code_execution_basic_return]
93+
*/
94+
return response;
95+
}
96+
97+
public static GenerateContentResponse CodeExecutionRequestOverride() throws Exception {
2898
// [START code_execution_request_override]
2999
Client client = new Client();
30100

@@ -42,13 +112,13 @@ public static void main(String[] args) throws IOException, HttpException {
42112
)
43113
).build();
44114

45-
46115
GenerateContentResponse response =
47116
client.models.generateContent(
48117
"gemini-2.0-flash",
49118
prompt,
50119
config);
51120

121+
System.out.println("-".repeat(80));
52122
System.out.println(response.executableCode());
53123
System.out.println(response.codeExecutionResult());
54124
// [END code_execution_request_override]
@@ -57,7 +127,6 @@ public static void main(String[] args) throws IOException, HttpException {
57127
[START code_execution_request_override_return]
58128
Expected output:
59129
--------------------------------------------------------------------------------
60-
61130
def is_prime(n):
62131
if n <= 1:
63132
return False
@@ -84,8 +153,8 @@ def is_prime(n):
84153
85154
primes=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229]
86155
sum(primes)=5117
87-
88156
[END code_execution_request_override_return]
89157
*/
158+
return response;
90159
}
91160
}

java/src/main/java/com/example/gemini/CodeExecutionBasic.java

-92
This file was deleted.

java/src/main/java/com/example/gemini/ConfigureModelParameters.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@
1919
import com.google.genai.Client;
2020
import com.google.genai.types.GenerateContentConfig;
2121
import com.google.genai.types.GenerateContentResponse;
22-
import org.apache.http.HttpException;
22+
import org.jspecify.annotations.Nullable;
2323

24-
import java.io.IOException;
2524
import java.util.List;
2625

2726
public class ConfigureModelParameters {
28-
public static void main(String[] args) throws IOException, HttpException {
27+
public static @Nullable String ContentGneConfigureModelParameters() throws Exception {
2928
// [START configure_model_parameters]
3029
Client client = new Client();
3130

@@ -45,5 +44,6 @@ public static void main(String[] args) throws IOException, HttpException {
4544

4645
System.out.println(response.text());
4746
// [END configure_model_parameters]
47+
return response.text();
4848
}
4949
}

0 commit comments

Comments
 (0)