Questions tagged [testing]

55657 questions
2.4k

votes
30

answer
691.5k

Views

How do I test a private function or a class that has private methods, fields or inner classes?

How do I unit test (using xUnit) a class that has internal private methods, fields or nested classes? Or a function that is made private by having internal linkage (static in C/C++) or is in a private (anonymous) namespace? It seems bad to change the access modifier for a method or function just to...
758

votes
10

answer
624.8k

Views

How to make mock to void methods with mockito

How to mock methods with void return type? I implemented an Observer pattern but I can't mock it with Mockito because I don't know how. And I tried to find an example on the Internet, but didn't succeed. My class looks like public class World { List listeners; void addListener(Listener item) { list...
ibrahimyilmaz
717

votes
13

answer
524k

Views

JavaScript unit test tools for TDD

I've looked into and considered many JavaScript unit tests and testing tools, but have been unable to find a suitable option to remain fully TDD compliant. So, is there a JavaScript unit test tool that is fully TDD compliant?
248

votes
10

answer
448.7k

Views

Mocking static methods with Mockito

I've written a factory to produce java.sql.Connection objects: public class MySQLDatabaseConnectionFactory implements DatabaseConnectionFactory { @Override public Connection getConnection() { try { return DriverManager.getConnection(...); } catch (SQLException e) { throw new RuntimeException(e); } }...
Naftuli Kay
624

votes
12

answer
440.8k

Views

How do I generate a stream from a string?

I need to write a unit test for a method that takes a stream which comes from a text file. I would like to do do something like this: Stream s = GenerateStreamFromString('a,b \n c,d');
Omu
700

votes
21

answer
394.6k

Views

How do I use Assert to verify that an exception has been thrown?

How do I use Assert (or other Test class?) to verify that an exception has been thrown?
Alex
228

votes
7

answer
368.5k

Views

Mockito : how to verify method was called on an object created within a method?

I am new to Mockito. Given the class below, how can I use Mockito to verify that someMethod was invoked exactly once after foo was invoked? public class Foo { public void foo(){ Bar bar = new Bar(); bar.someMethod(); } } I would like to make the following verification call, verify(bar, times(1)).som...
mre
402

votes
30

answer
350.5k

Views

How to emulate GPS location in the Android Emulator?

I want to get longitude and latitude in Android emulator for testing. Can any one guide me how to achieve this? How do I set the location of the emulator to a test position?
UMAR
178

votes
5

answer
337.6k

Views

How to check if a string array contains one string in JavaScript? [duplicate]

This question already has an answer here: How do I check if an array includes an object in JavaScript? 43 answers I have a string array and one string. I'd like to test this string against the array values and apply a condition the result - if the array contains the string do 'A', else do 'B'. How...
gtludwig
71

votes
10

answer
332.7k

Views

How to solve the error LNK2019: unresolved external symbol - function?

I get this error, but I don't know how to fix it. I'm using Visual Studio 2013. I made the solution name MyProjectTest This is the structure of my test solution: -function.h #ifndef MY_FUNCTION_H #define MY_FUNCTION_H int multiple(int x, int y); #endif -function.cpp #include 'function.h' int multipl...
phibao37
755

votes
30

answer
329.2k

Views

Unit Testing C Code [closed]

