junit

JUnit @BeforeClass non-static work around for Spring Boot application

一曲冷凌霜 提交于 2020-01-01 04:01:11
问题 JUnit's @BeforeClass annotation must be declared static if you want it to run once before all the @Test methods. However, this cannot be used with dependency injection. I want to clean up a database that I @Autowire with Spring Boot, once before I run my JUnit tests. I cannot @Autowire static fields so I need to think of a work around. Any ideas? 回答1: Just use @Before (instead of @BeforeClass ) (or BeforeTransaction (depending on how you initialize the database)). This annotation must been

一键自动生成 java junit 测试代码神器 gen-test-plugin 入门介绍

∥☆過路亽.° 提交于 2020-01-01 02:54:21
gen-test-plugin 红尘小说网 wap.zuxs.net 我们日常编写代码的过程中,经常需要为代码编写测试案例。 随着对代码质量的要求越来越高,很多公司开始通过代码的测试覆盖率作为 QA 的一个评定指标。 本框架可以一键生成所有代码对应的 junit 测试案例,为你的人生节约宝贵的时间。 特性 支持生成 junit4/junit5 支持 jdk7 支持自定义生成模板 更新记录 更新记录 gen-test 用于生成 Junit4/Junit5 单元测试。 引入 <plugin> <groupId>com.github.houbb</groupId> <artifactId>gen-test-plugin</artifactId> <version>0.0.1</version> </plugin> 属性说明 属性 说明 默认值 类型 备注 isOverwriteWhenExists 如果 test 文件已存在,是否覆盖 false 字符串 默认不进行覆盖 encoding 项目编码 utf-8 字符串 includes 包含文件正则 **\/*.java 字符串 默认为所有 java 文件 excludes 排除文件正则 字符串 默认不进行排除 junitVersion junit 版本 4 字符串 默认为 junit4 运行 命令行直接执行 mvn com.github

Use different Spring test context configuration for different test methods

♀尐吖头ヾ 提交于 2020-01-01 02:34:07
问题 We have a Spring based JUnit test class which is utilizing an inner test context configuration class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = ServiceTest.Config.class) public class ServiceTest { @Test public void someTest() { ... @Configuration @PropertySource(value = { "classpath:application.properties" }) @ComponentScan({ "..." }) public static class Config { ... New functionalities have been recently introduced to the Service class, for which the concerned

JUnit: NoClassDefFoundError: org/junit/runner/manipulation/Filter [duplicate]

馋奶兔 提交于 2020-01-01 01:35:24
问题 This question already has answers here : java.lang.NoClassDefFoundError in junit (15 answers) Closed 4 years ago . When I try to run some unit tests, the following error is raised: java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:190) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadTestLoaderClass(RemoteTestRunner.java:320) at org.eclipse.jdt.internal.junit.runner

Android: Skip Gradle “testClasses” task for a dependency project

 ̄綄美尐妖づ 提交于 2019-12-31 18:57:07
问题 I have followed this guide to create a JUnit test file for my main Android module (let's call it "module-a"), in Android Studio v1.4. My "module-a" has a dependency on an external library that is provided as a .aar file and for which I had to create a dedicated module. This dependency causes an error: When right clicking the test Java file and hitting "Run MyTestName" , it fails with this error Error:Gradle: FAILURE: Build failed with an exception. * What went wrong: Task 'testClasses' not

IntelliJ - How to jump to source instead of compiled classes from failed unit tests in the “Run” view

十年热恋 提交于 2019-12-31 17:54:17
问题 When I run my JUnit tests in IntelliJ and one of them fails, I would like to jump to the source code of the failing test by double-clicking it (or by right-clicking it and selecting "Jump to source" or "Show source") in the results view (in the list of failed tests in the "Run" view). However, this feature doesn't always work correctly. Sometimes the source code is shown and sometimes only the compiled ".class" file of the test is shown to me in the editor window. I would always like to see

Do not run a category of slow JUnit tests by default in Maven and every IDE (Eclipse, IntelliJ, …) without an explicit TestSuite

狂风中的少年 提交于 2019-12-31 10:52:46
问题 I have a set of really slow tests, which take a week to run. (They literally run some code non-stop for about a week). Naturally, no developer (or even the default build job) wants to run these tests. Only a specific, separate build job has the time to run them. So these tests needs to be disabled by default . JUnit's categories seemed perfect for this: I annotated those slow tests with @Category(SlowTests.class) . Problem is that they are still run because: I don't want to maintain TestSuite

Do not run a category of slow JUnit tests by default in Maven and every IDE (Eclipse, IntelliJ, …) without an explicit TestSuite

我与影子孤独终老i 提交于 2019-12-31 10:51:55
问题 I have a set of really slow tests, which take a week to run. (They literally run some code non-stop for about a week). Naturally, no developer (or even the default build job) wants to run these tests. Only a specific, separate build job has the time to run them. So these tests needs to be disabled by default . JUnit's categories seemed perfect for this: I annotated those slow tests with @Category(SlowTests.class) . Problem is that they are still run because: I don't want to maintain TestSuite

Mock private method in the same class that is being tested

半世苍凉 提交于 2019-12-31 10:44:05
问题 I have a Java class named, MyClass , that I want to test with JUnit. The public method, methodA , that I want to test calls a private method, methodB , in the same class to determine which conditional path to follow. My goal is to write JUnit tests for the different paths in methodA . Also, methodB calls a service, so I do not want it to actually be executed when I run the JUnit tests. What is the best way to mock methodB and control its return so that I can test different paths for 'methodA'

Mock private method in the same class that is being tested

余生颓废 提交于 2019-12-31 10:42:02
问题 I have a Java class named, MyClass , that I want to test with JUnit. The public method, methodA , that I want to test calls a private method, methodB , in the same class to determine which conditional path to follow. My goal is to write JUnit tests for the different paths in methodA . Also, methodB calls a service, so I do not want it to actually be executed when I run the JUnit tests. What is the best way to mock methodB and control its return so that I can test different paths for 'methodA'