I am new to JUnit
and automated testing and really want to get into automating my tests. This is a Spring Boot application. I have used Java Based Annotation style instead of XML based configuration.
I have a test class in which I'd like to test a method which retrieves a response based on a users' input.
Testing class:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleTest(){
@Autowired
private SampleClass sampleClass;
@Test
public void testInput(){
String sampleInput = "hi";
String actualResponse = sampleClass.retrieveResponse(sampleInput);
assertEquals("You typed hi", actualResponse);
}
}
Inside my "SampleClass" I have autowired a bean like this.
@Autowired
private OtherSampleClass sampleBean;
Inside my "OtherSampleClass" I have annotated a method like so:
@Bean(name = "sampleBean")
public void someMethod(){
....
}
The issue I'm having is when I try to run the test without the @RunWith
and @SpringBootTest
annotations when I try to run the test my variables annotated @Autowired
are null. And when I try to run the test with those annotations RunWith & SpringBootTest then I get an
IllegalStateException caused by BeanCreationException: Error creating bean with name "sampleBean" AND failure to load application context caused by BeanInstantiationException.
The code works 'properly' when I try to use it as a user would so I can always test this way but I think automated tests would be good for the longevity of the program.
I have used the Spring Boot Testing Docs to assist me in this.