首页 文章 精选 留言 我的

精选列表

搜索[Java框架],共10018篇文章
优秀的个人博客,低调大师

Java JUnit框架里@Category注解的工作原理

Suppose you have a large number of unit test cases and you don’t want them to be executed all at the same time during Maven build. You can simply achieve it via annotation @Category. (1) Create empty class FastTests and SlowTests.(2) In your test case class, categorize your test method using @Category annotation: (3) Append the following code to your pom.xml: <profiles> <profile> <id>SlowTests</id> <properties> <testcase.groups>com.sap.SlowTests</testcase.groups> </properties> </profile> <profile> <id>FastTests</id> <properties> <testcase.groups>com.sap.FastTests</testcase.groups> </properties> </profile> </profiles> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.13</version> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit47</artifactId> <version>2.13</version> </dependency> </dependencies> <configuration> <groups>${testcase.groups}</groups> </configuration> </plugin> </plugins> </build> (4) In my project, by default all 7 test methods will be executed during Maven build: Suppose you only want to execute unit test belonging to category “SlowTests”, use the following command line: Since now I only marked one method with annotation SlowTests, only one test method is executed: If you would like to execute all unit tests EXCEPT @SlowTests, simply add another profile in pom.xml: <profile> <id>NonSlowTests</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <excludedGroups>com.sap.SlowTests</excludedGroups> </configuration> </plugin> </plugins> </build> </profile> Before test, in order to prove that Slow method is NOT really executed, I add a system.out.println in each method: Use command line: mvn test -P NonSlowTestsFrom console output, I can ensure that the method with @Category(SlowTests.class) is NOT executed at all. 本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

优秀的个人博客,低调大师

SOFARPC 发布 v5.6.3 版本,Java RPC 框架

本期发布内容 虽然是个小版本升级,但是干货不少,详情请见https://github.com/sofastack/sofa-rpc-boot-projects/releases 鼓励目前正在使用 5.6.x 版本的用户升级 New Features (新功能) PR 788- support msgpack serialization 支持msgpack序列化 PR 770- H2 TLS security support H2 TLS安全协议支持 PR 716- Support GRPC 支持GRPC Enhancement (新特性) PR 812- feat:usually we do not open the switch of sec or blacklist PR 811- chore:print extension loader debug info instead of error PR 807- support non-registered center ip for direct connection PR 800- feat:add custom parameter to values PR 799- feat:add some headers to sofa request PR 792- chore:remove fetch group name PR 789- update netty 4.1.42 PR 712- Enable baggage-items for rpc reseteasy mode Bug Fix PR 808- fix:fix simple map serializer (805) PR 795- bugfix:fix npe of AbstractCluster PR 794- chore:fix alert for jackson-databind PR 783- Fix data util test when run in a Daylight Saving Time

优秀的个人博客,低调大师

使用Java JUnit框架里的@Rule注解的用法举例

Suppose you need to repeatedly execute some test method in your unit test case, for example, you would like to test getPrice based on the first set of test data 5 times in test method test1() while for the second set of test data, only one time should be executed. The below class RepeatDemoOne is a bad example, where this special LOOP operation is mixed with test method implementation. Ideally the test method should only contain the pure logic to operate on the method being tested. So we have a better solution RepeatDemoTwo:It could easily be observed that now the test method test1 and test2 are rather clean: no more for LOOP and System.out.println exist any more. Instead, I put the LOOP logic and print out operation into class RepeatableRule which implements interface MethodRule. The concrete rule implementation is done by overriding method apply as below: class RepeatableRule implements MethodRule{ int times = 1; String[] testMethods = null; RepeatableRule(int times, String[] testMethods){ this.times = times; this.testMethods = testMethods; } @Override public Statement apply(final Statement base, final FrameworkMethod method, Object target) { return new Statement() { @Override public void evaluate() throws Throwable { int loopTime = 1; if(Arrays.asList(testMethods).contains(method.getName())) { loopTime = times; } for(int i = 0; i < loopTime; i++ ) { base.evaluate(); System.out.println(method.getName() + " executed."); } } }; } } When I execute this test case, I can get exactly the same result as RepeatDemoOne: With the help of @Rule, we can achieve the same as @Test(expected=). For example, we can use an instance of class ExpectedException to manually declare within a test method itself that a test method expects a given type of exception class. Besides exception, we can also manually specify a sub string which is expected to appear in an error message, and add our custom error message in Junit report if a test method fails. See following code for example: public class RuleWithException { @Rule public ExpectedException exp = ExpectedException.none(); @Test public void expectMessage() { exp.expectMessage("Hello World"); throw new RuntimeException("Hello World will throw exception."); } @Test public void expectCourse() { exp.expectCause(new BaseMatcher<IllegalArgumentException>() { public boolean matches(Object item) { return item instanceof IllegalArgumentException; } @Override public void describeTo(org.hamcrest.Description description) { description.appendText("Expected exception with type IllegalArgumentException " + "raised in test method! "); } }); Throwable cause = new IllegalArgumentException("Cause Test."); throw new RuntimeException(cause); } } In this example, if we comment out line 46, the customed message defined in method describeTo will be printed out in JUnit console: 本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册