junit

Mockito gives UnfinishedVerificationException when it seems OK

南楼画角 提交于 2019-12-18 01:25:07
问题 Mockito appears to be throwing an UnfinishedVerificationException when I think I've done everything correctly. Here's my partial test case: HttpServletRequest req = mock(HttpServletRequest.class); when(req.getHeader("Authorization")).thenReturn("foo"); HttpServletResponse res = mock(HttpServletResponse.class); classUnderTest.doMethod(req, res); // Use the mock verify(res, never()); verify(req).setAttribute(anyString(), anyObject()); And here's the partial class and method: class

junit: no tests found

你说的曾经没有我的故事 提交于 2019-12-18 01:23:10
问题 I have inherited a Java project and am new to Java development. I feel a good way for me to get comfortable with the code is to write some tests around it. I'm writing my code using IntelliJ. My existing project has a folder structure like this: /myProject /src /main /java /com.lexcorp /core /email /providers emailProvider.java I created a new project that will hold tests for this project. I would like this project to hold both unit and integration tests. Currently, my new project has a

How to debug tests with Play! 2.0

懵懂的女人 提交于 2019-12-18 01:05:10
问题 I am setting up a project using Play 2 and I am already able to debug the webapp using eclipse remote debugging. Though, I'd also like to use breakpoints along my tests. Does anyone know how setup unit tests' remote debugging? 回答1: This is happening since Play (SBT) forks separate JVM for tests, without options needed for remote debug. You have at least two options: disable fork of new JVM, pass additional options to JVM used for tests. To disable fork, modify Build.scala, add fork in (Test)

JUnit Exception Testing

会有一股神秘感。 提交于 2019-12-17 23:18:43
问题 Edit: Not JUnit 4 available at this time. Hi there, I have a question about "smart" exception testing with JUnit. At this time, I do it like this: public void testGet() { SoundFileManager sfm = new SoundFileManager(); // Test adding a sound file and then getting it by id and name. try { SoundFile addedFile = sfm.addSoundfile("E:\\Eclipse_Prj\\pSound\\data\\Adrenaline01.wav"); SoundFile sf = sfm.getSoundfile(addedFile.getID()); assertTrue(sf!=null); System.out.println(sf.toString()); sf = sfm

How do I configure JUnit Ant task to only produce output on failures?

て烟熏妆下的殇ゞ 提交于 2019-12-17 22:39:45
问题 I am configuring JUnit in Ant so that unit tests will be run on each build. I would like the output of failing tests to be printed in the Ant console output whenever they are run. I don't need to see any output from succeeding tests. Here is the relevant bit of my build.xml file: <junit> <classpath> <pathelement path="${build}"/> </classpath> <formatter type="brief" usefile="false"/> <batchtest> <fileset dir="${src}" includes="my/tree/junit/"/> </batchtest> </junit> This produces almost what

Run Junit Suite using Maven Command

杀马特。学长 韩版系。学妹 提交于 2019-12-17 22:31:38
问题 I have multiple Junit test suites (SlowTestSuite, FastTestSuite etc). I would like to run only specific suite using maven command. e.g. mvn clean install test -Dtest=FastTestSuite -DfailIfNoTests=false but its not working. Just not running any test at all. Any suggestions please. 回答1: I have achieved this by adding property into pom as: <properties> <runSuite>**/FastTestSuite.class</runSuite> </properties> and maven-surefire-plugin should be: <plugin> <groupId>org.apache.maven.plugins<

Comparing text files with Junit

半世苍凉 提交于 2019-12-17 21:55:23
问题 I am comparing text files in junit using: public static void assertReaders(BufferedReader expected, BufferedReader actual) throws IOException { String line; while ((line = expected.readLine()) != null) { assertEquals(line, actual.readLine()); } assertNull("Actual had more lines then the expected.", actual.readLine()); assertNull("Expected had more lines then the actual.", expected.readLine()); } Is this a good way to compare text files? What is preferred? 回答1: junit-addons has nice support

Where should I put my JUnit tests?

痴心易碎 提交于 2019-12-17 21:54:48
问题 I've got 2 questions about organising Unit tests. Do I have to put test to the same package as tested class, or can I organise tests in different packages? For example if I have validity and other tests, is it correct to split them into different packages, even if they are for same class? What about mock and stub classes? Shall I separate them from packages containing only tests, or put them together? 回答1: The way we do our JUnit test cases is to put them in the same package, but in a

testEquals(), testHashCode() and testToString()

笑着哭i 提交于 2019-12-17 21:37:17
问题 I prepared short Java class. Could anyone show me how write voids: testEquals, testHashCode, testToString for this code in junit? I have a little problem with it;) public class JW { private String name; private int quantityVoters; private int voted; public JW( String nam, int quantityV ) { if( nam == null || nam.length() == 0 || quantityV < 10 ) throw new IllegalArgumentException( "JW: Wrong" ); name= nam; quantityVoters= quantityV; voted= 0; } public void voting( int n ) { if( n < 0 || n >

junit test class for the following code

不打扰是莪最后的温柔 提交于 2019-12-17 21:35:11
问题 How to mock the object for the Phone object. code bellow, public class Fortest { UserDao userdao = new UserDao(); Phone name = new Phone(); public String handleUser(User user) { String returncode="failed"; //User usr = new User("bob"); String username=user.getUsername(); String pass=user.getPass(); System.out.println("username and password : "+username+" : "+pass); Phone name = new Phone(); String ph = name.getA(); System.out.println("ph "+ph); if(ph.equalsIgnoreCase("test")){ System.out