JUnit框架包含2个Assert类(很明显,在不同的包中),每个方法看起来都非常相似。 有人可以解释为什么这是? 我所指的类是: junit.framework.Assert和org.junit.Assert 。
我们有一堆JUnittesting用例(集成testing),它们在逻辑上被分组到不同的testing类中。 我们能够为每个testing类加载一次Spring应用程序上下文,并将其重用于JUnittesting类中的所有testing用例,如http://static.springsource.org/spring/docs/current/spring-framework-reference /html/testing.html 然而,我们只是想知道是否有一种方法只为一堆JUnittesting类加载Spring应用程序上下文。 FWIW,我们使用Spring 3.0.5,JUnit 4.5并使用Maven来构build项目。
我是Java新手,正在尝试在我正在编写的类上运行unit testing。 Eclipse(3.5)为我创build了unit testing类,并将Junit4添加到我的类path中。 我的课: public class DistanceUtil { public static double metersToMiles( double meters ) { return 0; } public static double metersToKilometers( double meters ) { return 0; } } 我的unit testing: public class DistanceUtilTest { @Test public final void testMetersToMiles() { fail("Not yet implemented"); // TODO } @Test public final void testMetersToKilometers() […]
我正在使用JUnit-dep 4.10和Hamcrest 1.3.RC2。 我创build了一个如下所示的自定义匹配器: public static class MyMatcher extends TypeSafeMatcher<String> { @Override protected boolean matchesSafely(String s) { /* implementation */ } @Override public void describeTo(Description description) { /* implementation */ } @Override protected void describeMismatchSafely(String item, Description mismatchDescription) { /* implementation */ } } 从命令行使用Ant运行时,它工作得很好。 但是从IntelliJ运行时,它会失败: java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18) at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8) at com.netflix.build.MyTest.testmyStuff(MyTest.java:40) 我的猜测是它使用了错误的hamcrest.MatcherAssert。 […]
我想创build一个使用JUnit 4的junittesting套件,在testing套件运行之前,要包含的testing类的名字是不知道的。 在JUnit 3中,我可以这样做: public final class MasterTester extends TestCase { /** * Used by junit to specify what TestCases to run. * * @return a suite containing what TestCases to run */ public static TestSuite suite() { TestSuite suite = new TestSuite(); for(Class<?> klass : gatherTestClasses()) { suite.addTestSuite(klass); } return suite; } } 并让gatherTestClasses()方法处理确定要运行的testing类。 […]
我知道有一种方法可以做到: @Test public void foo(){ try{ //execute code that you expect not to throw Exceptions. } catch(Exception e){ fail("Should not have thrown any exception"); } } 有没有更干净的方式做到这一点。 (可能使用Junit的@Rule ?)
主要区别是什么? @Before和@BeforeClass 并在JUnit 5 @BeforeEach和@BeforeAll @After和@AfterClass 根据JUnit Api @Before在以下情况下使用: 编写testing时,通常会发现多个testing需要在运行之前创build类似的对象。 而@BeforeClass可以用来build立数据库连接。 但不能@Before做同样的?
我的Javatesting在Eclipse中运行良好。 但现在,当我从运行菜单中重新启动testing时,我收到以下消息: No tests found with test runner 'JUnit 4' 在.classpath文件中,我有所有的jar文件,并在最后有: <classpathentry exported="true" kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> <classpathentry kind="output" path="bin"/> </classpath> 我怎样才能解决这个错误,并让testing再次运行?
当写一个新的jUnit4testing,我想知道是否使用@RunWith(MockitoJUnitRunner.class)或MockitoAnnotations.initMocks(this) 。 我创build了一个新的testing&向导自动生成与亚军testing。 针对MockitoJUnitRunner的Javadoc声明如下: 与JUnit 4.4及更高版本兼容,该跑步者增加了以下行为: 初始化用Mock注释的mock,以便显式使用MockitoAnnotations.initMocks(Object)不是必需的。 在每个testing方法之前,Mock都被初始化。 validation每种testing方法之后的框架使用情况。 我不清楚使用Runner是否比我以前使用的initMocks()方法有优势。 任何想法或链接将不胜感激!
在JUnit4中使用参数化testing时,有没有办法设置我自己的自定义testing用例名称? 我想改变默认 – [Test class].runTest[n] – 有意义的。