junit

Where to put common setUp-code for differen testclasses?

旧城冷巷雨未停 提交于 2019-12-23 17:43:39
问题 I have several different test classes that require that certain objects are created before those tests can be run. Now I'm wondering if I should put the object initialization code into a separate helper class or superclass. Doing so would surely reduce the amount of duplicate code in my test classes but it would also make them less readable. Is there a guideline or pattern how to deal with common setUp-code for unit tests? 回答1: Patterns and practices when dealing with test code is no

Mock a superclass constructor

谁说我不能喝 提交于 2019-12-23 17:40:27
问题 I would like to know if I can mock a super class constructors call and its super() calls. For example, I have the following classes class A { A(..) { super(..) } } class B extends A { B(C c) { super(c) } } So, I am planning to unit test some methods in class B, but when creating an instance it does call the super class constructors making it tough to write unit tests. So, how can I mock all the super class constructor calls. Also I would like to mock few methods in class A so that it does

1.导入junit包和@Test无法使用

谁说胖子不能爱 提交于 2019-12-23 17:33:12
1.先下载 junit-4.12.jar 和 hamcrest-core-1.3.jar 两个包。 http://central.maven.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar http://central.maven.org/maven2/junit/junit/4.12/junit-4.12.jar 2.IDEA 打开 Project Structure(ctrl + alt + shift + s),在dependencies一栏加入这两个包。直接应用即可. 3.@Test会依然显示错误,因为你必须写个完整比如 public void hello(){};错误才会消失。 来源: CSDN 作者: 小白之路 链接: https://blog.csdn.net/s546889/article/details/103664576

Maven Surefire Junit test suite

心已入冬 提交于 2019-12-23 17:27:00
问题 I am using the Maven Surefire plugin in order to run just a specific suite of tests. For instance: package suite; import org.junit.runner.RunWith; import org.junit.runners.Suite; import suite.slow.EvenSlowerClassTest; import suite.slow.SlowClassTest; @RunWith(Suite.class) @Suite.SuiteClasses({ SlowClassTest.class, EvenSlowerClassTest.class }) public class SlowSuite { } By using the maven command test -DrunSuite=**/FastSuite.class -DfailIfNoTests=false I can run the FastSuite or SlowSuite test

JUnit, JMock, JUnitRuleMockery: what am I missing

前提是你 提交于 2019-12-23 16:53:43
问题 This is strange: I am using JUnit with JMock using the Rule JUnitRuleMockery for a while and it always worked perfectly fine: expectations where checked at the end of the test and if one (or more) was missing the test was failing. However, this snippet is not working for me in Eclipse ( DAO is the interface described in org.mongodb.morphia.dao.DAO ): @Rule public JUnitRuleMockery context = new JUnitRuleMockery(); private DAO<Cobrand, ObjectId> dao = context.mock(DAO.class); private final

Concurrent JUnit Tests with Parameters

白昼怎懂夜的黑 提交于 2019-12-23 16:46:10
问题 So I'm attempting to run parallel parameterized tests. I have a solution where the same test can run in parallel with the parameters supplied for example say I have the following: @Test public void someTest1(){ } @Test public void someTest2(){ } I can get someTest1() to run with all the parameters concurrently, but someTest2() will have still have to wait for someTest1() to complete with all parameters before executing. I was wondering if anyone knew of a solution to be able to run someTest1(

NPE in StrutsTestCase after enabling Tiles

[亡魂溺海] 提交于 2019-12-23 15:44:22
问题 I developed some JUnit tests that extend org.apache.struts2.StrutsTestCase. I used the tutorial on struts.apache.org as my starting point. Everything was working fine until I modified my simple web application to use Tiles. I have Tiles working fine in the app but now my Action test cases have stopped working. I'm getting NullPointerException at org.apache.struts2.views.tiles.TilesResult.doExecute when I run the following line of code: ActionProxy proxy = getActionProxy("/displaytag.action");

Why does a call to fail() compile in a Java Class using JUnit

人走茶凉 提交于 2019-12-23 15:28:21
问题 This seems like it shouldn't compile and run as Object does not have a fail() method. At compile time is something funky happening? (I am using NetBeans): import static org.junit.Assert.*; import org.junit.Test; public class Test { @Test public void hello() { fail(); } } Regards, Guido 回答1: Your import static line imports all static members of the Assert class into the static namespace of your compilation unit. The fail() call refers to Assert.fail() . The confusion you are experiencing

Android JUnit testing of Preferences

只谈情不闲聊 提交于 2019-12-23 15:14:51
问题 A fairly normal scenario: an Android application has a preferences activity, and selecting an option from a ListPreference triggers code to change that ListPreference's summary text. ie: Selecting "Green" from a color ListPreference would change the ListPreference's summary text to "Green" through a onPreferenceChange callback. I'd like to be able to use the Android JUnit testing to confirm that these summary changes are all being performed correctly. However, there seems to be very little

TAP::Harness timing issue with TAP::Formatter::JUnit

喜你入骨 提交于 2019-12-23 14:24:59
问题 I have a set of scripts that produces a JUnit output for Jenkins. The code I execute looks like this (this is just a snippet so you get the idea) : #!/usr/bin/env perl use strict; use warnings; use Test::More; use TAP::Harness; use Test::Builder; my $runner = sub { my ($harness,$test) = @_; sleep(2); my $builder = Test::Builder->new; $builder->reset; $builder->output( \my ($out) ); $builder->failure_output( \$out ); $builder->todo_output( \$out ); $builder->is_eq($test,'test', 'Test is test')