setup tests for HelloWorldService
This commit is contained in:
158
src/test/java/com/example/service/HelloWorldServiceImplTest.java
Normal file
158
src/test/java/com/example/service/HelloWorldServiceImplTest.java
Normal file
@ -0,0 +1,158 @@
|
||||
package com.example.service;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Unit tests for HelloWorldServiceImpl
|
||||
* Tests the basic Hello World API functionality
|
||||
*/
|
||||
@DisplayName("HelloWorldServiceImpl Tests")
|
||||
class HelloWorldServiceImplTest {
|
||||
|
||||
private HelloWorldServiceImpl service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new HelloWorldServiceImpl();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return greeting with valid name")
|
||||
void testGetHelloWorld_WithValidName() {
|
||||
// Arrange
|
||||
String name = "John";
|
||||
String expected = "Hello World, John!";
|
||||
|
||||
// Act
|
||||
String result = service.getHelloWorld(name);
|
||||
|
||||
// Assert
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return greeting with empty string")
|
||||
void testGetHelloWorld_WithEmptyString() {
|
||||
// Arrange
|
||||
String name = "";
|
||||
String expected = "Hello World, !";
|
||||
|
||||
// Act
|
||||
String result = service.getHelloWorld(name);
|
||||
|
||||
// Assert
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should handle null input gracefully")
|
||||
void testGetHelloWorld_WithNull() {
|
||||
// Arrange
|
||||
String name = null;
|
||||
String expected = "Hello World, null!";
|
||||
|
||||
// Act
|
||||
String result = service.getHelloWorld(name);
|
||||
|
||||
// Assert
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should handle special characters in name")
|
||||
void testGetHelloWorld_WithSpecialCharacters() {
|
||||
// Arrange
|
||||
String name = "Alice@123";
|
||||
String expected = "Hello World, Alice@123!";
|
||||
|
||||
// Act
|
||||
String result = service.getHelloWorld(name);
|
||||
|
||||
// Assert
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should handle names with spaces")
|
||||
void testGetHelloWorld_WithSpaces() {
|
||||
// Arrange
|
||||
String name = "John Doe";
|
||||
String expected = "Hello World, John Doe!";
|
||||
|
||||
// Act
|
||||
String result = service.getHelloWorld(name);
|
||||
|
||||
// Assert
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should handle long names")
|
||||
void testGetHelloWorld_WithLongName() {
|
||||
// Arrange
|
||||
String name = "ThisIsAVeryLongNameForTestingPurposes";
|
||||
String expected = "Hello World, ThisIsAVeryLongNameForTestingPurposes!";
|
||||
|
||||
// Act
|
||||
String result = service.getHelloWorld(name);
|
||||
|
||||
// Assert
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should return non-null result")
|
||||
void testGetHelloWorld_ReturnsNonNull() {
|
||||
// Arrange
|
||||
String name = "TestUser";
|
||||
|
||||
// Act
|
||||
String result = service.getHelloWorld(name);
|
||||
|
||||
// Assert
|
||||
assertNotNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should start with 'Hello World,'")
|
||||
void testGetHelloWorld_StartsWithHelloWorld() {
|
||||
// Arrange
|
||||
String name = "User";
|
||||
|
||||
// Act
|
||||
String result = service.getHelloWorld(name);
|
||||
|
||||
// Assert
|
||||
assertTrue(result.startsWith("Hello World,"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should end with exclamation mark")
|
||||
void testGetHelloWorld_EndsWithExclamation() {
|
||||
// Arrange
|
||||
String name = "User";
|
||||
|
||||
// Act
|
||||
String result = service.getHelloWorld(name);
|
||||
|
||||
// Assert
|
||||
assertTrue(result.endsWith("!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should contain the provided name in result")
|
||||
void testGetHelloWorld_ContainsName() {
|
||||
// Arrange
|
||||
String name = "TestUser";
|
||||
|
||||
// Act
|
||||
String result = service.getHelloWorld(name);
|
||||
|
||||
// Assert
|
||||
assertTrue(result.contains(name));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user