183
社区成员
发帖
与我相关
我的任务
分享| The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZ |
|---|---|
| The Goal of This Assignment | Software Testing |
| Leader's MU STU ID and Name | 19103379_831902122 |
| Teammate 1's MU STU ID and Name | 19104065_831902128 |
| Teammate 2's MU STU ID and Name | 19103913_831902127 |
| Teammate 3's MU STU ID and Name | 19103964_831902125 |
| Teammate 4's MU STU ID and Name | 19104090_831902111 |
We test some functions of our back-end part of the program.
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bcslr.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.bcslr.entity.User;
import org.springframework.test.context.junit4.SpringRunner;
// import org.junit.rules.ExpectedException;
// import org.springframework.util.Assert;
// import static org.junit.Assert.*;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
class BcslrApplicationTests extends ServiceImpl<UserMapper, User> {
@Test
public void test() {
// add assert test code between Begin and End, no other change allowed
/***********************Begin**************************/
Assert.assertEquals("var1" , var2, var1); //
// for example
/************************End***************************/
}
}


The Junit assertEquals function can check whether two values are same, and give the corresponding output like the error information.
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bcslr.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.bcslr.entity.User;
import org.springframework.test.context.junit4.SpringRunner;
// import org.junit.rules.ExpectedException;
// import org.springframework.util.Assert;
// import static org.junit.Assert.*;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
class BcslrApplicationTests extends ServiceImpl<UserMapper, User> {
/*
test timeout function
*/
@Test()
public void testTimeout(){
log.info("yybzjbs");
while(true){
log.info("yybzjbs");
}
}
}

The Junit time test function are capable to prevent the function run for overlong time, it can prevent them to run when the function reaches the timeout function limitation and output the wrong information.
package com.example.bcslr;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import com.example.bcslr.Calculate;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
/**
* JUnit4 parameterized test
*/
@RunWith(Parameterized.class)
public class ParameterTest {
private final int input11;
private final int input22;
private final int expected;
public ParameterTest(int input11, int input22, int expected) {
this.input11 = input11;
this.input22 = input22;
this.expected = expected;
}
@Parameterized.Parameters()
public static Collection<Object> prepareData() {
return Arrays.asList(new Object[][]{
{3, 1, 2}, {5, 2, 3}//, {3,4,5}, {6,7,8}
});
}
@Test
public void testSub() {
Calculator cal = new Calculator();
assertEquals(cal.sub(input11, input22), expected);
}
// Calculator.java Junit Parameterized Test
/**
* Mathematical Calculation subtract
*/
public class Calculator {
public int sub(int a, int b) {
return a - b;
}
}
}

Junit parameterized test function makes it possible to run tests with multiple kinds of parameters, in we can test multiple parameters.
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bcslr.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.bcslr.entity.User;
import org.springframework.test.context.junit4.SpringRunner;
// import org.junit.rules.ExpectedException;
// import org.springframework.util.Assert;
// import static org.junit.Assert.*;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
class BcslrApplicationTests extends ServiceImpl<UserMapper, User> {
@Test
void testnegid() {
User user = new User();
user.setName("王宁");
user.setAge(-1);//怎么改id??
}
}

Junit Exception Test will stick with the functions in the original places and throw exceptions as usual.
package com.example.bcslr;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({TestDemo2.class,TestDemo.class})
public class TestSuite {
@org.junit.Test(timeout = 1000)
public void test2() {
// log.info("yybzjbs");
//log.info("yybzjbs");
System.out.println(1);
}
}

The two functions in the TestDemo2.java file are able to run fluently by using the junit.

The two functions in the TestDemo1.java file are able to run fluently by using the junit.

Junit Suite Test is capable to test the subset functions and files from the java files that include by Suite functions. In this way, it test all the functions and cases from TestDemo.java file and TestDemo2.java file. And, we can see the four cases all pass fluently like we test them individually, and we can see the running performance individually.