简体   繁体   English

带注释的JUnit测试

[英]JUnit testing with annotations

Hey all I am trying to get these Unit tests to fail but can't it uses annotations, which is new to me. 嘿,我正在尝试使这些单元测试失败,但不能使用注释,这对我来说是新的。 Any ideas would be great! 任何想法都很棒!

I have been trying all sorts of ways to get them to fail by either setting the test class variables to null, or trying to use if/else statements in the minimum test, but they always come out passing. 我一直在尝试通过将测试类变量设置为null或尝试在最小测试中使用if / else语句使它们失败的各种方法,但是它们总是通过。 Is this correct? 这个对吗?

public class ValidationServiceTest extends BaseServiceTest {
    ValidationService validationService;
    ValidationException ve;
    TestDto test;
    Field f;

    @Before
    public void setup() {
        validationService = new ValidationService();
        ve = null;
    }

    @Test
    public void validateNotNull(){
        try {
            validationService.validate(ve, test.xx);
            assertNotNull("testing notNull()", ve);
        } catch (Exception e) {
        }
    }

    @Test
    public void validateMin(){
        try {
            validationService.validate(ve, test.xy);
            if(test.xy > f.min()){
                assertTrue("testing min()" , test.xy > -1);
            }
        } catch (Exception e) {
        }
    }

    public class TestDto{
        @Field(notNull=true)
        public Integer xx = null;
        @Field(min=2)
        public Integer xy = -5;
    }
}

Do not catch Exception, mark test method with throws Exception instead. 不要捕获Exception,而是用throws Exception标记测试方法。

The test field is never initialized in your test, which means it's null . test字段永远不会在您的测试中初始化,这意味着它为null This causes NullPointerException whenever you try to access fields of test . 每当您尝试访问test字段时,这都会导致NullPointerException。 The assertion line is skipped and exception is suppressed in the catch clause. catch子句中的断言行被跳过,异常被抑制。 Removing the try/catch block and marking test method with throws Exception instead will cause the test to report error and you will see what's wrong instantly. 删除try / catch块并使用throws Exception标记测试方法将导致测试报告错误,并且您会立即发现问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM