标签: junit4

如何检查Java类是否包含JUnit4测试?

我有一个Java类.如何检查类是否包含JUnit4测试的方法?我是否必须使用反射对所有方法进行迭代,或者JUnit4是否提供此类检查?

编辑:

由于评论不能包含代码,我根据答案放置了我的代码:

private static boolean containsUnitTests(Class<?> clazz) 
{
        List<FrameworkMethod> methods= new TestClass(clazz).getAnnotatedMethods(Test.class);
        for (FrameworkMethod eachTestMethod : methods)
        {
            List<Throwable> errors = new ArrayList<Throwable>();
            eachTestMethod.validatePublicVoidNoArg(false, errors);
            if (errors.isEmpty()) 
            {
                return true;
            }
            else
            {
                throw ExceptionUtils.toUncheked(errors.get(0));
            }
        }
        return false;
}
Run Code Online (Sandbox Code Playgroud)

java reflection junit junit4

1
推荐指数
1
解决办法
1702
查看次数

使用Junit进行Paramterized测试

如何在JUnit中使用参数化测试来测试以下方法

public class Math {
    public static int add(int a, int b) {  
        return a + b;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道如何使用Junit进行参数化测试以测试此方法,当我想用​​10个不同的args进行测试时.

junit4 parameterized-unit-test

1
推荐指数
1
解决办法
252
查看次数

JUnit4中的assumeTrue错误而不是忽略测试

我正在使用JUnit 4(junit-4.8.2.jar)在我的Java项目上运行测试,并在使用assumeTrue时遇到问题.我有这个基本设置:

public class ClientTest extends TestCase {

    @BeforeClass
    public void setUp() {
        assumeTrue(isPaidAccount());
    }

    @Test
    public void testTheThings() {
        // run this test if the user has a paid account
    }
}
Run Code Online (Sandbox Code Playgroud)

方法isPaidAccount()返回false,这是预期的,但是忽略了类中的测试,它们都将作为错误返回.这在Eclipse中与JUnit4 Test Runner以及maven一起发生.我也尝试将assumeTrue移动到@Before方法中,如下所示:

public class ClientTest extends TestCase {

    @BeforeClass
    public void setUp() {
        // set up
    }

    @Before
    public void mustHavePaidAccount() {
        assumeTrue(isPaidAccount());
    }

    @Test
    public void testTheThings() {
        // run this test if the user has a paid account
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我这样做时,它完全忽略了@Before方法,无论如何都只是运行测试.同样,我对Eclipse Test Runner和maven都有相同的行为.我确定我做的事情显然不正确,但我不确定它是什么.

java junit junit4

1
推荐指数
1
解决办法
935
查看次数

JUnit3和JUnit4之间是否存在性能差异?

我想知道运行JUnit3-Test或JUnit4-Test是否有区别.即使总运行时间只有5-10%的改进,在一个带夜间构建的大型项目中,这也很重要.

Subquestins是:

  • TestRunner是否改进了对Junit4的更新?
  • 或者@annotationJUnit4风格需要更长时间才能解决?
  • JUnit3对JUnit4执行的书面测试有什么好处?

如果可能,请回答参考.

java performance junit junit4 junit3

1
推荐指数
1
解决办法
471
查看次数

如何使用新的JUnit 4.11功能,包括更改测试名称和设置执行顺序

我正在尝试使用JUnit 4.11来设置执行顺序.

我尝试在Ecipse IDE中运行此链接上的参数化测试示例(更改参数化测试的名称),我看到Eclipse IDE中显示的测试名称没有变化.我希望看到测试名称显示为test [1:fib(1)= 1]和test [4:fib(4)= 3],但它们显示为test [0]和test [1]

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
Run Code Online (Sandbox Code Playgroud)

在Eclipse IDE中运行的以下示例导致以下执行顺序(b,a,d,c)而不是预期的(a,b,c,d)

package com.org;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ExecutionOrderTestName {

    @Test
    public void bTest() {
        System.out.println("b");
    }

    @Test
    public void aTest() {
        System.out.println("a");
    }

    @Test
    public void dTest() {
        System.out.println("d");
    }

    @Test
    public void cTest() {
        System.out.println("c");
    }
}
Run Code Online (Sandbox Code Playgroud)

测试的顺序没有发生,我做错了什么?

java junit unit-testing junit4

1
推荐指数
1
解决办法
5042
查看次数

创建名为'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0'的bean时出错

使用名称创建bean时出错

'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0' defined in URL [file:/E:/source-files-healthentic/securityadmin/build/test/classes/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in URL [file:/E:/source-files-healthentic/securityadmin/build/test/classes/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0' defined in URL [file:/E:/source-files-healthentic/securityadmin/build/test/classes/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in URL [file:/E:/source-files-healthentic/securityadmin/build/test/classes/applicationContext.xml]: Invocation of init method failed; nested exception …
Run Code Online (Sandbox Code Playgroud)

jpa struts2 junit4 eclipselink spring-3

1
推荐指数
1
解决办法
2万
查看次数

单元测试使用资源包的静态方法

我已经阅读了很多关于使用Powermock和Mockito的文章,并尝试了很多不同的方法,但我仍然无法弄清楚如何对下面的静态方法进行单元测试.

public static Map<String, String> getEntries() {
    Map<String, String> myEntriesMap = new TreeMap<String, String>();
    ResourceBundle myEntries = ResourceBundle.getBundle(ENTRIES_BUNDLE);
    Enumeration<String> enumList = myEntries.getKeys();
    String key = null;
    String value = null;
    while (enumList.hasMoreElements()) {
        key = enumList.nextElement().toString();
        value = myEntries.getString(key);
        myEntriesMap.put(key, value);
    }
    return myEntriesMap;
}
Run Code Online (Sandbox Code Playgroud)

代码是包含大约30个这样的静态方法的(遗留)类的一部分,并且重构实际上不是一个选项.类似地,在一些其他静态方法中,正在检索DB连接.

例如:如何模拟资源包ENTRIES_BUNDLE并对此方法进行单元测试?我正在寻找一种可以普遍适用于所有静态方法的模式.

java unit-testing junit4 mockito powermock

1
推荐指数
2
解决办法
6798
查看次数

JUnit测试类(netbeans)上没有可运行的方法

请帮帮我!

我已经创建了一个EmployeeTest写入Employee类的类,但是在我完成它之前会发生这个错误.我在这个项目之前编写了类似的项目,它运行没有错误.这是一个非常简单的类,如下所示.

这是错误消息:

initialization ERROR : No runnable methods
    -No runnable methods
    -java.lang.Exception
    -at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
Run Code Online (Sandbox Code Playgroud)

这是EmployeeTest班级:

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

public class EmployeeTest {
Employee employee;
public EmployeeTest() {
}

@BeforeClass
public static void setUpClass() {
}

@AfterClass
public static void tearDownClass() {
}

@Before
public void setUp() {
    employee = new Employee("Austin", "Powers", 70000.00); 
}
public void testGetName(){
    String expected = "Austin Powers";
    String actual = …
Run Code Online (Sandbox Code Playgroud)

java unit-testing junit4 runnable netbeans-7

1
推荐指数
1
解决办法
9950
查看次数

如何为所有测试仅一次初始化Spring applicationContext

我有一组基于Spring的测试。

为了快速执行测试,我想确保Spring上下文仅初始化一次,然后所有测试都应针对该上下文运行,然后将其关闭。

我已经尝试了以下方法:

  1. 使用@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(MyAnnotatedConfig.class)初始化spring上下文
  2. @RunWith(SpringJUnit4ClassRunner.class)@TestExecutionListeners({MyTestExecutionListener.class})与手写测试执行侦听器一起使用,该侦听器将初始化spring上下文并将其注入到具体的测试类中
  3. @BeforeClass在基类和静态字段中使用侦听器来存储spring上下文,以及@AfterClass用于关闭的

使用这三种方法,spring上下文似乎不止一次被初始化,这需要很多时间。看起来JUnit在运行测试时会卸载类,因此有时会丢失静态字段的内容。

有没有办法确保spring上下文仅初始化一次?

java spring spring-test junit4

1
推荐指数
1
解决办法
6591
查看次数

从Java循环创建多个单元测试

我正在为我的(小)程序编写单元测试,测试用例在大约30个不同的文件中指定.
为了测试它,我需要的只是一个遍历所有文件的循环,解析它们并执行所需的操作.

问题是,在这种情况下,我的所有测试都将被视为一个,因为它与@Test符号的功能相同.
有可能以某种方式拆分它而不必为每个测试文件分别设置一个函数吗?

作为一个测试用例的所有测试的问题是,我无法看到哪个测试用例失败了该过程; 如果一个失败,它就会失败(我会得到1个测试失败,而不是5/30失败)

我目前正在使用JUnit(4.12),但我没有义务继续使用它,所以如果有更好的解决方案,我可以切换框架.

谢谢!

例:

public class MyTests {
    @Test
    public void testFromFiles {
        // loop through all the files
    }
}

output: 1 test run successfully 
Run Code Online (Sandbox Code Playgroud)

更新:所选答案对我来说很有用,我添加了另一个解决方案JUnit 5(而不是4),以防它可以帮助某人.

java junit unit-testing junit4

1
推荐指数
1
解决办法
1156
查看次数