how to handle exceptions in junit

An unexpected exception is a test failure, so you neither need nor want to catch one. @Test public void canConvertStringsToDecimals() { String str = “1.234”; Assert.assertEquals(1.234, service.convert(str), 1.0e-4); } Until service does not throw an IllegalArgumentException because str has a decimal point in it, that will be a simple test failure. An expected exception should … Read more

How to tell spring to only load the needed beans for the JUnit test?

Consider adding default-lazy-init=”true” to your spring context xml beans tag (or add lazy-init=”true” to those specific beans that take a long time starting up). This will ensure that only those beans are created that called with applicationContext.getBean(class-or-bean-name) or injected via @Autowired / @Inject into your tests. (Some other types of beans like @Scheduled beans will … Read more

How to make JUnit test cases execute in parallel? [duplicate]

Junit4 provides parallel feature using ParallelComputer: public class ParallelComputerTest { @Test public void test() { Class[] cls={ParallelTest1.class,ParallelTest2.class }; //Parallel among classes JUnitCore.runClasses(ParallelComputer.classes(), cls); //Parallel among methods in a class JUnitCore.runClasses(ParallelComputer.methods(), cls); //Parallel all methods in all classes JUnitCore.runClasses(new ParallelComputer(true, true), cls); } public static class ParallelTest1 { @Test public void a(){} @Test public void b(){} … Read more

Unit testing jersey Restful Services

For Jersey web services testing there are several testing frameworks, namely: Jersey Test Framework (already mentioned in other answer – see here documentation for version 1.17 here: https://jersey.java.net/documentation/1.17/test-framework.html) and REST-Assured (https://code.google.com/p/rest-assured) – see here a comparison/setup of both (http://www.hascode.com/2011/09/rest-assured-vs-jersey-test-framework-testing-your-restful-web-services/). I find the REST-Assured more interesting and powerful, but Jersey Test Framework is very easy to … Read more

Force IOException during file reading

Disclaimer: I have not tested this on a non-Windows platform, so it may have different results on a platform with different file locking characteristics. If you lock the file beforehand, you can trigger an IOException when something attempts to read from it: java.io.IOException: The process cannot access the file because another process has locked a … Read more

Logging level under maven surefire

I tried setting java.util.logging.config.file in the MAVEN_OPTS environment variable, which does not work but finally got it working by putting that system property in the pom.xml (and of course creating an appropriate logging.properties in src/test/resources): <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <systemProperties> <property> <name>java.util.logging.config.file</name> <value>src/test/resources/logging.properties</value> </property> </systemProperties> </configuration> </plugin> </plugins>

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)