普通项目示例
本章节基于普通项目提供对应的自动生成
需要生成测试用例的代码(Java代码):
public class Zoo
{
private Tiger tiger;//Tiger是一个接口,有多个实现。
private Monkey monkey;//Monkey是一个接口,有多个实现。
public String toString()
{
return tiger.getColor() + "\n" + monkey.getColor();
}
}
//Tiger接口
public interface Tiger {
String getColor();
}
//Tiger的实现
public class BlackTiger implements Tiger{
@Override
public String getColor() {
return "black";
}
}
//Tiger的实现
public class WhiteTiger implements Tiger{
@Override
public String getColor(){
return "white";
}
}
//Monkey的接口
public interface Monkey {
String getColor();
}
//Monkey的实现
public class BlackMonkey implements Monkey{
@Override
public String getColor() {
return "black";
}
}
//Monkey的实现
public class WhiteMonkey implements Monkey{
@Override
public String getColor(){
return "white";
}
}
使用不同的Mocking strategy生成的UT测试用例:
//Mocking strategy为Do not mock生成的UT测试用例
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
}
//Mocking strategy为Auto mock、Mock outside the class生成的UT测试用例
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();
}
}
父主题: 案例Demo