Skip to main content

java junit 记录

maven test忽略 @Disabled @Ignore

代码里配置了junit的@Disabled @Ignore, 但执行mvn test还是运行了测试.

最后搜索发现网上有一个类似的, 升级surefire plugin插件版本解决.

出问题的版本是2.19.1, 修改为3.0.0-M3成功识别.

https://stackoverflow.com/questions/58197153/junit-5-performing-tests-in-maven-build-even-when-not-annotated

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>

idea 执行报 no tests were found

idea 里写了一个方法用@Test注解, 手动执行后报 no tests were found, 最后发现因为使用@Test包裹的方法定义为public static, 修改为public后成功触发.


import org.apache.commons.collections4.CollectionUtils;
import org.junit.Assert;
import org.junit.jupiter.api.Test;


public class OpenLdapTest {

@Test
public void searchGroupByNameTest() {
final List<String> strings;
try {
strings = searchGroupByName("abc");
} catch (NamingException e) {
throw new RuntimeException(e);
}
Assert.assertTrue(CollectionUtils.isNotEmpty(strings));
System.out.println(strings);
}
}

其他各种可能性可以参考stackoverflow:

No tests were found - Empty test suite when running jUnit 5 testcase on bare-bone Spring Boot Maven project

https://stackoverflow.com/questions/53179735/no-tests-were-found-empty-test-suite-when-running-junit-5-testcase-on-bare-bon

created at Tue Jul 04 2023 08:00:00 GMT+0800 (China Standard Time)