Tag: junit

Eclipse在同一个项目中的junittesting

这是一个相对开放的问题。 如果我在Eclipse的一个项目中构build了一个应用程序,然后我想testing这个项目,我应该在同一个项目中创buildJUnit代码还是创build一个单独的项目。 例如… ShopSystem也许是我的主要项目的名称 – 我应该创build一个叫做ShopSystemTest的项目吗? 一般来说 – testing代码应该从主项目文件夹存储多远? 如果我将testing代码存储在主项目中,然后将主项目导出为可运行的jar,那么它将使用testing代码,这不是理想的… build议?

JUnit:使用构造函数而不是@Before

我正在使用JUnit 4.我看不到在构造函数中初始化或使用由@Before注释的专用init函数之间的@Before 。 这是否意味着我不必担心呢? 有没有什么情况下@Before不仅仅是在构造函数中进行初始化?

JUnittesting抛出exception的不良forms?

对于JUnit来说,我相当新,而且我不知道什么是最佳实践,例外和exception处理。 例如,假设我正在编写一个IPAddress类的testing。 它有一个构造函数IPAddress(String addr),如果addr为null,将会抛出一个InvalidIPAddressExceptionexception。 据我可以从谷歌search来看,空参数的testing看起来像这样。 @Test public void testNullParameter() { try { IPAddress addr = new IPAddress(null); assertTrue(addr.getOctets() == null); } catch(InvalidIPAddressException e) { return; } fail("InvalidIPAddressException not thrown."); } 在这种情况下,try / catch是有道理的,因为我知道exception即将到来。 但是现在如果我想写testValidIPAddress(),有几种方法可以做到这一点: 方法1: @Test public void testValidIPAddress() throws InvalidIPAddressException { IPAddress addr = new IPAddress("127.0.0.1"); byte[] octets = addr.getOctets(); assertTrue(octets[0] == 127); assertTrue(octets[1] […]

setUp / tearDown(@ Before / @ After)为什么我们在JUnit中需要它们?

我相信大家都知道setUp(@Before)会在任何testing方法执行之前执行,而tearDown(@After)会在执行testing方法之后执行。 我们也知道Junit会创build一个Test 每个testing方法的实例。 我的问题是,我们可以将setUp方法内容移动到类Constructor并删除setUp方法吗? 有没有什么特定的原因保持setUp方法?

JUnit消息应该说明成功还是失败的条件?

我可以用两种方式之一写一个断言信息。 成功说明: assertEquals( "objects should be identical", expected, actual ); 或者说明被打破的状况: assertEquals( "objects aren't identical", expected, actual ); 在JUnit中是否有这样的标准? 如果不是,各方有什么争议? PS我已经看到网上的文章展示这两个这些没有解释,所以只是说“search谷歌”不是一个答案! [UPDATE] 每个人都挂上了我使用assertEquals的事实,因此这个消息可能是无用的。 当然,这只是因为我想简单地说明这个问题。 所以想象一下吧: assertTrue( … big long multi-line expression … ); 消息是有用的。

使用Springconfiguration文件设置系统属性

configuration : Spring 2.5,Junit 4,Log4j log4j文件位置是从系统属性指定的 ${log.location} 在运行时,使用-D java选项设置系统属性。 一切都很好。 问题/我需要什么: 在unit testing时间,系统属性未设置,文件位置未解决。 应用程序使用Spring,想简单地configurationSpring来设置系统属性。 更多信息: 要求仅用于configuration。 无法引入新的Java代码或条目到IDE中。 理想情况下,Spring的属性configuration实现可以处理这一点 – 我只是无法find正确的组合。 这个想法很接近,但需要添加Java代码: Spring的SystemPropertyInitializingBean 有什么帮助吗? 任何想法都表示赞赏。

在Maven的某些模块中跳过testing

我希望我的Maven能够运行大部分的unit testing。 但是在一个项目中有unit testing比较慢,我想排除它们; 偶尔打开它们。 问 :我该怎么做? 我知道-Dmaven.test.skip=true ,但是关掉了所有的unit testing。 我也知道跳过集成testing, 这里描述。 但是我没有集成testing,只是unit testing,我没有任何明确的调用maven-surefire插件。 (我正在使用Maven 2和Eclipse-Maven插件)。

Maven 3和JUnit 4编译问题:org.junit包不存在

我想用Maven构build一个简单的Java项目。 在我的pom文件中,我声明JUnit 4.8.2是唯一的依赖项。 仍然Maven坚持使用JUnit版本3.8.1。 我如何解决它? 这个问题performance在编译失败:“org.junit包不存在”。 这是因为我的源代码中的import语句。 JUnit 4. *中的正确包名是org.junit。*,而在版本3中。*是junit.framework。* 我想我已经在http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html上find了关于问题根源的文档,但是这里的build议似乎是针对Maven专家的。 我不明白该怎么做。

如何编写接口的junittesting?

为接口编写junittesting的最好方法是什么,以便它们可以用于具体的实现类? 你有这个接口和实现类: public interface MyInterface { /** Return the given value. */ public boolean myMethod(boolean retVal); } public class MyClass1 implements MyInterface { public boolean myMethod(boolean retVal) { return retVal; } } public class MyClass2 implements MyInterface { public boolean myMethod(boolean retVal) { return retVal; } } 你将如何写一个testing对接口,所以你可以使用它的类? 可能性1: public abstract class MyInterfaceTest { public […]

如何在Maven中按类别运行JUnittesting?

使用JUnit 4.8和新的@Category注解,有没有办法selectMaven的Surefire插件运行的类别的子集? 例如,我有: @Test public void a() { } @Category(SlowTests.class) @Test public void b() { } 我想运行所有非慢速testing,如下所示(注意-Dtest.categories由我组成)。 mvn test -Dtest.categories=!SlowTests // run non-slow tests mvn test -Dtest.categories=SlowTests // run only slow tests mvn test -Dtest.categories=SlowTests,FastTests // run only slow tests and fast tests mvn test // run all tests, including non-categorized 所以问题是我不想创buildtesting套件(Maven只是在项目中拿起所有的unit testing非常方便),我希望Maven能够按类别selecttesting。 我想我只是编写了-Dtest.categories,所以我想知道是否有类似的设施可以使用?