Junit单元测试之断言(Assert)

Junit为我们提供了一些辅助函数,他们用来帮助我们确定被测试的方法是否按照预期的效果正常工作。

一、assertEquals 

函数原型1:

    Assert.assertEquals([String message],expected,actual) 

参数说明: 

message(可选):将会在发生错误时报告这个消息。 

expected(必填):期望值,通常都是用户指定的内容。 

actual(必填):是被测试的代码返回的实际值。 

例:

    Assert.assertEquals("equals","1","1"); 

函数原型2:

    Assert.assertEquals([String message],expected,actual,tolerance) 

参数说明: 

message(可选):将会在发生错误时报告这个消息。 

expected(必填):期望值,通常都是用户指定的内容。 

actual(必填):被测试的代码返回的实际值。
 
tolerance(选填):误差参数,参加比较的两个浮点数在这个误差之内则会被认为是相等的。 

例:

    Assert.assertEquals ("yes",5.8,11.0/2.0,0.5); 

二、assertTrue 

函数原型:

    Assert.assertTrue ([String message],Boolean condition) 

参数说明: 

message(可选):将会在发生错误时报告这个消息。 

condition(必填):待验证的布尔型值。 

该断言用来验证给定的布尔型值是否为真,假如结果为假,则验证失败。当然,更有验证为假的测试条件: 

函数原型:

    Assert.assertFalse([String message],Boolean condition) 

该断言用来验证给定的布尔型值是否为假,假如结果为真,则验证失败。 

例:

Assert.assertTrue("true",1==1); 

Assert.assertFalse("false",2==1); 

三、assertNull 

函数原型:

    Assert.assertNull([String message],Object object) 

参数说明: 

message(可选):将会在发生错误时报告这个消息。 

object(必填):待验证的对象。 

该断言用来验证给定的对象是否为null,假如不为null,则验证失败。相应地,还存在能够验证非null的断言: 

函数原型:

    Assert.assertNotNull([String message],Object object) 

该断言用来验证给定的对象是否为非null,假如为null,则验证失败。 

例:

Assert.assertNull("null",null); 

Assert.assertNotNull("not null",new String()); 

四、assertSame 

函数原型:

    Assert.assertSame ([String message], expected, actual) 

参数说明: 

message是个可选的消息,假如提供,将会在发生错误时报告这个消息。 

expected是期望值。 

actual是被测试的代码返回的实际值。 

该断言用来验证expected参数和actual参数所引用的是否是同一个对象,假如不是,则验证失败。相应地,也存在验证不是同一个对象的断言: 

函数原型:

    Assert.assertNotSame ([String message], expected, actual) 

该断言用来验证expected参数和actual参数所引用的是否是不同对象,假如所引用的对象相同,则验证失败。 

例:

Assert.assertSame("same",2,4-2); 

Assert.assertNotSame("not same",2,4-3); 

五、fail 

函数原型:

    Assert.fail(actual, expected, [String message], operator)

参数说明: 

actual(必填):被测试的代码返回的实际值。

expected(必填):期望值,通常都是用户指定的内容。 

message(可选):会在发生错误时报告这个消息。 

operator(必填):在发生错误时,actual值和expexted中间的分隔符。

抛出一个 AssertionError。如果 message 是假值,错误信息会被设置为被 operator 分隔在两边 actual 和 expected 的值。否则,该错误信息会是 message 的值。该断言会使测试立即失败,通常用在测试不能达到的分支上(如异常)。

例:

Assert.fail(1, 2, undefined, '>');// AssertionError: 1 > 2

Assert.fail(1, 2, 'whoops', '>');// AssertionError: whoops

JUnit单元测试之断言介绍到此结束。

  • 11
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
如果你按照我的建议修改了代码,使用了getBeansWithAnnotation()方法,并且测试仍然失败,那么你可以考虑以下几个方面: 1. 检查是否正确引入了MyCustomAnnotation注解。可以在测试类中添加如下代码片段,以确保MyCustomAnnotation注解被正确引入: ``` import org.springframework.stereotype.Component; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.junit.Test; import org.junit.runner.RunWith; import static org.junit.Assert.assertTrue; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Component public @interface MyCustomAnnotation { // ... } @RunWith(SpringRunner.class) @SpringBootTest public class DynamicDatasourceDemoApplicationTests { @Autowired private ApplicationContext applicationContext; @Test public void contextLoads() { boolean hasMyCustomAnnotation = applicationContext.getBeansWithAnnotation(MyCustomAnnotation.class).size() > 0; assertTrue(hasMyCustomAnnotation); } } ``` 在这个测试类中,我添加了一个import语句,以引入@Component注解。这是因为MyCustomAnnotation注解没有指定自己是一个组件,因此它不会被扫描并注册为Bean。在这种情况下,我们可以通过@Component注解来告诉Spring将该注解标记的类注册为Bean。 2. 检查是否正确定义了MyCustomAnnotation注解。你可以参考以下代码片段,确认注解的定义是否正确: ``` @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Component public @interface MyCustomAnnotation { // ... } ``` 在这个代码片段中,我添加了@Component注解,以确保该注解被正确扫描并注册为Bean。如果你的代码中也缺少了@Component注解,你可以参考这个示例来修改代码。 3. 检查是否正确注入了ApplicationContext。你可以在测试类中添加如下代码片段,以确保ApplicationContext被正确注入: ``` @RunWith(SpringRunner.class) @SpringBootTest public class DynamicDatasourceDemoApplicationTests { @Autowired private ApplicationContext applicationContext; @Test public void contextLoads() { assertNotNull(applicationContext); } } ``` 在这个代码片段中,我添加了一个assertNotNull()方法,以确保ApplicationContext被正确注入。如果该测试方法失败,说明ApplicationContext没有被正确注入。此时,你需要检查测试类是否正确引入了Spring Boot相关的依赖,并且是否正确配置了ApplicationContext的加载方式。 如果以上三个方面都没有问题,你可以将测试方法改为使用assertEquals()方法,以便查看测试失败的具体原因: ``` @Test public void contextLoads() { String[] beanNames = applicationContext.getBeanNamesForType(MyCustomAnnotation.class); assertEquals(1, beanNames.length); assertEquals("myCustomAnnotation", beanNames[0]); } ``` 这个测试方法中,我使用了assertEquals()方法,以确保ApplicationContext中只有一个MyCustomAnnotation类型的Bean,并且该Bean的名称为"myCustomAnnotation"。如果测试方法失败,你可以根据输出的错误信息进一步排查问题。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BasicLab基础架构实验室

你的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值