junit4

junit testing - assertEquals for exception

爷,独闯天下 提交于 2020-02-18 13:55:50
问题 How can I use assertEquals to see if the exception message is correct? The test passes but I don't know if it hits the correct error or not. The test I am running. @Test public void testTC3() { try { assertEquals("Legal Values: Package Type must be P or R", Shipping.shippingCost('P', -5)); } catch (Exception e) { } } The method being tested. public static int shippingCost(char packageType, int weight) throws Exception { String e1 = "Legal Values: Package Type must be P or R"; String e2 =

PowerMock throws NoSuchMethodError (setMockName)

不羁岁月 提交于 2020-02-17 10:54:46
问题 I'm trying to mock a constructor using PowerMockito but every time I run the test I get the following error: java.lang.NoSuchMethodError: org.mockito.internal.creation.MockSettingsImpl.setMockName(Lorg/mockito/mock/MockName;)Lorg/mockito/internal/creation/settings/CreationSettings; at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:107) at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60) at org

JUnit Error: “Failed to load ApplicationContext”

喜欢而已 提交于 2020-01-24 20:14:22
问题 Nothing seems to work I'm trying to run simple test: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/applicationContext.xml"}) @Transactional public class EmailServiceTest { I've also tried: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"src/main/webapp/WEB-INF/applicationContext.xml"}) @Transactional public class EmailServiceTest { Along with a few different things in place of "location," such as "classpath" and "file." The

Migrating JUnit JPA tests from Spring 2.5.5 to Spring 3.0.4

独自空忆成欢 提交于 2020-01-23 04:02:59
问题 Our project has just decided to migrate from Spring 2.5.5 to Spring 3.0.4. In my original code, I had unit tests that looked like the following: public class RequisitionDaoTest extends AbstractJpaTests { public static String FAIL_MSG_UNEXPECTED_ERROR = "FAIL: Unexpected Error"; @PersistenceContext private EntityManager em; @PersistenceUnit private EntityManagerFactory emf; @Test public void testSomething () { ... } } This worked just fine with Spring 2.5.5. Now with Spring 3.0.4, the

Migrating JUnit JPA tests from Spring 2.5.5 to Spring 3.0.4

99封情书 提交于 2020-01-23 04:02:32
问题 Our project has just decided to migrate from Spring 2.5.5 to Spring 3.0.4. In my original code, I had unit tests that looked like the following: public class RequisitionDaoTest extends AbstractJpaTests { public static String FAIL_MSG_UNEXPECTED_ERROR = "FAIL: Unexpected Error"; @PersistenceContext private EntityManager em; @PersistenceUnit private EntityManagerFactory emf; @Test public void testSomething () { ... } } This worked just fine with Spring 2.5.5. Now with Spring 3.0.4, the

Testing non-activity classes in Android

旧时模样 提交于 2020-01-22 17:45:26
问题 I know how to test Activity classes with JUnit 4 in Android but I am unable to understand how to test non-activity classes (which don't extends Activity, ListActivity, or some other Activity class, but uses some Android APIs). Please help me in this regard. 回答1: To test non activity classes: create a test project create a test case run as Android JUnit Test public class MyClassTests extends TestCase { /** * @param name */ public myClassTests(String name) { super(name); } /* (non-Javadoc) *

Spring MVC controller Test - print the result JSON String

前提是你 提交于 2020-01-22 04:18:49
问题 Hi I have a Spring mvc controller @RequestMapping(value = "/jobsdetails/{userId}", method = RequestMethod.GET) @ResponseBody public List<Jobs> jobsDetails(@PathVariable Integer userId,HttpServletResponse response) throws IOException { try { Map<String, Object> queryParams=new LinkedHashMap<String, Object>(); queryParams.put("userId", userId); jobs=jobsService.findByNamedQuery("findJobsByUserId", queryParams); } catch(Exception e) { logger.debug(e.getMessage()); response.sendError

No JSON content (comes up null) from MockMvc Test using Spring MVC and Mockito

徘徊边缘 提交于 2020-01-15 12:00:51
问题 Have a codebase which uses SpringMVC 4.0.3.RELEASE for Restful Web Services. Codebase contains a fully functional Restful Web Service which I can verify works using postman and curl. However, when trying to write a unit test for the particular Restful Web Service using MockMvc, I become blocked with trying to obtain the JSON content from the unit test. Am wondering if its a config issue or an issue where I am not creating a fake object correctly (since this doesn't rely on tomcat and is

Unit testing of encrypt/decrypt

蹲街弑〆低调 提交于 2020-01-14 10:32:29
问题 I have implemented a very simple class called Enigma which has a symmetric key and two methods: byte[] encryptString(String strToEncrypt) and String decryptBytes(byte[] arrayToDecrypt) . I am trying to write some tests for the methods. I have thought of testing that the encrypt and decrypt methods are the inverse of each other, but that says nothing about each of them individually. I wanted to use the methods as they are now to obtain a battery of input-outputs and set that as the tests (I

Unit testing of encrypt/decrypt

天涯浪子 提交于 2020-01-14 10:32:10
问题 I have implemented a very simple class called Enigma which has a symmetric key and two methods: byte[] encryptString(String strToEncrypt) and String decryptBytes(byte[] arrayToDecrypt) . I am trying to write some tests for the methods. I have thought of testing that the encrypt and decrypt methods are the inverse of each other, but that says nothing about each of them individually. I wanted to use the methods as they are now to obtain a battery of input-outputs and set that as the tests (I