Tag: junit

如何JUnit测试从文件解析的对象

我正在编写一个解析一些文件而不是处理这些文件的程序。 解析器经过了很好的测试,但我也必须测试处理部分。 问题是unit testing函数我需要来自解析器的大对象。 我不想手动创建对象(这是太多的时间)。 我的目标是重构一些过程函数,因为某些函数做了很多事情并且使用了大量的对象。 语言是java,我使用JUnit。 我试图在unit testing中保存对象并加载,但这是唯一的方法吗? 有什么建议么? 彼得,先谢谢你

为什么JUnit 5测试不从抽象类inheritance@Test注释?

我刚刚意识到(在将遗留代码从JUnit 4迁移到JUnit 5时)我们的一些测试方法没有执行,因为它们没有@Test注释。 他们没有它,因为它们覆盖了抽象超类(注释存在的地方)的方法。 我可以通过在每个方法中添加@Test来轻松解决这个问题。 但我想知道这是否是预期的行为。 它从JUnit 4更改为5但我在官方JUnit5用户指南或其他任何地方都找不到任何相关内容。 根据这个问题 ,注释通常不会被inheritance。 但似乎在新的JUnit版本中故意改变了这一点。 (或者我错过了什么?) 抽象测试类 import org.junit.jupiter.api.Test; abstract class AbstractJUnit5Test { @Test void generalTest() { System.out.println(“This is a test in the abstract class”); } @Test abstract void concreteTest(); } 具体的测试课 import org.junit.jupiter.api.Test; class ConcreteJUnt5Test extends AbstractJUnit5Test { // only gets executed with an additional @Test annotation: @Override […]

使用PowerMockito模拟java.lang.Runtime

想为像这样的方法编写unit testing public static void startProgram() { process = Runtime.getRuntime().exec(command, null, file); } 我不想因为某些原因注入运行时对象,所以我想将getRuntime方法存根,它返回一个Runtime mock …我试过这样: @RunWith(PowerMockRunner.class) @PrepareForTest(Runtime.class) public class ProgramTest { @Test public void testStartProgram() { Runtime mockedRuntime = PowerMockito.mock(Runtime.class); PowerMockito.mockStatic(Runtime.class); Mockito.when(Runtime.getRuntime()).thenReturn(mockedRuntime); … //test } } 但这不起作用。 实际上似乎没有任何东西被嘲笑。 在测试中,使用正常的Runtime对象。 任何人都知道为什么这不起作用和/或它是如何工作的? 由于这个小例子似乎没有重现问题,这里是完整的测试代码:测试方法(缩短) public static synchronized long startProgram(String workspace) { // Here happens someting with Settings […]

如何添加注释以从jacoco代码覆盖率报告中排除方法?

我有一些Java代码,我想从代码覆盖中排除。 我该怎么办? 我希望能够添加注释。 有没有办法配置或扩展jacoco(在gradle中使用)来使用它? 例: public class Something { @ExcludeFromCodeCoverage public void someMethod() {} }

检查JUnit中的exception的最佳做法是什么?

我正在努力编写测试用例。 根据我的阅读,我的测试应该从一开始就失败,我应该努力让测试通过。 但是,我发现自己编写测试检查边界以及它们应该导致的exception: @Test(expected=NegativeArraySizeException.class) public void testWorldMapIntInt() { WorldMap w = new WorldMap(-1, -1); } @Test(expected=IndexOutOfBoundsException.class) public void testGetnIntnInt() { WorldMap w = new WorldMap(10,10); Object o = w.get(-1, -1); } 但是,此测试默认情况下会通过,因为Java无论如何都会抛出exception。 有没有更好的方法来处理这些预期的exception,可能是默认情况下失败的方式 – 迫使我努力处理这些情况?

JUnit assertEquals更改字符串

我有一个JUnit测试,如下所示: @Test public void testToDatabaseString() { DateConvertor convertor = new DateConvertor(); Date date = convertor.convert(“20/07/1984:00:00:00:00”); String convertedDate = convertor.toDatabaseString(date); assertEquals(“to_date(’20/07/1984:00:00:00:00′, ‘DD/MM/YYYY HH24:MI:SS’)”,convertedDate); } 测试失败说明: org.junit.ComparisonFailure: expected: but was: 特别感兴趣的是为什么预期值是: to_date(’20/07/1984[00:]00:00:00′,等… 当我的测试中的字符串文字显然是: “to_date(’20/07/1984:00:00:00:00′,等等…… 有谁能解释一下? 为什么要添加”[00:]” ? 感谢帮助。

无法检索PlatformTransactionManager以进行测试上下文的@Transactional测试

当尝试在事务之间测试Hibernate(版本4)EHCache的缓存function时 – 它失败: Failed to retrieve PlatformTransactionManager for @Transactional test for test context 。 测试 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { ApplicationConfig.class, CachingConfig.class }, loader = AnnotationConfigContextLoader.class) @PersistenceContext @Transactional public class EHCacheTest extends AbstractTransactionalJUnit4SpringContextTests { @Autowired private SessionFactory sessionFactory; @Test public void testTransactionCaching(){ Session session = sessionFactory.getCurrentSession(); System.out.println(session.get(CustomerEntity.class, 1)); Query query = session.createQuery(“from CustomerEntity where CustomerEntity.customerId<10").setCacheable(true).setCacheRegion("customer"); @SuppressWarnings("unchecked") […]

我如何对servlet进行unit testing?

我有一个名为Calculator的servlet。 它通过在响应中设置属性result来读取left , right和op的参数并返回。 unit testing的最简单方法是什么:基本上我想创建一个HttpServletRequest,设置参数,然后检查响应 – 但我该怎么做? 这是servlet代码(它的目的很小而且很愚蠢): public class Calculator extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { public Calculator() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Integer left = Integer.valueOf(request.getParameter(“left”)); Integer right = Integer.valueOf(request.getParameter(“right”)); Integer result = 0; […]

如何在JUnit 5中使用String数组进行参数化

我想编写JUnit 5参数化测试,它将字符串数组( String[] )作为参数: @ParameterizedTest @MethodSource(“stringArrayProvider”) void parseFirstAndSecondInt(String[] args) { Arguments arguments = new Arguments(args); assertEquals(1, arguments.getFirst()); assertEquals(2, arguments.getSecond()); } 我不确定,如何提供字符串数组的集合/流/迭代器。 我没有成功尝试使用@MethodSource注释的方法 static Stream stringArrayProvider() { return Stream.of( new String[]{“1”, “2”}, new String[]{“1”, “2”, “3”}); } 但是我收到了这个例外: org.junit.jupiter.params.converter.ArgumentConversionException: No implicit conversion to convert object of type java.lang.String to type [Ljava.lang.String; 有这种参数化测试的好设计/解决方案是什么?

如何在Junit控制台中接受来自用户的输入

我正在尝试为下面给出的函数编写Junit测试用例: class A{ int i; void set() { Scanner in=new Scanner(System.in); i=in.nextInt(); } } 现在我的问题是当我为它创建一个Junit测试用例时,除了用户的输入之外它没有: public void testSet() throws FileNotFoundException { System.out.println(“set”); A instance = new A(); int i=1; instance.set(i); // TODO review the generated test code and remove the default call to fail. //fail(“The test case is a prototype.”); } 请建议我该怎么做才能接受用户的输入。