Maven will not execute tests - maven

I have a default Maven directory structure, but it will not see/run my tests. The directory structure is as followed:
src
-main
-java
-com.foo.webservice
-...
-test
-java
-com.foo.webservice
-AbstractTest.java
When I run the command mvn test, it tells me nothing, but a successful build.
The project is viewable on my Git over here.
This is my test class:
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = Application.class)
#WebAppConfiguration
public abstract class AbstractTest {
#Autowired
private CustomerDAO dao;
#Test
public void testGetCustomer() {
Customer customer = new Customer();
customer.setUsername("JUnitCustomer");
customer.setCompanyName("SpringTest");
customer.setPhoneNumber("0612345678");
if (customer != null) {
assertNotNull("Username isn't null", customer.getUsername());
assertNotNull("Company isn't null", customer.getCompanyName());
assertTrue(customer.getPhoneNumber().length() == 8);
}
}
#Test
public void testGetCustomerPhoneNumber() {
assertTrue(dao.getCustomer(0).getPhoneNumber().length() == 8);
}
}

By looking at your code, the class you want to test with JUnit is abstract.
However, for JUnit to run your test, your class must not be abstract. You should declare your class like this:
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = Application.class)
#WebAppConfiguration
public class MyTest {
#Test
public void testSomething() {
}
}

Related

How to initalize spring bean for separation between tests in junit?

I'm using Junit5 and Spring for test.
I want to initalize spring bean for each test because I don't want different tests to change the other results of tests.
I'm knowing that a new instance of the test class is created before running each test method by default. under result of test codes is true,because the instance variable number is initalized for each test by junit5.
public class TestInstanceVaribale{
int number = 0;
#Test
public void test1() {
number += 3;
Assertions.assertEquals(3, number);
}
#Test
public void test2() {
number += 5;
Assertions.assertEquals(5, number);
}
}
but, this code is failed because spring bean is not initalized.
#Component
public class Car {
public String name = "myCar";
}
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
#SpringBootTest
#TestMethodOrder(OrderAnnotation.class)
public class TestSpringVariable {
#Autowired
Car car;
#Test
#Order(1)
public void test1() {
car.name = "testCar";
Assertions.assertEquals("testCar", car.name);
}
#Test
#Order(2)
public void test2() {
// this is fail. expected: <myCar> but was: <testCar>
// but I want expected: <myCar> but was: <myCar>
Assertions.assertEquals("myCar", car.name);
}
}
How to initalize spring bean for separation between tests in junit?
#SpringBootTest
#initalizeSpringBeanPerMethod <-- I want like this
public class TestSpringVariable2 {
#Autowired
Car car;
#BeforeEach
public void initalize() { <-- I want like this
SpirngBean.initalize()
}
}
Take a look at DirtiesContext
Probably adding this to your class should work. It's telling Spring to reset it's state after/before (depending on how you set it) each test
#DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)

How to inject certain properties values during junit to a specific test

My Spring Boot app has some test that are reading their properties from the application.yml that is in the test folder.
cat:
maxAge:30
maxNameSize:10
all is working fine, but I like that in certain tests, other values will be injected:
#ExtendWith(SpringExtension.class)
#ContextConfiguration(classes = {
Cat.class
})
#SpringBootTest
public class CatTest {
#Test
public void testX(){
//inject maxAge=90
// use maxNameSize from the application.yml
....
#Test
public void testZ(){
//inject maxNameSize=5
// use maxAge from the application.yml
....
}
Changing properties on method level is not supported by Spring at this moment.
You can use nested classes to accomplish this.
#ExtendWith(SpringExtension.class)
#ContextConfiguration(classes = {
Cat.class
})
#SpringBootTest
public class CatTest {
#Nested
#SpringBootTest(properties = "cat.maxAge=90")
public class NestedTestX {
#Test
void testX() {
//noop
}
}
#Nested
#SpringBootTest(properties = "cat.maxNameSize=5")
public class NestedTestZ {
#Test
void testZ() {
//noop
}
}
}

autowired #components null in unit test

I have a class:
#Component
public class B {
#Autowired
private A a;
}
and A is a component:
#Component
public class A{}
In unit test class BTest:
public class BTest {
#Test
public void testBMethod() {
}
}
I am not using an xml to define context or for beans to be picked from.
What is the cleanest way I can get the test to run?
You don't have to use Spring for the unit tests. Mockito may be used for this.
public class BTest {
#Mock
private A a;
#Mock
private B b;
#Test
public void testBMethod() {
}
}
For more details, you may check https://springframework.guru/mocking-unit-tests-mockito/
and https://dzone.com/articles/use-mockito-mock-autowired

How to make data repository available in spring boot unit tests?

I'm consistently getting NPE on the repository I'm trying to implement.
Here's the repo:
public interface EmployeeRepository extends CrudRepository<Employee, Long> {
Employee findByEmployeeId(String employeeId);
}
I'm just trying to do a simple integration test to make sure the app is wired correctly:
public class EmployeeRepositoryTest extends BaseIntegrationTest {
private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeRepositoryTest.class);
#Autowired
private static EmployeeRepository repo;
#Test
public void findByEmployeeIdReturnsAppropriateEmployee() {
Employee e = new Employee("Some", "Person", "0001111");
repo.save(e);
assertEquals("Did not find appropriate Employee", e, repo.findByEmployeeId("0001111"));
}
}
BaseIntegrationTest is just a collection of annotations:
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = Application.class)
#WebAppConfiguration
#ActiveProfiles("test")
#IntegrationTest("server.port:0")
public class BaseIntegrationTest {
#Test
public void contextLoads() {
}
}
Can I provide any more helpful information? Thanks.
Remove the keyword static from your repo field. Static fields aren't autowired.

test suite inside spring context

Is it possible to run test suite with loaded spring context, something like this
#RunWith(Suite.class)
#SuiteClasses({ Test1.class, Test2.class })
#ContextConfiguration(locations = { "classpath:context.xml" }) <------
public class SuiteTest {
}
The code above obviously wont work, but is there any way to accomplish such behavior?
This is currently how spring context is used in my test suite:
#BeforeClass
public static void setUp() {
final ConfigurableApplicationContext context =
loadContext(new String[] { "context.xml" });
jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
// initialization of other beans...
}
I have tried you code, the test suite are running with spring context loaded. Can you explain in more detail what the problem is?
here is the code:
#RunWith(Suite.class)
#SuiteClasses({ Test1.class, Test2.class })
public class SuiteTest {
}
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:context.xml" })
#Transactional
public class Test1 {}
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:context.xml" })
#Transactional
public class Test2 {}
If you want Suite class to have its own application context, try this:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:context.xml" })
#Transactional
public class SuiteTest {
#Test public void run() {
JUnitCore.runClasses(Test1.class, Test2.class);
}
}

Resources