主要pom文件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <!--<exclusions>--> <!--<exclusion>--> <!--<groupId>org.junit.vintage</groupId>--> <!--<artifactId>junit-vintage-engine</artifactId>--> <!--</exclusion>--> <!--</exclusions>--> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> </dependency>
完整测试类和导入jar
import com.student.validatordemo.bean.ValidationBeanModel; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import java.util.*; //import org.junit.jupiter.api.Test; //import org.junit.jupiter.api.Test; @RunWith(SpringRunner.class) @SpringBootTest public class ValidatordemoApplicationTests { private Validator validator; public ValidatordemoApplicationTests(){ } @Before public void initValidator() { ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); validator = validatorFactory.getValidator(); } // @Test public void contextLoads() { } /** * 在myAssertFalse属性上加@AssertFalse注解 * <p> * 程序输出: com.aspire.model.ValidationBeanModel$AbcAssertFalse类的myAssertFalse属性 -> 只能为false */ @Test public void testAssertFalse() { ValidationBeanModel.AbcAssertFalse vm = new ValidationBeanModel().new AbcAssertFalse(); vm.setMyAssertFalse(true); fa(vm); }