为什么org.junit.Assert.assertTrue要静态导入

转自:http://stackoverflow.com/questions/16213910/asserttrue-statement-requires-static-import-in-intellij-idea


提问:

I just shifted my project form Netbeans to intelliJ IDEA, its a junit based test project. In netbeans I was using statments

assertTrue("Message", conditionCustom());

and it was working without any extra import. Now when using the same above command in intelliJ I have to import file

import static org.junit.Assert.assertTrue;

is there any way so I dont need to write the above line in my code file? otherwise I have to edit all my files to get working assertTrue statement.


回答:

You either have to add the static import OR make clear what class that static call is associated with:

Assert.assertTrue("Message", false);

I usually use the latter because I think it's clearer.

Java won't compile unless it can figure out which class to associate that static method with.

I'd guess that perhaps you use inheritance to associate that static method with your test.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你按照我的建议修改了代码,使用了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"。如果测试方法失败,你可以根据输出的错误信息进一步排查问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值