junit

Mockito verify after exception Junit 4.10

徘徊边缘 提交于 2019-12-17 16:24:15
问题 I am testing a method with an expected exception. I also need to verify that some cleanup code was called (on a mocked object) after the exception is thrown, but it looks like that verification is being ignored. Here is the code. I am using the Junit ExpectedException Rule to verify the expected exception. @Rule public ExpectedException expectedEx = ExpectedException.none(); @Test public void testExpectedException() { MockedObject mockObj = mock(MockedObj.class); MySubject subject = new

running junits and cobertura with maven

人盡茶涼 提交于 2019-12-17 16:16:37
问题 In our project, we run both junits and cobertura using maven. The problem I am facing is that, junit test cases are running twice, once before the jar creation process and then once again for generating cobertura coverage reports. When running cobertura and junits with ant, we run junits only once since, cobertura runs along with junits. Is there a way to configure the above case with maven. I know we can use "maven.test.skip" property to skip junits. But when I do this, I am not able to see

Java, Junit - Capture the standard input / Output for use in a unit test [duplicate]

百般思念 提交于 2019-12-17 15:53:49
问题 This question already has answers here : JUnit test for System.out.println() (12 answers) Closed 5 years ago . I'm writing integration tests using JUnit to automate the testing of a console based application. The application is homework but this part isn't the homework. I want to automate these tests to be more productive -- I don't want to have to go back and retest already tested parts of the application. (Standard reasons to use Unit tests) Anyway, I can't figure out or find an article on

How can I get a list of instantiated beans from Spring?

做~自己de王妃 提交于 2019-12-17 15:46:34
问题 I have several beans in my Spring context that have state, so I'd like to reset that state before/after unit tests. My idea was to add a method to a helper class which just goes through all beans in the Spring context, checks for methods that are annotated with @Before or @After and invoke them. How do I get a list of instantiated beans from the ApplicationContext ? Note: Solutions which simply iterate over all defined beans are useless because I have many lazy beans and some of them must not

Spring not autowiring in unit tests with JUnit

谁说我不能喝 提交于 2019-12-17 15:46:34
问题 I test the following DAO with JUnit: @Repository public class MyDao { @Autowired private SessionFactory sessionFactory; // Other stuff here } As you can see, the sessionFactory is autowired using Spring. When I run the test, sessionFactory remains null and I get a null pointer exception. This is the sessionFactory configuration in Spring: <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name

Android Eclipse Plugin: Instrumentation Test Runner not specified

被刻印的时光 ゝ 提交于 2019-12-17 15:39:18
问题 I'm getting this error when trying to run unit tests from Eclipse with an Android Project. The list of Instrumentation Test Runners is empty in the Android preferences. [2009-06-17 23:57:51 - MyApp] ERROR: Application does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner It's also annoyingly decided that because I tried to run a unit test once, that's what I always want to do. 回答1: In the Run Configuration you may have

Excluding a non param test in parameterized test class

旧时模样 提交于 2019-12-17 15:38:11
问题 Is there any annotation in JUnit to exclude a non param test in parameterized test class? 回答1: JUnit 5 As of Junit 5.0.0 you can now annotate your test methods with @ParameterizedTest . So no need for inner classes. There are many ways to supply the arguments to the parameterized test apart from ValueSource as shown below. See the official junit user guide for details: import org.junit.jupiter.api.Test; import org.junit.jupiter.api.ParameterizedTest; import org.junit.jupiter.params.provider

How to intercept SLF4J (with logback) logging via a JUnit test?

主宰稳场 提交于 2019-12-17 15:36:07
问题 Is it possible to somehow intercept the logging (SLF4J + logback) and get an InputStream (or something else that is readable) via a JUnit test case...? 回答1: You can create a custom appender public class TestAppender extends AppenderBase<LoggingEvent> { static List<LoggingEvent> events = new ArrayList<>(); @Override protected void append(LoggingEvent e) { events.add(e); } } and configure logback-test.xml to use it. Now we can check logging events from our test: @Test public void test() { ...

Unit testing a JAX-RS Web Service?

独自空忆成欢 提交于 2019-12-17 15:34:09
问题 I'm currently looking for ways to create automated tests for a JAX-RS (Java API for RESTful Web Services) based web service. I basically need a way to send it certain inputs and verify that I get the expected responses. I'd prefer to do this via JUnit, but I'm not sure how that can be achieved. What approach do you use to test your web-services? Update: As entzik pointed out, decoupling the web service from the business logic allows me to unit test the business logic. However, I also want to

Mocking Logger and LoggerFactory with PowerMock and Mockito

妖精的绣舞 提交于 2019-12-17 15:32:14
问题 I have the following Logger I want to mock out, but to validate log entries are getting called, not for the content. private static Logger logger = LoggerFactory.getLogger(GoodbyeController.class); I want to Mock ANY class that is used for LoggerFactory.getLogger() but I could not find out how to do that. This is what I ended up with so far: @Before public void performBeforeEachTest() { PowerMockito.mockStatic(LoggerFactory.class); when(LoggerFactory.getLogger(GoodbyeController.class)).