Spring Boot Project Sample
This section provides sample code for automatically generating UT cases based on a Spring Boot project.
For Spring Boot projects, test cases of the specified type can be generated based on the configuration class described by the @Configuration annotation.
@Configuration: This Spring configuration class analyzes the bean definition in the class of the annotation to determine the type constraint for generating a test case, that is, the type of the bean used by the generated test case.
Java code for generating a test case:
@Component
public class Zoo
{
@Autowired
private Tiger tiger;//Tiger is an interface with multiple implementations. In this test case, WhiteTiger defined by the Spring bean is used.
@Autowired
private Monkey monkey;//Monkey is an interface with multiple implementations. In this test case, WhiteMonkey defined by the Spring bean is used.
public String toString()
{
return tiger.getColor() + "\n" + monkey.getColor();
}
}
//Tiger interface
public interface Tiger {
String getColor();
}
//Tiger implementation
public class BlackTiger implements Tiger{
@Override
public String getColor() {
return "black";
}
}
//Tiger implementation
public class WhiteTiger implements Tiger{
@Override
public String getColor(){
return "white";
}
}
//Monkey interface
public interface Monkey {
String getColor();
}
//Monkey implementation
public class BlackMonkey implements Monkey{
@Override
public String getColor() {
return "black";
}
}
//Monkey implementation
public class WhiteMonkey implements Monkey{
@Override
public String getColor(){
return "white";
}
}
The configuration file refers to the Spring configuration class selected in the dialog box of generating test cases.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public Zoo zoo()
{
return new Zoo();
}
@Bean
public Tiger tiger()
{
return new WhiteTiger();//Bean definition in Spring
}
@Bean
public Monkey monkey()
{
return new WhiteMonkey();//Bean definition in Spring
}
}
The tiger and monkey types in the generated UT case are WhiteTiger and WhiteMonkey, respectively. The UT case is as follows:
public final class ZooTest {
@InjectMocks
private Zoo zoo; // @InjectMocks automatically creates a Zoo object and injects the object marked by @Mock into the Zoo object.
@Mock
private WhiteTiger whiteTigerMock; // @Mock automatically creates a WhiteTiger mock object, which is to be injected into the Zoo object.
@Mock
private WhiteMonkey whiteMonkeyMock; // @Mock automatically creates a WhiteMonkey mock object, which is to be injected into the Zoo object.
private AutoCloseable mockitoCloseable; // Clears Mockito resources.
///region Test suites for executable com.ruoyi.web.controller.common.Zoo.toString
///region SYMBOLIC EXECUTION: SUCCESSFUL EXECUTIONS for method toString()
/**
* @utgen.classUnderTest {@link Zoo}
* @utgen.methodUnderTest {@link Zoo#toString()}
* @utgen.invokes {@link com.ruoyi.web.controller.demo.domain.Tiger#getColor()}
* @utgen.invokes {@link com.ruoyi.web.controller.demo.domain.Monkey#getColor()}
* @utgen.returnsFrom {@code return tiger.getColor() + "\n" + monkey.getColor();}
*/
@UTgen("toString()") // @UTgen indicates that the current test method is generated by the UTgen tool and its attribute is the signature of the test method.
@Test
@DisplayName("toString: TigerGetColor -> return tiger.getColor() + \"\\n\" + monkey.getColor()")
public void testToString_MonkeyGetColor() {
(when(whiteTigerMock.getColor())).thenReturn(((String) null));
(when(whiteMonkeyMock.getColor())).thenReturn(((String) null));
String actual = zoo.toString();
String expected = "null\nnull";
assertEquals(expected, actual);
}
///endregion
///region FUZZER: SUCCESSFUL EXECUTIONS for method toString()
/**
* @utgen.classUnderTest {@link Zoo}
* @utgen.methodUnderTest {@link Zoo#toString()}
*/
@UTgen("toString()")
@Test
@DisplayName("toString: ")
public void testToString() throws ClassNotFoundException, IllegalAccessException, NoSuchFieldException, InvocationTargetException, NoSuchMethodException {
WhiteTiger whiteTiger = new WhiteTiger();
setField(zoo, "com.ruoyi.web.controller.common.Zoo", "tiger", whiteTiger);
WhiteMonkey whiteMonkey = new WhiteMonkey();
setField(zoo, "com.ruoyi.web.controller.common.Zoo", "monkey", whiteMonkey);
String actual = zoo.toString();
String expected = "white\nwhite";
assertEquals(expected, actual);
}
///endregion
///endregion
@BeforeEach
public void setUp() {
mockitoCloseable = openMocks(this); // Initializes the @Mock and @InjectMocks annotations and returns an AutoCloseable object for resource cleanup.
}
@AfterEach
public void tearDown() throws Exception {
mockitoCloseable.close(); // Releases the internal resources of Mockito and clears the Mock registration information generated for the current test to avoid resource leakage or status pollution.
}
}
Parent topic: Demos