I worked on an embedded system this summer written in straight C. It was an existing project that the company I work for had taken over. I have become quite accustomed to writing unit tests in Java using JUnit but was at a loss as to the best way to write unit tests for existing code (which needed...
361

votes
16

answer
318.8k

Views

How to run test methods in specific order in JUnit4?

I want to execute test methods which are annotated by @Test in specific order. For example: public class MyTest { @Test public void test1(){} @Test public void test2(){} } I want to ensure to run test1() before test2() each time I run MyTest, but I couldn't find annotation like @Test(order=xx). I th...
卢声远 Shengyuan Lu
215

votes
9

answer
293.5k

Views

Testing HTML email rendering [closed]

Are there any good tools to easily test how HTML email will look across different email clients? I prefer something with instant feed back rather than a submit and wait service like http://litmusapp.com Or at the very least a way to test the Outlook 2007/MS Word rendering? I found this related quest...
ejunker
277

votes
11

answer
292.1k

Views

How to run JUnit test cases from the command line

I would like to run JUnit test cases from the command line. How can I do this?
KK.
541

votes
11

answer
269.1k

Views

How do you test that a Python function throws an exception?

How does one write a unittest that fails only if a function doesn't throw an expected exception?
Daryl Spitzer
614

votes
20

answer
260.4k

Views

What is Unit test, Integration Test, Smoke test, Regression Test?

What is Unit test, Integration Test, Smoke test, Regression Test and what are the differences between them? And Which tools can I use for each of them? For example I use JUnit and NUnit for Unit testing and Integration Testing. Are there any Smoke Test or Regression Test tools?
mcaaltuntas
102

votes
10

answer
260k

Views

WebDriver: check if an element exists? [duplicate]

This question already has an answer here: Selenium WebDriver - Test if element is present 20 answers How to check if an element exist with web driver? Is using a try catch really the only possible way? boolean present; try { driver.findElement(By.id('logoutLink')); present = true; } catch (NoSuchEl...
Ralph
437

votes
11

answer
247.3k

Views

Run a single test method with maven

I know you can run all the tests in a certain class using: mvn test -Dtest=classname But I want to run an individual method and -Dtest=classname.methodname doesn't seem to work.
BillMan
108

votes
5

answer
244.4k

Views

How to write a Unit Test?

I have a java class that does a binary sum, I am still trying to figure out how to do unit tests but I don't know how to do it. I've googled around and the best explanation I had was the page from Wikipedia: http://en.wikipedia.org/wiki/Unit_testing but I'm still unsure how to do my test for this pr...
Tsundoku
167

votes
4

answer
240.7k

Views

Using Mockito's generic “any()” method

I have an interface with a method that expects an array of Foo: public interface IBar { void doStuff(Foo[] arr); } I am mocking this interface using Mockito, and I'd like to assert that doStuff() is called, but I don't want to validate what argument are passed - 'don't care'. How do I write the foll...
ripper234
720

votes
30

answer
233.1k

Views

What's the difference between a mock & stub?

I've read various articles about mocking vs stubbing in testing, including Martin Fowler's Mocks Aren't Stubs, but still don't understand the difference.
never_had_a_name
490

votes
7

answer
230k

Views

Writing unit tests in Python: How do I start? [closed]

I completed my first proper project in Python and now my task is to write tests for it. Since this is the first time I did a project, this is the first time I would be writing tests for it. The question is, how do I start? I have absolutely no idea. Can anyone point me to some documentation/ tutoria...
user225312
259

votes
10

answer
220.2k

Views

Comparison of c++ unit test frameworks [closed]

I know there are already a few questions regarding recomendations for c++ unit test frameworks, but all the answers did not help as they just recomend one of the frameworks but do not provide any information about a (feature) comparison. I think the most interesting frameworks are CppUnit, Boost and...
housemaister
443

votes
12

answer
219k

Views

Unit test naming best practices [closed]

What are the best practices for naming unit test classes and test methods? This was discussed on SO before, at What are some popular naming conventions for Unit Tests? I don't know if this is a very good approach, but currently in my testing projects, I have one-to-one mappings between each producti...
James Newton-King
212

votes
5

answer
216.7k

Views

What is “X-Content-Type-Options=nosniff”?

I am doing some penetration testing on my localhost with OWASP ZAP, and it keeps reporting this message: The Anti-MIME-Sniffing header X-Content-Type-Options was not set to 'nosniff' This check is specific to Internet Explorer 8 and Google Chrome. Ensure each page sets a Content-Type header and the...
Koffeehaus
744

votes
8

answer
209.3k

Views

What's the difference between unit, functional, acceptance, and integration tests? [closed]

What is the difference between unit, functional, acceptance, and integration testing (and any other types of tests that I failed to mention)?
Andrew
344

votes
5

answer
197.7k

Views

Can Mockito capture arguments of a method called multiple times?

I have a method that gets called twice, and I want to capture the argument of the second method call. Here's what I've tried: ArgumentCaptor firstFooCaptor = ArgumentCaptor.forClass(Foo.class); ArgumentCaptor secondFooCaptor = ArgumentCaptor.forClass(Foo.class); verify(mockBar).doSomething(firstFooC...
Eric Wilson
448

votes
10

answer
194.7k

Views

How do you beta test an iphone app?

How can you beta test an iPhone app? I can get it on my own device, and anyone that gives me a device, I can run it on theirs, but is there a way to do a limited release via the app store for beta testing? Related: Also, see this question on getting your app onto phones without using the App Store.
Michael Pryor
330

votes
5

answer
193.5k

Views

What's the difference between unit tests and integration tests? [duplicate]

This question already has an answer here: What is the difference between integration and unit tests? 20 answers What's the difference between unit tests and integration tests? Are there different names for these tests? Like some people calling unit tests functional tests, etc?
Books
197

votes
10

answer
190k

Views

Best way to stress test a website [duplicate]

This question already has an answer here: ASP.NET Stress Testing 6 answers This may be the wrong question to ask but, what's the best way to replicate a large load on an asp.net web application? Is there an easy way to simulate many requests on particular pages? Or is the best thing to use a profil...
Charlie Bear
160

votes
8

answer
187.1k

Views

How to read a text-file resource into Java unit test? [duplicate]

This question already has an answer here: How do I create a Java string from the contents of a file? 33 answers What is simplest way to read a file into String? [duplicate] 9 answers I have a unit test that needs to work with XML file located in src/test/resources/abc.xml. What is the easiest way...
yegor256
47

votes
19

answer
184.4k

Views

Switch tabs using Selenium WebDriver with Java

Using Selenium WebDriver with JAVA , I am trying to automate a functionality where I have to open a new tab do some operations there and come back to previous tab (Parent). I used switch handle but its not working. And one strange thing the two tabs are having same window handle due to which i am no...
Umesh Kumar
264

votes
8

answer
181.2k

Views

Exact time measurement for performance testing [duplicate]

This question already has an answer here: How to measure code performance in .NET? 18 answers What is the most exact way of seeing how long something, for example a method call, took in code? The easiest and quickest I would guess is this: DateTime start = DateTime.Now; { // Do some work } TimeSpan...
Svish
456

votes
30

answer
178.4k

Views

How do you unit test private methods?

I'm building a class library that will have some public & private methods. I want to be able to unit test the private methods (mostly while developing, but also it could be useful for future refactoring). What is the correct way to do this?
Eric Labashosky
144

votes
8

answer
178.4k

Views

Mockito. Verify method arguments

I've googled about this, but didn't find anything relevant. I've got something like this: Object obj = getObject(); Mockeable mock= Mockito.mock(Mockeable.class); Mockito.when(mock.mymethod(obj )).thenReturn(null); Testeable testableObj = new Testeable(); testableObj.setMockeable(mock); command.runt...
manolowar
251

votes
14

answer
177k

Views

How do you run a single test/spec file in RSpec?

I want to be able to run a single spec file's tests — for the one file I'm editing, for example. rake spec executes all the specs. My project is not a Rails project, so rake spec:doc doesn't work. Don't know if this matters, but here is my directory structure. ./Rakefile ./lib ./lib/cushion.rb ....
Jonathan Tran
163

votes
4

answer
176.5k

Views

Configuring IntelliJ IDEA for unit testing with JUnit

I decided to try out IntelliJ this morning via the trial version and installed the JUnit plugin. I made a new Java project and I want to write a test case for it. How do I add the junit.jar file to my project? (I actually want to add it to every java project, now and forever more - is there a way of...
Bob
234

votes
20

answer
175.3k

Views

Making the Android emulator run faster

The Android emulator is a bit sluggish. For some devices, like the Motorola Droid and the Nexus One, the app runs faster in the actual device than the emulator. This is a problem when testing games and visual effects. How do you make the emulator run as fast as possible? I've been toying with its pa...
hpique
46

votes
9

answer
170.2k

Views

Spring @ContextConfiguration how to put the right location for the xml

In our project we are writting a test to check if the controller returns the right modelview @Test public void controllerReturnsModelToOverzichtpage() { ModelAndView modelView = new ModelAndView(); KlasoverzichtController controller = new KlasoverzichtController(); modelView = controller.showOverzic...
David
510

votes
30

answer
169.2k

Views

What is a reasonable code coverage % for unit tests (and why)? [closed]

If you were to mandate a minimum percentage code-coverage for unit tests, perhaps even as a requirement for committing to a repository, what would it be? Please explain how you arrived at your answer (since if all you did was pick a number, then I could have done that all by myself ;)
sanity