junit

Access a file in package

感情迁移 提交于 2019-12-23 22:24:32
问题 I am using JUnit to do some automated tests on my application, the tests are in a folder called tests in my JUnit classes package. As of now, I access the files using the following: File file = new File(MyClass.class.getResource("../path/to/tests/" + name).toURI()); Is there a cleaner (and nicer) way to do it? Thanks 回答1: If you want to use the classloader to load your test data, then you can't use File . A File instance represents a path in the file system. The class loads files from the

JUnitCore Stopping

大兔子大兔子 提交于 2019-12-23 22:19:28
问题 I want to stop/destroy a running JUnitCore, which is started with JUnitCore.run(Request.aClass(ClassToRun)); Like pleaseStop() on the RunNotifier. Any ideas? http://junit.sourceforge.net/javadoc/org/junit/runner/package-summary.html 回答1: Option 1: the best option is to write your own Runner implementation, inherited from org.junit.runners.BlockJUnit4ClassRunner and declare it in your execution context, for instance as Main Class of a raw Java command line. Get inspired of JUnit source code

Test if an exception is caught with Junit

半腔热情 提交于 2019-12-23 20:22:49
问题 I'll begin with a code example; I have to test a function, which handles data-packets. In this function, the data-packet is opened and when it doesn't contain all expected data, an InvalidParameterExeption is thrown which is logged. public void handleData(dataPacket) { try { analyseData(dataPacket); } catch (InvalidParameterException e) { e.printStackTrace() } } So, if everything goes well, my exception is printed in my terminal. But how can I test this? I can't use this: (because the

Log4j Logger.getLogger(Class) throws NPE when running with jMockit and Cobertura

a 夏天 提交于 2019-12-23 20:04:34
问题 I have found a strange interaction between cobertura-maven-plugin 2.6 and jmockit 1.8. A particular pattern in our production code has a class with a lot of static methods that effectively wraps a different class that acts like a singleton. Writing unit tests for these classes went fine until I tried to run coverage reports with cobertura, when this error cropped up: java.lang.ExceptionInInitializerError at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at sun.reflect

Run junit test certain amount of time

混江龙づ霸主 提交于 2019-12-23 19:38:57
问题 I want to test something for a while, say 5 seconds, and then pass the test if nothing wrong has been asserted. Is this possible with annotations? Can something like @Test(uptime=5000) be used? 回答1: Revised answer after question was edited Fundamentally it feels like you're testing the wrong thing here - it seems very odd for "nothing happening" to be a sign of success. If you want to prove that your algorithm can run for a certain amount of time without failing, I would actually extract out

Java / JUnit - comparing two polynomial objects

六月ゝ 毕业季﹏ 提交于 2019-12-23 19:21:03
问题 I have a Java class called Term holding polynomials like below public Term(int c, int e) throws NegativeExponent { if (e < 0) throw new NegativeExponent(); coef = c; expo = (coef == 0) ? 1 : e; } I also have an equals method in the same class like below @Override public boolean equals(Object obj) { } I am stuck with how to code how to compare these 2 Term objects Within my JUnit test file I am using the test below to try and test the equals method import static org.junit.Assert.*; import org

Junit4 run a test class a fixed number of times and display results (eclipse)

假如想象 提交于 2019-12-23 18:42:13
问题 I want to be able to run a Test class a specified number of times. The class looks like : @RunWith(Parameterized.class) public class TestSmithWaterman { private static String[] args; private static SmithWaterman sw; private Double[][] h; private String seq1aligned; @Parameters public static Collection<Object[]> configs() { // h and seq1aligned values } public TestSmithWaterman(Double[][] h, String seq1aligned) { this.h = h; this.seq1aligned = seq1aligned; } @BeforeClass public static void

org.apache.commons.fileupload.disk.DiskFileItem is not created properly?

无人久伴 提交于 2019-12-23 18:23:05
问题 I am trying to use the code shown in the following example: java.lang.NullPointerException while creating DiskFileItem My Test method contains the following code: final File TEST_FILE = new File("C:/my_text.txt"); final DiskFileItem diskFileItem = new DiskFileItem("fileData", "text/plain", true, TEST_FILE.getName(), 100000000, TEST_FILE.getParentFile()); diskFileItem.getOutputStream(); System.out.println("diskFileItem.getString() = " + diskFileItem.getString()); The text file exists in this

Integration issues with Spring Batch and Spring Integration - “No poller has been defined for endpoint” exception

拟墨画扇 提交于 2019-12-23 18:20:32
问题 I went through the Spring Integration guide and the examples here and got a working sample for Spring Integration SFTP program. I already have a working Spring Batch program that reads a bunch of file and dumps into the Database. I am now trying to integrate both Spring Batch and Spring Integration programs by going through the Spring docs and I created the below configuration. <bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">

Failed to load Spring ApplicationContext

我的梦境 提交于 2019-12-23 18:04:27
问题 I'm writing unit tests for a spring application which is sort of complex. I want to load spring context in order to use defined beans. My context.xml is located at: src/main/resources/context.xml After maven build, the context.xml appears at: target/classes/context.xml In the pom.xml, I have: (As suggested by this post) <resources> <resource> <filtering>true</filtering> <directory>src/test/resources</directory> <includes> <include>**/*.properties</include> </includes> <excludes> <exclude>**/