junit

Automated way to find JUnit tests that leak memory

拈花ヽ惹草 提交于 2020-01-13 06:58:10
问题 The root of our problem is Singletons. But Singletons are hard to break and in the meantime we have a lot of unit tests that use Singletons without being careful to completely clear them in the tearDown() method. I figure that a good way to detect tests like these is to look for memory leaks. If the memory used after tearDown() and System.gc() is more than when the test started, either the test leaked or more classes were loaded by the classloader. Is there any way to automatically detect

Maven小结

人走茶凉 提交于 2020-01-13 03:58:22
一、简单的小问题?     解释之前,提1个小问题。     1.1、假如你正在IDE下开发两个Java项目,姑且把它们称为A、B,其中A项目中的一些功能依赖于B项目中的某些类,那么如何维系这种依赖关系的呢?     很简单,这不就是跟我们之前写程序时一样吗,需要用哪个项目中的哪些类,也就是用别人写好了的功能代码,导入jar包即可。所以这里也如此,可以将B项目打成jar包,然后在A项目的Library下导入B的jar文件,这样,A项目就可以调用B项目中的某些类了。     这样做几种缺陷     如果在开发过程中,发现B中的bug,则必须将B项目修改好,并重新将B打包并对A项目进行重编译操作     在完成A项目的开发后,为了保证A的正常运行,就需要依赖B(就像在使用某个jar包时必须依赖另外一个jar一样),两种解决方案,第一种,选择将B打包入A中,第二种,将B也发布出去,等别人需要用A时,告诉开发者,想要用A就必须在导入Bjar包。两个都很麻烦,前者可能造成资源的浪费(比如,开发者可能正在开发依赖B的其它项目,B已经存储到本地了,在导入A的jar包的话,就有了两个B的jar),后者是我们常遇到的,找各种jar包,非常麻烦(有了maven就不一样了) 1.2、我们开发一个项目,或者做一个小demo,比如用SSH框架

JUnit 4 and Suspend-on-Exception

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-12 14:29:14
问题 When an uncaught exception is thrown in my code, I am used to having the debugger stop at the throwing statement so that I can examine the local variables and the members of all objects involved at the moment that the exception was thrown. With IntelliJ Idea this can be accomplished by going to Run , View Breakpoints , selecting the Exception Breakpoints tab, checking Any exception , and making sure that the Caught exception checkbox is unchecked, while the Uncaught exception checkbox is

Service层的单元测试

邮差的信 提交于 2020-01-12 08:42:42
一 代码位置 https://gitee.com/cakin24/code/tree/master/07/UnitTestDemo 二 关键代码 package com.example.demo.service; import com.example.demo.entity.User; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import static org.hamcrest.CoreMatchers.is; //表明要在测试环境运行,底层使用的junit测试工具 @RunWith(SpringRunner.class) // SpringJUnit支持,由此引入Spring-Test框架支持! //启动整个spring的工程 @SpringBootTest public class

Testing code which calls native methods

痴心易碎 提交于 2020-01-12 07:10:11
问题 I have a class like this: public final class Foo { public native int getBar(); public String toString() { return "Bar: " + getBar(); } } Please note that getBar() is implemented with JNI and that the class is final . I want to write a junit test to test the toString() method. For this I need to mock the getBar() method and then run the original toString() method to check the output. My first thought was that this must be impossible but then I found PowerMock which supports testing final

Could not resolve com.android.support:appcompat-v7:28.0.0问题的解决

与世无争的帅哥 提交于 2020-01-12 04:53:55
ERROR: Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve com.android.support:appcompat-v7:28.0.0. 这个问题一般是找不到相关依赖导致的。 问题出在app下的build gradle中指定的android sdk的版本,找不到对应的依赖库导致。 解决方法是:找到一个可以在本地编译通过的build.gradle的配置,将 compileSdkVersion targetSdkVersion 修改为配置中的版本号。 android { compileSdkVersion 27 defaultConfig { applicationId "com.taobao.android.mnndemo" minSdkVersion 14 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID

Reading System.in from the Console using Intellij and JUnit

早过忘川 提交于 2020-01-11 11:27:10
问题 The following code works nicely when running the code through a main in Idea System.in.read() However the same code inside a junit method is not working public void testConsoleRead() { System.in.read(); } Any idea how to make this work, or something similar ? 回答1: You need to start the IDE with -Deditable.java.test.console=true (e.g. via "Help" > "Edit Custom VM Options..."), see this comment. 回答2: You need to use the Scanner class. First import it: import java.util.Scanner Now to use it:

Reading System.in from the Console using Intellij and JUnit

久未见 提交于 2020-01-11 11:27:06
问题 The following code works nicely when running the code through a main in Idea System.in.read() However the same code inside a junit method is not working public void testConsoleRead() { System.in.read(); } Any idea how to make this work, or something similar ? 回答1: You need to start the IDE with -Deditable.java.test.console=true (e.g. via "Help" > "Edit Custom VM Options..."), see this comment. 回答2: You need to use the Scanner class. First import it: import java.util.Scanner Now to use it:

Testing @TransactionalEvents and @Rollback

孤街醉人 提交于 2020-01-11 06:34:08
问题 I've been trying to test out @TransactionalEvents (a feature of Spring 4.2 https://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2) with our existing Spring JUnit Tests (run via either @TransactionalTestExecutionListener or subclassing AbstractTransactionalUnit4SpringContextTests but, it seems like there's a forced choice -- either run the test without a @Rollback annotation, or the events don't fire. Has anyone come across a good way to test @TransactionalEvents

Maven EAR module and EJB dependencies tests

删除回忆录丶 提交于 2020-01-11 02:26:06
问题 We are building our EAR & EJB projects with maven. It will build all the EJB projects and then they are used as the dependency for EAR, so they are packed into the EAR file eventually. The problem is that each EJB project has junit tests that check the EJB. For now these tests are not very useful because they try to connect to application server (jboss) and execute methods from EJB interface. Is there any way I can build the EJBs, build and deploy the EAR and then run all the tests from all