junit4

AndroidJUnit4.class + org.junit.Assume.assumeTrue = AssumptionViolatedException

限于喜欢 提交于 2019-12-07 08:12:17
问题 I've managed to get my Android project transitioned over to JUnit4, and of course the main reason I wanted to do it isn't working. Would love any help if anyone's got ideas here. The problem I'm trying to solve is that I want to automatically skip certain tests if the build is not pointed at the staging server. I've got this set up with a BUILD_TYPE which is using gradle to inject the base URL. I set up an assumeThat clause in my setup which correctly identifies when the build is not staging,

Using PowerMock to mock static class in a rest-assured test

为君一笑 提交于 2019-12-07 06:09:18
问题 Is there any way to use PowerMock with rest-assured, because when I'm trying to test a RESTful API with rest-assured. I want to PowerMock a static call. The code of operation: @POST @Produces("application/json") @Consumes(MediaType.APPLICATION_JSON) public Response createEntity(@Context HttpHeaders hh, String body) { . . . String sec = MDI.check(Some_string, ..., ...); if (sec == null) throw ... return Response.... } And the test: @RunWith(PowerMockRunner.class) @PrepareForTest(MDI.class)

Selenium Scripts on the command line

梦想的初衷 提交于 2019-12-07 06:03:16
Is there any way to run selenium webdriver tests scripts from the command line that have been written using Java and JUnit through Eclipse IDE? I've been trying to use Ant or Maven but I can't get it working. Can someone please offer some advice about where to go with this? The following steps will help you run selenium scripts from the command prompt without the help of eclipse. Set the CLASSPATH for all the supporting files. Right-Click on MyComputer-->Proprties-->Advanced System Settings--> Click on "Advanced" tab in the new window-->Environment Variables--> SystemVariables--> Double click

how to accept input from user in Junit console

南楼画角 提交于 2019-12-07 05:06:19
问题 I am trying to write Junit test case for the function given below: class A{ int i; void set() { Scanner in=new Scanner(System.in); i=in.nextInt(); } } Now my problem is when i create a Junit test case for it, it does not except input from user: 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."); }

Junit4 + Spring 2.5 : Asserts throw “NoClassDefFoundError”

ⅰ亾dé卋堺 提交于 2019-12-07 02:05:59
问题 I've been coding tests in Junit4 with Spring, and I got this funny behavior: If my tests are passing like this, everything is fine: @Test public void truthTest(){ assertTrue(true); //Ok } But, if my test fails: @Test public void truthTest(){ assertTrue(false); //ERROR } Then instead of a test failure I receive an ugly and cryptic stack trace, This is it: http://pastie.org/429912 Sorry for this ugly dump, but its the only data I've got to explain the problem (I "pastied" it for readability) I

Maven, Jenkins - how to build project to different test environments?

心已入冬 提交于 2019-12-07 01:24:40
问题 I have a Java project containing junit tests that needs to be run on different test environments (Dev, Staging, etc.) via Jenkins. How can I setup the building of the project to the different environments and how to pass the url, username and the password to maven? Can I use maven 3 profiles to read the environment url, username and password from a property file? Edit: I've added the profiles to the Project POM: <profiles> <profile> <id>Integration</id> </profile> <profile> <id>Staging</id> <

How to pass command line arguments to tests with gradle test? [duplicate]

ぐ巨炮叔叔 提交于 2019-12-06 23:28:56
问题 This question already has answers here : How can I add a Java system property during JUnit Test Execution (2 answers) Closed 2 years ago . I am using gradle to run JUnit tests. The problem is that I need to pass arguments from the command line to tests. I tries to pass System properties but failed. gradle test -Darg1=something Here is my test: public class MyTest { @Test public void someTest() throws Exception { assertEquals(System.getProperty("arg1"), "something"); } } It fails because there

Test the error code of a custom exception with JUnit 4

非 Y 不嫁゛ 提交于 2019-12-06 23:24:01
问题 I would like to test the return code of an exception. Here is my production code: class A { try { something... } catch (Exception e) { throw new MyExceptionClass(INTERNAL_ERROR_CODE, e); } } And the corresponding exception: class MyExceptionClass extends ... { private errorCode; public MyExceptionClass(int errorCode){ this.errorCode = errorCode; } public getErrorCode(){ return this.errorCode; } } My unit test: public class AUnitTests{ @Rule public ExpectedException thrown= ExpectedException

Cause of an unexpected behaviour using JUnit 4's expected exception mechanism?

坚强是说给别人听的谎言 提交于 2019-12-06 21:59:32
问题 I am trying to test that a particular method throws an expected exception from a method. As per JUnit4 documentation and this answer I wrote the test as: @Test(expected=CannotUndoException.class) public void testUndoThrowsCannotUndoException() { // code to initialise 'command' command.undo(); } However, this code fails the JUnit test, reporting the thrown (and expected) exception as an error. The method I'm testing has only this in the body: public void undo() { throw new CannotUndoException(

Will the 'finally' block fire even after a Junit test throws an Assertion Error from with in 'try' block?

末鹿安然 提交于 2019-12-06 17:49:59
问题 Will the writer.close() method inside the finally { } block run on an Junit Assertion Error? Assume the following code: @Test public void testWriter() { try { writer.open(); final List<MyBean> myBeans = new ArrayList<ProfileBean>(); /** Add 2 beans to the myBeans List here. **/ final int beansWritten = writer.writeBeans(myBeans); // Say this assertion error below is triggered org.junit.Assert.assertEquals("Wrong number of beans written.", -1, profilesWritten); } finally { writer.close(); //