Eclipse classpath entries only used for tests

You could separate all your tests into another project and add the main project as a dependency (Project->Properties->Java Build Path->Projects->Add…) Update: To avoid changing the original project structure, your test projects can use linked locations. Create the test project as normal, you now need to create a linked resource to bring in the src/test/java folder. … Read more

How do I use google mock in C?

I found a way to be able to mock bare C functions in google-mock. The solution is to declare foobar to be a weak alias that maps to foobarImpl. In production code you do not implement foobar() and for unit tests you provide an implementation that calls a static mock object. This solution is GCC … Read more

Unit Test Laravel’s FormRequest

I found a good solution on Laracast and added some customization to the mix. The Code /** * Test first_name validation rules * * @return void */ public function test_valid_first_name() { $this->assertTrue($this->validateField(‘first_name’, ‘jon’)); $this->assertTrue($this->validateField(‘first_name’, ‘jo’)); $this->assertFalse($this->validateField(‘first_name’, ‘j’)); $this->assertFalse($this->validateField(‘first_name’, ”)); $this->assertFalse($this->validateField(‘first_name’, ‘1’)); $this->assertFalse($this->validateField(‘first_name’, ‘jon1’)); } /** * Check a field and value against validation rule * … Read more

How to unit test a Spring MVC controller using @PathVariable?

I’d call what you’re after an integration test based on the terminology in the Spring reference manual. How about doing something like: import static org.springframework.test.web.ModelAndViewAssert.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({/* include live config here e.g. “file:web/WEB-INF/application-context.xml”, “file:web/WEB-INF/dispatcher-servlet.xml” */}) public class MyControllerIntegrationTest { @Inject private ApplicationContext applicationContext; private MockHttpServletRequest request; private MockHttpServletResponse response; private HandlerAdapter handlerAdapter; private MyController controller; … Read more

When is a Test not a Unit-test?

See Michael Feathers’ definition A test is not a unit test if: It talks to the database It communicates across the network It touches the file system It can’t run at the same time as any of your other unit tests You have to do special things to your environment (such as editing config files) … Read more