junit

Persist/commit not working in test environment with Spring JPA JUnit

ⅰ亾dé卋堺 提交于 2019-12-22 19:24:12
问题 I'm trying to setup a basic JPA insert test. But nothing is saved in the DB. DB is Postgresql. Hibernate is used as Persistence provider. Many thanks in advance. @Entity public class Currency { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) protected Integer id; @Column private String code; @Column private String name; ... } The CRUD class : @Repository @Scope(BeanDefinition.SCOPE_PROTOTYPE) @Transactional(propagation = Propagation.REQUIRED) public class CRUDServiceBean implements

Assertion fail message for string contains substring [duplicate]

你。 提交于 2019-12-22 18:46:25
问题 This question already has answers here : AssertContains on strings in jUnit (7 answers) Closed 6 years ago . I have done a lot of functional testing on text outputs on text generating software lately, an find myself writing a lot of assertTrue(actualString.contains(wantedString)); However, the message for when this fails is something non-descriptive like Expected [true], but was [false] An alternative is to include a custom fail message as String failMsg = String.format("Wanted string to

How to pass parameters to post method in Spring MVC controller while doing junit?

孤人 提交于 2019-12-22 18:37:40
问题 I am doing unit testing on my Spring MVC controller methods. Below is my method which I am trying to unit test as it is working fine when I start my server. Whenever I will be hitting index page it will show me three text box on the browser in which I am typing the data and pressing the submit button and then the call goes to addNewServers with the proper values. Now I need to unit test the same thing: @RequestMapping(value = "/index", method = RequestMethod.GET) public Map<String, String>

Testing protected method with JUnit

对着背影说爱祢 提交于 2019-12-22 18:31:03
问题 I am testing a method which is protected . In my test case I've used Reflection to access the method but I am not exactly sure whether I am doing it in a right way. Method to be tested: protected void checkORCondition( Map<String, Message> messagesMap ) throws EISClientException { Message message = containsAMessageCode(getMessageCodes(), messagesMap); if(message!=null) { throw new EISClientException("One of the specified message code matched returned errors." + message.getMessageCode() + ": "

Testing protected method with JUnit

老子叫甜甜 提交于 2019-12-22 18:30:11
问题 I am testing a method which is protected . In my test case I've used Reflection to access the method but I am not exactly sure whether I am doing it in a right way. Method to be tested: protected void checkORCondition( Map<String, Message> messagesMap ) throws EISClientException { Message message = containsAMessageCode(getMessageCodes(), messagesMap); if(message!=null) { throw new EISClientException("One of the specified message code matched returned errors." + message.getMessageCode() + ": "

Powermock not able to mock static methods when using scala, junit

ⅰ亾dé卋堺 提交于 2019-12-22 18:16:27
问题 I have used Powermock previously with java and junit. I have been successfully been able to mock static methods like in the following example: @PrepareForTest({ TimeHelper.class, MainApp.class }) @RunWith(PowerMockRunner.class) public class TestSuite { @Before public void setUp() throws IOException { PowerMockito.mockStatic(TimeHelper.class); Mockito.doReturn("2015-01-01 00:00:00").when(TimeHelper.getUnixTime()); } } However, the same doesn't seem to work when translated to scala. Note that

Mocking ActionContext.getContext().getSession() returns null

左心房为你撑大大i 提交于 2019-12-22 18:11:57
问题 I am trying to write jUnit test case for following method. public class MyClass { public static Map<String, Object> getSession() { Map<String, Object> session = ActionContext.getContext().getSession(); return session; } } I followed this question and also this question and tried to mock ActionContext . But still session is null . public class TestClass { private HttpServletRequest request; private HttpSession session; @Before public void setUp() { // mock the session session = mock

How can I run cleanup method only after tagged tests?

有些话、适合烂在心里 提交于 2019-12-22 18:10:26
问题 I'm writing JUnit 5 tests for my Java project. I have some test methods that require time consuming clean up (after each of them). Ideally, I would like to mark them with some annotation and run cleanup method only for them. This is what I tried: class MyTest { @AfterEach @Tag("needs-cleanup") void cleanup() { //do some complex stuff } @Test void test1() { //do test1 } @Test @Tag("needs-cleanup") void test2() { //do test2 } } I want cleanup method to be run only after test2 . But it actually

Error producing to embedded kafka queue after upgrade from 0.7 to 0.8.1.1

浪子不回头ぞ 提交于 2019-12-22 17:49:03
问题 I haven't been able to find anything that directly handled the problem I'm facing, so I'm posting here. I have JUnit/JBehave tests that spin up an embedded ZooKeeper server, embedded Kafka server, and kafka producers and consumers. After upgrading kafka from 0.7 to 0.8.1.1, I'm encountering the following types of errors: ERROR [kafka-request-handler-5] state.change.logger - Error on broker 1 while processing LeaderAndIsr request correlationId 7 received from controller 1 epoch 1 for partition

Can't suppress DriverManager's static initializer block

会有一股神秘感。 提交于 2019-12-22 16:40:44
问题 I have a unit test that attempts to create a SQLException to simulate a database error. In SQLException 's constructor, there is a call to DriverManager , which has a static initialization block. I figured that I could suppress the static block with this type of setup: @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor({"java.sql.DriverManager"}) public class StaticTest { @Test public void testStaticSuppress() throws Exception { SQLException ex = new SQLException(); expect(...)