0

As I haven't gotten an answer for my other question here, I am looking for another approach. Is there a way to no not execute or include a fragment during unit testing?

I want to display the version and build number in the footer of my templates, thus I have the following line:

<div class="versionInfo">Version <span th:text="${@buildProperties.getVersion()}"></span></div>

This works well when running the application, but during unit test I get the exception:

No bean named 'buildProperties' available

In the other question I am looking for a way to get this bean during unit test, as an alternative I am now looking for a way to exclude this template during unit tests. Something like this:

<div class="versionInfo" th:if="${!isUnitTest}">Version <span th:text="${@buildProperties.getVersion()}"></span></div>

I am using the following annotations on my test class:

@RunWith(SpringRunner.class)
@WebMvcTest(SimpleController.class)
Thomas
  • 6,325
  • 4
  • 30
  • 65
  • Could you please share the java code. 1.Did you Autowire the class and is buildProperties available in context? 2. Did you use Mock or any ReflectionTestUtils ? ReflectionTestUtils.setField("yourClass", buildProperties, objectBuildProperties) – Mebin Joe Feb 22 '19 at 07:29
  • The problem with buildProperties is, that it is an automatic thing from spring when it is built: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-build.html – Thomas Feb 22 '19 at 07:33
  • Do you use @SpringBootTest in the test class – Mebin Joe Feb 22 '19 at 07:37
  • @MebinJoe No, I am using `@WebMvcTest` as. I also tried it with `@SpringBootTest` but then `MockMvc` does not work as expected. – Thomas Feb 22 '19 at 07:39
  • what is the exception you geting? – Mebin Joe Feb 22 '19 at 08:47
  • @MebinJoe Let's not discuss this problem in this ticket. Have a look at the other one https://stackoverflow.com/questions/54712564/webmvctest-does-not-find-buildproperties and see my comment here: https://stackoverflow.com/questions/48078044/webmvctest-fails-with-java-lang-illegalstateexception-failed-to-load-applicati/53394808#53394808 – Thomas Feb 22 '19 at 09:15

2 Answers2

1

If it's a standard unit-test, use Mockito to mock the bean:

@RunWith(MockitoJUnitRunner.class)
public class SomeTest{

  @Mock
  private BuildProperties buildProperties;

  ...

Or, if it's a Spring MVC test:

@RunWith(SpringRunner.class)
@WebMvcTest(value = MyController.class)
public class MyControllerTest{

  @MockBean
  private BuildProperties buildProperties;

The version will be null, because all of the methods of the mocked bean returns null.

if you want to emulate a real version you can add something like this to your test or to the setUp() method

given(buildProperties.getVersion()).willReturn("whatever");

--------------- edit

Unfortunately all of the above solutions work only if the buildProperties bean was added to the model directly. But in your case you use the bean directly as a SpEL bean reference. Unfortunately I don't know any way to check if a bean exists in the context via SpEL. Also, it's not a good practice to add extra code into your code for checking if it's running in test mode. So I think the best solution if you create a nested test configuration class and define a default BuildProperties bean there.

@TestConfiguration
public static class TestConfig {    
  @Bean 
  BuildProperties buildProperties() {
    return new BuildProperties(new Properties());
  }
}

Or you can use @Import(TestConfig.class) if you need this extra configuration in multiple test classes.

Selindek
  • 3,269
  • 1
  • 18
  • 25
  • I tried that but still get the same error mentioned in my OP. – Thomas Feb 22 '19 at 07:51
  • 1
    Ok, your edit did the trick for me. For completeness sake, maybe add that you can use `@Import(TestConfig.class)` in your test classes so you don't need to inline it. – Thomas Feb 22 '19 at 10:35
  • If you want some extra points, post your edited answer to my other question (you actually answered that one and not this one, but I have accepted it nonetheless): https://stackoverflow.com/questions/54712564/webmvctest-does-not-find-buildproperties – Thomas Feb 22 '19 at 10:47
0

I got a hint from a colleague that works for this question.

Just add an application.properties to src/test/resources with a custom property:

isUnitTest=true

Then I can simply check for this in my thymeleaf template:

<div class="versionInfo" th:if="${@environment.getProperty('isUnitTest') != 'true'}">Version <span th:text="${@buildProperties.getVersion()}"></span></div>

Though if someone finds a way to achieve this automatically with a thymeleaf setting or property I didn't find so far, I will accept it as an answer.

Thomas
  • 6,325
  • 4
  • 30
  • 65
  • Adding extra code for testing if the code is running as a test is never a good idea. Check my edited answer – Selindek Feb 22 '19 at 09:45
  • Yes, I am aware of that, but it is a workaround for now. I'll check your other suggestion. – Thomas Feb 22 '19 at 10:21