Common Projects
This section provides sample code for automatically generating
Java code for generating a test case:
public class Zoo
{
private Tiger tiger;//Tiger is an interface with multiple implementations.
private Monkey monkey;//Monkey is an interface with multiple implementations.
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";
}
}
Use different mocking strategies to generate UT cases.
//UT case generated when the mocking strategy is Do not mock.
public final class ZooTest {
///region Test suites for executable com.ruoyi.web.controller.common.Zoo.toString
///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 Exception {
Zoo zoo = ((Zoo) createInstance("com.ruoyi.web.controller.common.Zoo"));
BlackTiger tiger = new BlackTiger();
setField(zoo, "com.ruoyi.web.controller.common.Zoo", "tiger", tiger);
BlackMonkey monkey = new BlackMonkey();
setField(zoo, "com.ruoyi.web.controller.common.Zoo", "monkey", monkey);
String actual = zoo.toString();
String expected = "black\nblack";
assertEquals(expected, actual);
}
/**
* @utgen.classUnderTest {@link Zoo}
* @utgen.methodUnderTest {@link Zoo#toString()}
*/
@UTgen("toString()")
@Test
@DisplayName("toString: ")
public void testToString1() throws Exception {
Zoo zoo = ((Zoo) createInstance("com.ruoyi.web.controller.common.Zoo"));
BlackTiger tiger = new BlackTiger();
setField(zoo, "com.ruoyi.web.controller.common.Zoo", "tiger", tiger);
WhiteMonkey monkey = new WhiteMonkey();
setField(zoo, "com.ruoyi.web.controller.common.Zoo", "monkey", monkey);
String actual = zoo.toString();
String expected = "black\nwhite";
assertEquals(expected, actual);
}
/**
* @utgen.classUnderTest {@link Zoo}
* @utgen.methodUnderTest {@link Zoo#toString()}
*/
@UTgen("toString()")
@Test
@DisplayName("toString: ")
public void testToString2() throws Exception {
Zoo zoo = ((Zoo) createInstance("com.ruoyi.web.controller.common.Zoo"));
WhiteTiger tiger = new WhiteTiger();
setField(zoo, "com.ruoyi.web.controller.common.Zoo", "tiger", tiger);
WhiteMonkey monkey = new WhiteMonkey();
setField(zoo, "com.ruoyi.web.controller.common.Zoo", "monkey", monkey);
String actual = zoo.toString();
String expected = "white\nwhite";
assertEquals(expected, actual);
}
/**
* @utgen.classUnderTest {@link Zoo}
* @utgen.methodUnderTest {@link Zoo#toString()}
*/
@UTgen("toString()")
@Test
@DisplayName("toString: ")
public void testToString3() throws Exception {
Zoo zoo = ((Zoo) createInstance("com.ruoyi.web.controller.common.Zoo"));
WhiteTiger tiger = new WhiteTiger();
setField(zoo, "com.ruoyi.web.controller.common.Zoo", "tiger", tiger);
BlackMonkey monkey = new BlackMonkey();
setField(zoo, "com.ruoyi.web.controller.common.Zoo", "monkey", monkey);
String actual = zoo.toString();
String expected = "white\nblack";
assertEquals(expected, actual);
}
///endregion
///region FUZZER: TIMEOUTS for method toString()
/**
* @utgen.classUnderTest {@link Zoo}
* @utgen.methodUnderTest {@link Zoo#toString()}
*/
@UTgen("toString()")
@Test
@DisplayName("toString: ")
@Timeout(value = 1000L, unit = TimeUnit.MILLISECONDS)
public void testToString4() throws Exception {
Zoo zoo = ((Zoo) createInstance("com.ruoyi.web.controller.common.Zoo"));
WhiteTiger tiger = new WhiteTiger();
setField(zoo, "com.ruoyi.web.controller.common.Zoo", "tiger", tiger);
WhiteMonkey monkey = new WhiteMonkey();
setField(zoo, "com.ruoyi.web.controller.common.Zoo", "monkey", monkey);
/* This execution may take longer than the 1000 ms timeout
and therefore fail due to exceeding the timeout. */
assertTimeoutPreemptively(Duration.ofMillis(1000L), () -> zoo.toString());
}
///endregion
///endregion
}
//UT case generated when the mocking strategy is Auto mock and Mock outside the class.
public final class ZooTest {
@InjectMocks
private Zoo zoo;
@Mock
private Tiger tigerMock;
@Mock
private Monkey monkeyMock;
private AutoCloseable mockitoCloseable;
///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 Tiger#getColor()}
* @utgen.invokes {@link Monkey#getColor()}
* @utgen.returnsFrom {@code return tiger.getColor() + "\n" + monkey.getColor();}
*/
@UTgen("toString()")
@Test
@DisplayName("toString: TigerGetColor -> return tiger.getColor() + \"\\n\" + monkey.getColor()")
public void testToString_MonkeyGetColor() {
(when(tigerMock.getColor())).thenReturn(((String) null));
(when(monkeyMock.getColor())).thenReturn(((String) null));
String actual = zoo.toString();
String expected = "null\nnull";
assertEquals(expected, actual);
}
///endregion
/// endregion
@BeforeEach
public void setUp() {
mockitoCloseable = openMocks(this);
}
@AfterEach
public void tearDown() throws Exception {
mockitoCloseable.close();
}
}
Parent topic: Demos