junit

Can a JUnit testmethod have a argument?

旧街凉风 提交于 2019-12-17 18:36:04
问题 import java.util.regex.Pattern; public class TestUI { private static Pattern p = Pattern.compile("^[A-Za-z0-9()+-]+$"); public static void main(String[] args) { // Test case1 String[] str=test(); System.out.println(str[0]+str.length); match("Alphanumeric(Text)"); } private static String[] test() { boolean res; String[] array={"a","b","c","d","e"}; for(int i=0;i<array.length;i++){ System.out.println(match(array[i])); res=match(array[i]); if(res=true) calltomethod(array); } return array; }

how to handle exceptions in junit

十年热恋 提交于 2019-12-17 18:34:37
问题 I wrote some test cases to test some method. But some methods throw an exception. Am I doing it correctly? private void testNumber(String word, int number) { try { assertEquals(word, service.convert(number)); } catch (OutOfRangeNumberException e) { Assert.fail("Test failed : " + e.getMessage()); } } @Test public final void testZero() { testNumber("zero", 0); } If I pass -45 , it will fail with OutOfRangeException but I am not able to test specific exception like @Test(Expected...) 回答1: An

How do I get Spring MVC to invoke validation in a JUnit test?

故事扮演 提交于 2019-12-17 18:21:55
问题 I have a POJO called Browser that I've annotated with Hibernate Validator annotations. import org.hibernate.validator.constraints.NotEmpty; public class Browser { @NotEmpty private String userAgent; @NotEmpty private String browserName; ... } I've written the following unit test that tries to verify my Controller method catches validation errors. @Test public void testInvalidData() throws Exception { Browser browser = new Browser("opera", null); MockHttpServletRequest request = new

Junit Test of Setters and Getters of Instance Variables

一曲冷凌霜 提交于 2019-12-17 18:19:21
问题 When creating test cases for setters and getters of instance variables within the object. What is the best approach? Here I use the get and set methods within my tests. Would this be poor testing strategy? /** * Test of setFlightNumber method, of class Flight. */ @Test public void testSetFlightNumber() { System.out.println("setFlightNumber"); int flightNumber = 1000; Flight instance = new Flight(); instance.setFlightNumber(flightNumber); // TODO review the generated test code and remove the

Java unit testing: how to measure memory footprint for method call

北城以北 提交于 2019-12-17 18:05:51
问题 Assuming I have a class that does some heavy processing, operating with several collections. What I want to do is to make sure that such operation can't lead to out-of-memory or even better I want to set a threshold of how much memory it can use. class MyClass() { public void myMethod() { for(int i=0; i<10000000; i++) { // Allocate some memory, may be several collections } } } class MyClassTest { @Test public void myMethod_makeSureMemoryFootprintIsNotBiggerThanMax() { new MyClass().myMethod()

Why is assertEquals(double,double) deprecated in JUnit?

[亡魂溺海] 提交于 2019-12-17 17:53:15
问题 I was wondering why assertEquals(double, double) is deprecated. I used import static org.junit.Assert.assertEquals; and I used JUnit 4.11. Below is my code: import org.junit.Test; import static org.junit.Assert.assertEquals; public class AccountTest { @Test public void test() { Account checking = new Account(Account.CHECKING); checking.deposit(1000.0); checking.withdraw(100.0); assertEquals(900.0, checking.getBalance()); } } checking.getBalance() returns a double value. What could be wrong?

How to use JUnit and Hamcrest together?

泪湿孤枕 提交于 2019-12-17 17:26:31
问题 I can't understand how JUnit 4.8 should work with Hamcrest matchers. There are some matchers defined inside junit-4.8.jar in org.hamcrest.CoreMatchers . At the same time there are some other matchers in hamcrest-all-1.1.jar in org.hamcrest.Matchers . So, where to go? Shall I explicitly include hamcrest JAR into the project and ignore matchers provided by JUnit? In particular, I'm interested in empty() matcher and can't find it in any of these jars. I need something else? :) And a

Python unittests in Jenkins?

纵饮孤独 提交于 2019-12-17 17:19:49
问题 How do you get Jenkins to execute python unittest cases? Is it possible to JUnit style XML output from the builtin unittest package? 回答1: sample tests: tests.py: # tests.py import random try: import unittest2 as unittest except ImportError: import unittest class SimpleTest(unittest.TestCase): @unittest.skip("demonstrating skipping") def test_skipped(self): self.fail("shouldn't happen") def test_pass(self): self.assertEqual(10, 7 + 3) def test_fail(self): self.assertEqual(11, 7 + 3) JUnit with

HttpMediaTypeNotAcceptableException: Could not find acceptable representation in exceptionhandler

只愿长相守 提交于 2019-12-17 16:30:27
问题 I have the following image download method in my controller (Spring 4.1): @RequestMapping(value = "/get/image/{id}/{fileName}", method=RequestMethod.GET) public @ResponseBody byte[] showImageOnId(@PathVariable("id") String id, @PathVariable("fileName") String fileName) { setContentType(fileName); //sets contenttype based on extention of file return getImage(id, fileName); } The following ControllerAdvice method should handle a non-existing file and return a json error response:

How to write a matcher that is not equal to something

冷暖自知 提交于 2019-12-17 16:26:41
问题 I am trying to create a mock for a call. Say I have this method I am trying to stub out: class ClassA { public String getString(String a) { return a + "hey"; } } What I am trying to mock out is: 1st instance is when(classA.getString(eq("a")).thenReturn(...);` in the same test case when(classA.getString([anything that is not a])).thenReturn(somethingelse); The 2nd case is my question: How do I match anyString() other than "a"? 回答1: With Mockito framework, you can use AdditionalMatchers ClassA